In iOS development, we often encounter the problem of multiple network requests. Sometimes, we need to request another network after the end of a network request. In fact, the simplest and lowest method is to request another interface information in the callback of a successful network request

The code used to implement the semaphore counter is as follows:

static NSString *userSign; static NSString *userId; dispatch_group_t group = dispatch_group_create(); dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); dispatch_queue_t queue = dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL); dispatch_group_async(group, queue, ^{ [[BFLoginService shareInstance] getImSignWith: @"gjs598" complete:^(id _Nonnull data, requesterError * _Nonnull error) {// Task 1 NSLog(@" get IM signature complete %@", data[@"UserSig"]); userSign = data[@"UserSig"]; dispatch_semaphore_signal(semaphore); }]; dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); }); dispatch_group_async(group, queue, ^{ [[BFLoginService shareInstance] getImUserId:^(id _Nonnull data, RequesterError * _Nonnull error) {// Task 2 NSLog(@" Get user ID completed %@", data[@"userId"]); userId = data[@"userId"]; dispatch_semaphore_signal(semaphore); }]; dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); }); Dispatch_group_notify (group, queue, ^{NSLog(@" ahhhhhhhhhhhhhhhhhhhh %@ ----- %@", userId, userSign); // task 1/ task 2 completed, now task 3 [[v2TimManager SharedInstance] login: userId userSig: UserSign succ:^{// NSLog(@Weakify (self) [[V2TimManager ShareDInstance] getConversationList: INT_MAX count: INT_MAX succ: ^(NSArray<V2TIMConversation *> *list, uint64_t lastTS, BOOL isFinished) { // @strongify(self) // [self updateConversation: List]; NSLog (@ "% @" session list, a list);} fail: ^ (MSG) int code, nsstrings * {NSLog (@ "pull session list fail");}];} fail: ^ (int code, Nsstrings * desc) {NSLog (@ "id login failed -- -- -- -- -- -- -- -- -- -"); NSLog (@ "% @" desc);}]; });