IOS development note _ multi-thread dependency in AFN
We usually encounter two network requests at the same time during development, and then download the resources and merge them. What should we do in AFN? Of course, some personal packaging skills may be written below. If you are interested, you can continue to follow me.
# Pragma mark-getter
-(NSOperationQueue *) queue
{
If (! _ Queue ){
_ Queue = [[NSOperationQueuealloc] init];
}
Return_queue;
}
Here is an encapsulation class for AFN.
NSOperationQueue * mainQueue = [NSOperationQueuemainQueue];
// Create a network request object
RequestManager * manager = [RequestManagermanager];
The request parameters are also encapsulated here, because during development, a request may contain many common parameters, such as UID, DID, APPVersion, screen resolution, and system operation version, if it is not exhausting to write it once every time, it must be used to inherit the parent class. Only the parent class has these parameters, and its subclass must have them.
// Configure Request Parameters
FSDynamicParams * dynamicParams = [[FSDynamicParamsalloc] initWithDynamicListParamsWithCityId: @ 2 page: self. page];
// The AFHTTPRequestOperation request is encapsulated to make the code more concise.
AFHTTPRequestOperation * operationList = [manageroperationGETWithParams: dynamicParamsToController: selfSuccessHandle: ^ (id responseObject ){
FSLog (@ Step 1 );
}];
// Configure Request Parameters
FSDynamicParams * messageParams = [[FSDynamicParamsalloc] initWithDynamicMessageParams];
// The AFHTTPRequestOperation request is encapsulated to make the code more concise.
AFHTTPRequestOperation * operationMessage = [manageroperationGETWithParams: messageParamsToController: selfSuccessHandle: ^ (id responseObject ){
FSLog (@ Step 2 );
}];
NSBlockOperation * operation3 = [NSBlockOperationblockOperationWithBlock: ^ {
# It's strange to note that a delay thread must be opened before it can be executed in normal order. There are so many pitfalls in the AFN thread. record it.
Dispatch_after (dispatch_time (DISPATCH_TIME_NOW, (int64_t) (0.1 * NSEC_PER_SEC), dispatch_get_main_queue (), ^ {
FSLog(@Step 3);
});
}];
[Operation3addDependency: operationList];
[Operation3addDependency: operationMessage];
[Self. queueaddOperation: operationList];
[Self. queueaddOperation: operationMessage];
[MainQueueaddOperation: operation3];
Here we will first explain why we need to enable a 0.1-second latency in the result step of step 3. If we do not need to enable the latency during the demonstration at ordinary times, however, when you execute a large number of operations in step 2, the problem occurs, and the thread will want to be silly, such as step 1, result step, and step 3. Of course, I also suffered a lot of losses, and finally I studied it. I had to solve the problem but I had not gotten to the original one. I can only say that it is inexplicable. Maybe I have not enough knowledge yet. I can only Mark it, I will continue to look into it later.
Let's talk about some code of RequestManager In the encapsulation class.
@ Class BaseParams;
@ Class FSBaseViewController;
@ Interface RequestManager: NSObject
-(AFHTTPRequestOperation *) operationGETWithParams :( BaseParams *) params ToController :( UIViewController *) controller SuccessHandle :( void (^) (id responseObject) SuccessHandle;
@ End
@ Implementation RequestManager
+ (Instancetype) manager
{
RequestManager * manager = [[RequestManageralloc] init];
Return manager;
}
// Return the thread operation
-(AFHTTPRequestOperation *) operationGETWithParams :( BaseParams *) params ToController :( UIViewController *) controller SuccessHandle :( void (^) (id responseObject) SuccessHandle {
//Use the dictionary ParametersAFNIs convertedHttpStructure string
NSMutableURLRequest * request = [[AFHTTPRequestSerializerserializer] requestWithMethod: @ GETURLString: URL_BASEparameters: [paramskeyValue] error: nil];
AFHTTPRequestOperation * operation = [[AFHTTPRequestOperationalloc] initWithRequest: request];
// Set the returned parser to JSON format
Operation. responseSerializer = [AFJSONResponseSerializerserializer];
// Set thread operations
[OperationsetCompletionBlockWithSuccess: ^ (AFHTTPRequestOperation * operation, id responseObject ){
If ([responseObject [@ result] intValue]! = 0 ){
// Here is the re-encapsulation of MBProgressHUD and a class is defined
[MBProgressHUBhubWithShowText: responseObject [@ message] toView: controller. view];
Return;
}
FSLog (@ success: % @, responseObject );
SuccessHandle (responseObject );
} Failure: ^ (AFHTTPRequestOperation * operation, NSError * error ){
// Unified operations after a request failure, such as telling the user that the request times out, and what, can be determined here by the incoming controller, when you change the Controller to another one, you can perform operations to display different images.
// At the network request layer, network requests should be processed. In my opinion, network requests belong to this class in any situation.
If ([controllerisKindOfClass: [FSBaseTableViewControllerclass]) {
[[(FSBaseTableViewController *) controllertableView]. headerendRefreshing];
}
[MBProgressHUBhubWithShowStatusCode: NetWorkNotRechabilytoView: controller. view];
FSLog (@ failure: request --- % @, operation. request );
}];
Return operation;
}
I think the above Code is useful to you. Remember to support me. Okay, Mark is done. Continue to work on the code.