1. Introduction
Starting from this chapter, we will start with the architecture design scheme of Alipay client, and subdivide and disassemble the specific implementation of alipay client in “container framework design”, “network optimization”, “performance startup optimization”, “automatic log collection”, “RPC component design”, “mobile application monitoring, diagnosis, positioning” and so on. Lead you to further understand the iteration and optimization process of Alipay’s client architecture.
This section will introduce the basic idea of the design of Alipay iOS container framework.
2. Overview of container implementation
In the opening introduction of mPaaS, I have shared with you the practice of Modularity and Decoupled Development in ANT Financial mPaaS: By isolating the business into relatively independent modules through the container development framework and focusing on high cohesion and low coupling between modules, we achieved flexible plug-in development and were able to divide the business into thousands of independent projects.
MPaaS iOS framework is derived from Alipay client. In order to realize the low coupling and related dependency calls among thousands of projects, mPaaS framework directly takes over the life cycle of App. Responsible for the entire App startup hosting, App lifecycle management, processing and distributing UIApplication agent events. The mPaaS framework provides a containerized environment in which business developers use microapplications and services to develop specific business requirements.
Microapplications and services are concepts defined within the mPaaS framework and are used to divide business modules. The mPaaS framework divides different business modules into microapplications and services based on whether there is a UI interface as a standard. A micro application is a business module with a user interface during APP operation; A service is a lightweight abstract service provided by the business during the App runtime. In THE mPaaS framework, the life cycle management of micro applications and services is carried out through the framework Context.
3. Application lifecycle management
By modifying the implementation of the main.m function, the mPaaS framework takes over the various App lifecycle in UIApplicationDelegate with its own ClientDelegate class. With the introduction of the mPaaS framework, the ClientDelegate completely replaces the role of the AppDelegate in general projects, thus enabling the entire application lifecycle to be managed by the framework.
int main(int argc, char * argv[]) {
@autoreleasepool {
// Now use mPaaS framework
return UIApplicationMain(argc, argv, @"Application"The @"ClientDelegate"); }}Copy the code
To make it easier for users to access the App lifecycle to develop custom functionality, the mPaaS framework provides equivalent access to all UIApplicationDelegate proxy methods in the DTFrameworkInterface class. You just override the corresponding method in the Category of the DTFrameworkInterface.
For example, the following common UIApplicationDelegate proxy method:
@protocol UIApplicationDelegate<NSObject>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
- (void)applicationDidBecomeActive:(UIApplication *)application;
- (void)applicationWillResignActive:(UIApplication *)application;
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation;
@end
Copy the code
The corresponding methods are provided in the DTFrameworkInterface:
typedef NS_ENUM(NSInteger, DTFrameworkCallbackResult) { DTFrameworkCallbackResultContinue = 0, / / continue DTFrameworkCallbackResultReturn = 1, / / interrupt execution DTFrameworkCallbackResultReturnYES = 2, after / / interrupt logic, And returns YES DTFrameworkCallbackResultReturnNO = 3, after / / interrupt logic, and returns NO}; @interface DTFrameworkInterface : NSObjectPragma mark - Application configuration, micro application configuration, service configuration, Scheme processor configuration, can be overridden by Category
- (void)application:(UIApplication *)application beforeDidFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
- (DTFrameworkCallbackResult)application:(UIApplication *)application handleDidFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
- (void)application:(UIApplication *)application afterDidFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
- (DTFrameworkCallbackResult)applicationDidBecomeActive:(UIApplication *)application;
- (DTFrameworkCallbackResult)applicationWillResignActive:(UIApplication *)application;
- (DTFrameworkCallbackResult)application:(UIApplication *)application
openURL:(NSURL *)url
newURL:(NSURL **)newURL
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation;
@end
Copy the code
Since the mPaaS framework has some of its own initialization logic to implement, In DTFrameworkInterface provides extra beforeDidFinishLaunchingWithOptions and afterDidFinishLaunchingWithOptions method, It is convenient for users to execute their initialization code at the time determined when the App is started.
Will start DTFrameworkInterface before afterDidFinishLaunchingWithOptions BootLoader that perform mPaaS initialization logic framework. In an embedded operating system, the role of the BootLoader is to initialize the hardware device in order to prepare the right environment for the final call to the operating system kernel. Similarly, in the mPaaS framework, the BootLoader is used to initialize the entire mPaaS framework environment. By default, the following process is implemented in sequence:
- Create a window
- Create a main NavigationController
- Run services that can only be run once and need to be started first
- Start all other non-Lazyload services
- Start Specifies the group of services to be started automatically in the “[AUTOSTART]” array of the ServicesMap
- According to the main Window
- Launch the micro application Launcher and display the home page
This completes the initialization of the mPaaS framework and displays the home page.
The three key concepts are described in detail: microapplication, service, and framework Context.
4. The application
A micro application is an independent business module with A UI interface. The most special micro application is the micro application Launcher. As the first micro application opened after an App is launched, Launcher is usually used to create the home page of an App. In the mPaaS framework, microapplications are highly independent and not interdependent.
Microapplications are registered through the PList file configuration. When configuring the microapplication, you need to specify the delegate class name, the description of the microapplication, and the name to use when opening the microapplication. So the framework Context opens the specified microapplication by the name of the microapplication.
To facilitate business development, each microapplication also has a life cycle. The life cycle of a microapplication is modeled after the life cycle of an iOS App. Each application needs to implement their own DTMicroApplicationDelegate agent, this is similar to the iOS App Appdelege implementation in class.
For specific business development, micro-application development is just like a complete App. Each micro-application is responsible for controlling its own page stack and performing corresponding operations according to the lifecycle of the micro-application. In the mPaaS framework, all microapplications run in the container provided by the mPaaS framework and are not concerned with the life cycle of the App. For specific business scenarios, mPaaS supports the creation of multiple instances of microapplications.
5. Service
Services differ from microapplications in that they have no UI and are executed in the background. Once a service is started, it persists throughout the client’s life cycle, so a service is typically used to provide a common service to a microapplication, such as performing a function or retrieving data.
A common service is the user login status service. Each microapplication can obtain the user login status and user information through this service.
Services are also registered through the PList file configuration. When registering a service, you need to provide the unique identification name of the service and the corresponding implementation class class name. When the framework creates a service, it uses the objective-C runtime mechanism to create an instance of the service implementation class. LazyLoading controls whether to load the class lazily. In the case of lazy loading, the service is not instantiated when the framework starts, but only when the service is used. If not lazily loaded, the service is started when the framework starts.
Due to the particularity of services, mPaaS also provides a ServicesMap to batch register classes. [AUTOSTART] in the ServicesMap is used to explain which groups of services need to be started first when the App is started.
This feature of hierarchical startup service can effectively control the startup time of the App and provide a good user experience.
Each service needs to implement the service interface:
@required /** * starts a service. * Note: * The framework calls this method after the initialization is complete. * If a service wants to start an application, it must start other applications only after this method is called. */ - (void)start; @optional /** * The service is created. */ - (void)didCreate; /** * Service to be destroyed. */ - (void)willDestroy;Copy the code
After adding services, the structure of the entire App is shown in the figure below. The service in the background becomes the bridge of communication between each micro application.
6. The Context
You already have a deep understanding of microapplications and services. In the mPaaS framework, the framework Context assumes the role of a dispatcher, responsible for the scheduling and communication management of each micro-application and service, so that the opening, page pushing and closing of each micro-application will not affect other micro-application modules of the App.
The framework Context object is obtained through the DTContext * DTContextGet() function provided by the mPaaS framework. A simplified Context class is implemented as follows:
@interface Context : NSObject @property(nonatomic, strong) UIWindow *window; @property(nonatomic, strong) UINavigationController *navigationController; // start a microapplication with the specified name - (BOOL)startApplication:(NSString *)name params:(NSDictionary *)params :(BOOL)animated; // Iterate backwards through the stack, returning the latest microapplication object. - (DTMicroApplication *)findApplicationByName:(NSString *)name; - (id)findServiceByName:(NSString *)name; - (BOOL)registerService:(id)serviceforName:(NSString *)name; / / registered against an existing service - (void) unregisterServiceForName (nsstrings *) name; @endCopy the code
For business developers, they can use the framework Context to get the main window, start the specified micro application, get a service, dynamically register and unregister the service, so as to realize the connection between businesses.
7. Summary
Through this section, we have a preliminary understanding of the design idea of the container framework of mPaaS on the iOS side, and complete the decoupling and invocation between business modules by means of micro applications and services. As a mini container operating system, Context provides the required container environment for the operation of micro applications and services, ensuring independent business development process and smooth user experience.
Welcome to experience the CONTAINER development framework of mPaaS, and look forward to your feedback and communication.
Past reading
“Alipay Client Architecture Analysis: A Preliminary Study of Android Container Framework”
The opening | modular and decoupling type development in ant gold mPaaS theorypractice probing depth,
Dependency Analysis Guide between Bundles of Koubei App
The source code analysis | ant gold suit mPaaS RPC calls under the framework of course”
“Practice of Alipay Mobile Terminal Dynamic Solution”