Semaphore (dispatch_semaphore_t) - Implement multiple requests for one page

Posted by thecard on Wed, 05 Jun 2019 18:32:40 +0200

Goal: Achieve multiple requests for one page

We often encounter situations where, for example, there are multiple interfaces in the data display of the user interface, and the page is displayed when our data request is complete.What should I do now?Or what do we do when the interfaces inside go from one layer to another (for example, we need to get the data of the user's basic interface to get other information about the user)?For this purpose, we used semaphores

Introduction to the semaphore (dispatch_semaphore_t) (Google is here, you can go and see it, but you don't know it either)

A semaphore is a resource counter that operates on two semaphores to achieve mutual exclusion, P and V.A common scenario for critical or mutually exclusive access is to set the semaphore value to 1, and when a process 1 runs, uses the resource to perform a P operation, that is, to reduce the semaphore value by 1, that is, to reduce the number of resources by 1.This is a semaphore value of 0.The system specifies that when the semaphore value is 0, you must wait until you know that the semaphore value is not zero to continue.At this point, if process 2 wants to run, it must also do P operation, but at this time the semaphore is 0, so it cannot be reduced by 1, that is, it cannot operate P and it will be blocked.This brings you to process 1 exclusive access.When process 1 finishes running, the resource is released for V operation.The number of resources is re-added to 1, which is the value of the semaphore changes to 1. When process 2 finds that the number of resources is not zero, the semaphore can perform P operation, and then P operation is executed immediately.The semaphore value changes to 0.Number of times Process 2 We have resources and exclusive access to resources.This is how semaphores control mutual exclusion

There are three main functions:
dispatch_semaphore_create to create a semaphore
dispatch_semaphore_signal sends a signal (which naturally adds 1 to the total signal)
dispatch_semaphore_wait waits for a signal (when the total signal is less than 0, it waits all the time, otherwise it can execute normally and let the total signal amount -1)

Achieving goals

1: Using GCD method

dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self loadUserData];
}) ;
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self loadBalaceData];
}) ;
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self loadUserFriendsData];
}) ;
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
//Show Page
[self showUserView];
});

-(void)request2{
//Create semaphore and set count to 0 by default
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
NSDictionary * para ;//I won't expose the parameter details
[WJFCollection postWithUrlString:@"url" Parameter:para success:^(id responseObject) {
//Just send a signal here
dispatch_semaphore_signal(semaphore);
} failure:^(NSError *error) {
//Just send a signal here
dispatch_semaphore_signal(semaphore);
NSLog(@"%@",error);
}];
//Wait until the count is 0
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
}

2: Using NSOperationQueue method

NSBlockOperation *operation_1 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"1");
[self loadUserFriendsData];
}];
NSBlockOperation *operation_2 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"2");
[self loadBalaceData];
}];
NSBlockOperation *operation_3 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"3");
[self loadUserData];
}];
//Set Dependencies
[operation_2 addDependency:operation_3];
[operation_1 addDependency:operation_2];
//Create a queue and add tasks (set No here)
NSOperationQueue *queue = [[NSOperationQueue alloc]init]; [queue addOperations:@[operation_3,operation_2,operation_1] waitUntilFinished:NO];
//Note that this must be No, or the thread will be blocked

Referenced articles:

http://www.jianshu.com/p/04ca5470f212

Topics: Google less