IOS component ideas – great God blog study and thinking

A great God blog study

With the gradual application of iterative demand, the size of the application code will be more and more large, in order to better manage the application of the project, we began to use the CocoaPods version of the management tools for the original application engineering. However, it is not enough to solve the code coupling between the business only, in order to better let the split out of the business project can run independently, we must split the components and achieve component service.

Here are some of the recent industry in the great God of the blog debate on the war, the specific information is as follows:

  • 2016.03.10 App components of the road
  • 2016.03.13 iOS application architecture on component solutions
  • 2016.03.14 App components of the road continued
  • 2016.03.14 iOS application architecture on component solutions (Supplement)
  • Exploration of 2016.03.18 iOS component scheme
  • 2016.03.21 onlookers fight God, anti revolutionary Engineer iOS application architecture to talk about the components of the program and Limboy’s App component of the road of reading guidance

Recently, in reference to the great God who discussed and the basis of the previous LDBusBundle program, extract a suitable for small and medium applications LDBusMediator middleware, is gradually used in the project.

middleware Git open source address:

(1)’s component solution

Article source:

2016.03.10 App components of the road:

Why component?
  • No explicit constraints between components and components;
  • Component development, separate testing, can not be rubbed into the project development, testing can also be targeted test;
How to manage short chain? (URL jump)
[MGJRouter registerURLPattern:@ "mgj://detail? Id=: ID toHandler:^ (NSDictionary *routerParameters) {NSNumber *id routerParameters[@ =" Id "]; / / create view controller with ID view controller / / push}]; [MGJRouter openURL:@ mgj:// detail id=404?"

How to manage short chain?

  1. The background of the management of the short chain; platform to generate the required documents, IOS platform to generate h, m file, Android generated java file into the project;
  2. Developers view generated files to understand all available URL;
  3. Disadvantages: unable to get the parameters passed through the build;
Synchronous Action call? (service call)

Method 1: by way of URL

[MGJRouter registerURLPattern:@ "mgj://cart/ordercount" toObjectHandler:^id (NSDictionary *routerParamters do some calculation return) {/ / @42; NSNumber *orderCount [MGJRouter objectForURL:@}] = "mgj://cart/ordercount"]

Methods: two through Protocolclass corresponding way

To unify the public protocol file into PublicProtocolDomain.h, all business components depend only on this file; protocol can only be provided by the class method?

@protocol MGJCart < NSObject> + (NSInteger) orderCount; @end [ModuleManager registerClass:MGJCartImpl forProtocol:@protocol (MGJCart)] [ModuleManager classForProtocol:@protocol (MGJCart)]
Component lifecycle management: component management

Start the initialization, an instance of APP in the module instance of all components, so that each component of the module instance through the didFinishLaunchingWithOptions method: in this method each component to register their own URL, use the class registration; each component can automatically inform the monitoring system, such as UIApplicationDidBecomeActiveNotification, no system of notification messages will be written to module this method protocol, protocol are examples of implementation of these methods;

[[ModuleManager sharedInstance] loadModuleFromPlist:[[NSBundle mainBundle] pathForResource:@ "modules" ofType:@ "plist" NSArray [[ModuleManager sharedInstance] "; *modules = allModules]; for (id< ModuleProtocol> module in; modules) {if ([module respondsToSelector:_cmd]) {[module application:application didFinishLaunchingWithOptions:launchOptions]}};
Component version management
  1. Version synchronization: API interface changes upgrade (the old interface does not exist, not backward compatible), in a version of the change; all rely on its call to change, in order to ensure housing projects and main project can be compiled through synchronization;
  2. After pod update compiler too long: consider the way to modify the framework;
  3. Continuous integration: not just put podspec directly into the private repo in the end, the need to throw to the main project package compile, compile version upgrade by allowing, don’t throw it back through processing; CI compiler checks by then version upgrade to private repo, the main project and modify the Podfile version dependent no; but if it is other engineering, by multiple businesses rely on engineering, how to do? open source components:


  1. The main problem is to find URL JLRoutes implementation is not efficient enough, through the traversal rather than matching. There are more features.
  2. HHRouter URL lookup is based on matching, so it will be more efficient, MGJRouter is also used in this approach, but it is bound with the ViewController is too tight, to some extent, reduce flexibility.

(2) the anti revolutionary programme

Article source:
  • 2016.03.13 iOS application architecture on component solutions
  • 2016.03.14 iOS application architecture on component solutions (Supplement)
Why’s plan is not good?
  • URL registration is not necessary for the implementation of components, scalability and maintainability are reduced;
  • OpenURL scheme based on the words, there is a fatal flaw: unconventional objects can not participate in the local assembly scheduling; but can be solved by transfer params, but this distinction between the entrance remote call local call;
  • Does the module still need to use URL to complete the scheduling? It is not necessary, why complicate it?
The anti revolutionary component program:

The Mediator model and the model based on TargetAction:

[CTMediator sharedInstance] openUrl:url] //call from other app with URL parseUrl performTarget:action:params //call form Native Module runtime [TargetA action1] [TargetA action2], [TargetB action1], [TargetB action2]
The call of the counter revolutionary component scheme:

Local inter component call:

[[CTMediator sharedInstance] performTarget:targetName action:actionName params:@{... }]

Remote application call:

OpenUrl + parseUrl mode; for the request of the routing operation, the Target and Action directly into the name of the url;
The benefits of a counter revolutionary modular approach:
  1. The remote call and the local call are split, and the local application calls the remote application call to provide services;
  2. Components can only be exposed through the Action call interface;
  3. The component scheme must go to the Model design: it is not necessary for the caller to rely on Mediator when the caller relies on it;
  4. How does the caller know what Key parameters the recipient needs and how to know which target can be called? Maintenance for Mediator Category: in mediator, each category corresponds to a target, the categroy method in the corresponding Action scene; category combination model, provides different methods according to different classification, each component has a corresponding category classification; parameter validation and remedial entrance; easy requests; unified all calls between components entrance; param hardcode in the app domain only exists in the category, with the call almost; safety guarantee, to ensure the native prefix validation in URL; dynamic scheduling considering;
Anti revolutionary component program open source Demo:

Code Git address:

Two, the actual project of the component problem

(1) why do they need to be modular?

  • Solve the problem of more people (better collaboration), more needs (better functional modules division);
  • To solve the problem of code coupling between project modules; (strongly resist the direct reference between business component codes)

(2) how to split components? The main issues discussed by the gods are the product business component

  • Basic functional components: (similar to the performance statistics, Networking, Patch, network diagnosis etc.) according to the functions of the library, do not involve the product business needs, with similar business component library Library upper arch through good interface; do not write logic by extending the interface customization, complete customization;
  • The basic UI components (for example: pull-down refresh components, similar to iCausel) in general UI (product components; design of each business module depends on the use, but the need to maintain a good custom extension of the general public (UI) components; visual design, not related to specific products currently less)
  • Product components: Business (such as circle, 1 yuan purchase, login, customer service MM) business functions are independent, do not rely on Model to share each other; business between the page calls can only jump through UIBus; business logic between Action calls only through the services provided;

(3) the problems that need to be solved in component engineering?

  • Component page Jump (UIBus) program requirements: to transfer common parameters (system data types) and complex parameters (object URL cannot load), is not responsible for the transfer of CustomModel; can be configured dynamically to obtain URL corresponding to controller TabController; to controller present custom URL registration shall be made; code completion, must go to the treatment center;
  • Component service (ServiceBus) requirements: to transfer common parameters and complex transmission parameters, try not to use CustomModel; dependent base through a unified interface file; (business development stage only depends on the interface file depends on the library in the main project integration test depends on all business component interface and testing) the class that implements the corresponding code to complete the registration shall be made by the middleware, to control the service implementation class generation;

Generic problem: the hard coded problem of transferring key values with complex parameters

(4) component maintenance problems?

  • Design of component service interfaces: minimum principles; naming conventions;
  • Version release: version number specification: continuous integration, completed by script:

Three, on the thinking and summary of components

MGJRouterModuleManager (
scheme CTMediator+Target-Action scheme (counter revolutionary scheme)

(1) to solve the communication problem between local business components

The main component is to solve the local business component calls between App or Hybrid, as for the cross page through the openUrl page and service mode can be split into two steps: problem specific module parsing + middleware calls. Cross App through the info.plist configuration of the scheme jump into, hybrid page Jump through the JSBridge framework to enter, this part has a specific module to complete the analysis. It is not something that a component middleware has to do to make sure that the page or service for other business components is determined by a particular module in a particular module.

(2) from the perspective of engineering code, the component is to solve the problem of direct reference and confusion among the components in the middleware;

From the actual development, component between the maximum demand is the page Jump, need from the components of the A pageA1 page to B component of the pageB1 page, to avoid direct dependence on the components of the B page ViewController file. Followed by the call of the service, the service call module is not to solve the problem of URL jump, but the service call can be used to solve the needs of the page Jump, but there is no URL jump program cost is low. So there are differences between class protocol and ModuleManager MGJRouter of solutions; and counter revolutionary solutions still use Target-Action solutions to the page Jump problem, the cost is slightly larger; and the URL jump and call services are two different kinds of inter component communication needs, using two different ways to accomplish more indexing.

(3) the pure middleware is only responsible for the communication problem of the hanging node, and should not involve any logic of hanging the specific business of the contact point.

If the middleware involves specific business logic, is bound to cause direct dependence on middleware business modules, so the middleware need only abstracts the basic duties of business communication, provides a good interface, can complete the scheduling function.

Each connected node (here refers to the business component) follow the middleware protocol to complete the hanging, of course, this will cause the hanging joint on middleware protocol dependent; the caller must also call the push operation to the middleware by hanging contacts provided, not the calling process specific, such as hanging node dependent middleware, business logic is not directly dependent on middleware. This is the bus bus before Ali wireless sharing of ideas, through this way even if the switch or remove the middleware, only need to be modified can be completed in hanging nodes, avoid direct calls to the business logic code changes.

As for the removal of middleware, the application is still able to run the proposition? If there is no modification of any code, it is equivalent to the solution to the Lotus Bridge removed, and then the framework can not meet the cattle.

  • The call of the counter revolutionary framework directly depends on the call method provided by the middleware, and the middleware needs to be modified.

(4) whether the middleware should solve the components’ external disclosure of URL calls and service interface information?

Middleware solves the problem of communication between components, it is necessary to hide the information provided by the component, otherwise it can not achieve the goal of communication. program disclosure method:

  • URL short chain in the background management, automatic generation can view the.{h, m, java} file, developers through this file to view, the code file with the document function is similar; can not solve the problem of parameter key, type;
  • Service call interface unified on the PublicProtocol.h file, all other business components are required to rely on this file;

(whether the URL short chain and publicProtocol files into a repo, in fact, is equivalent to the role of documentation)

Disclosure of counter revolutionary programmes:

  • By relying on the middleware category (target), all the components of the business components are exposed by the category method;
  • Advantages: to solve the problem of URL parameter key, type checking; through Target – Action solution at the same time to solve the URL short chain and service calls communication needs, and more suitable for the style of the ape to solve the problem.

Four, our component program

After listening to Ali’s component sharing, he did a set of Bus bus program, but in the process of the use of specific products or trouble, it is difficult to promote the project is relatively large. Especially about the information disclosure of the component parts, is not a good idea to now, although the counter revolutionary solution to the issue of disclosure, but I think the expansibility and maintainability is still relatively poor.

Git open source address:

After reading a few blogs and Bryant recently discussed with some new ideas, hope to be able to continue in accordance with the bus + category ideas. The research, hope to a really suitable for implementation in the project plan.

Recently, in reference to the great God who discussed and the basis of the previous LDBusBundle program, extract a suitable for small and medium applications LDBusMediator middleware, is gradually used in the project.

middleware Git open source address: