During the development, the interface will occasionally encounter the problem of interface data dependence. For example, interface 2 needs a callback parameter returned by interface 1 as an input parameter to request interface 2. In this case, interface 2 needs to wait for interface 1 to complete the request before invoking interface 2. Semaphores, GCDS, etc are generally used.

When faced with this kind of problem, bloggers usually use semaphores. Such as distributed

__block dispatch_group_t group = dispatch_group_create();
dispatch_group_enter(group);
NSDictionary *arguments = @{@"type":@(1)};
[Network JSON_POST:arguments view:self.view prefix:@"" funItem:@"" success:^(NSDictionary * _Nonnull response){
    dispatch_group_leave(group);
} error:^(NSDictionary * _Nonnull response) {
} failure:^{
}];
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
    
});
Copy the code

Or use the following nested hell, the advantage is very intuitive to solve the problem, the disadvantage is that if only two layers of nesting is good, when more can let people see the scalp tingling, late maintenance trouble is easy to be sprayed.

[Network requestData2:^{} error:^{! Error?: error();}];} error:^{! Error?: error();} error(); }];Copy the code

And then I came up with local blocks, and blocks can be written inside methods, and blocks look a little bit more straightforward.

- (void)viewDidLoad { [super viewDidLoad]; Void (^requestData3)(NSString *value) = ^(NSString *value){ } failure:^{failure();}]; }; Void (^requestData2)(NSString *value) = ^(NSString *value){// } failure:^{failure();}]; }; [the Network request: @ {@ "data1:" @ "a"} success: ^ {/ / interface 1 requestData2 (@ "call data2);} failure: ^ {failure ();}]. }Copy the code