我有以下设置使用AFNetworking来调用我的服务器。我使用了我在互联网上找到的一个例子来包含一个完成块,所以我知道通话何时结束。
档案“FCEngine.m”
- (void)fetchBusinessProfile:(NSString *)userID userAccessToken:(NSString *)userAccessToken completion:(void (^)(NSDictionary *json, BOOL success))completion {
/// Validate the user token again the user id.
NSDictionary *parameters = [[NSDictionary alloc]initWithObjectsAndKeys:userAccessToken,@"user_access_token",
userID,@"user_id",
nil];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer];
manager.requestSerializer = serializer;
manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
[manager POST:@"" parameters:parameters progress:nil success:^(NSURLSessionTask *task, id responseObject) {
NSLog(@"JSON Business Profile: %@", responseObject);
fetchBusinessProfileCompletion(responseObject, YES);
} failure:^(NSURLSessionTask *operation, NSError *error) {
//NSLog(@"Error: %@", error);
NSMutableDictionary *errorResponse = [[NSMutableDictionary alloc] init];
[errorResponse setObject:@"connection_error" forKey:@"state"];
[errorResponse setObject:[error localizedDescription] forKey:@"description"];
fetchBusinessProfileCompletion(errorResponse, YES);
}];
}
- (void)fetchNotifications:(NSString *)userID userAccessToken:(NSString *)userAccessToken completion:(void (^)(NSDictionary *json, BOOL success))completion {
/// Validate the user token again the user id.
NSDictionary *parameters = [[NSDictionary alloc]initWithObjectsAndKeys:userAccessToken,@"user_access_token",
userID,@"user_id",
nil];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer];
manager.requestSerializer = serializer;
manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
[manager POST:@"" parameters:parameters progress:nil success:^(NSURLSessionTask *task, id responseObject) {
//NSLog(@"JSON: %@", responseObject);
completion(responseObject, YES);
} failure:^(NSURLSessionTask *operation, NSError *error) {
//NSLog(@"Error: %@", error);
NSMutableDictionary *errorResponse = [[NSMutableDictionary alloc] init];
[errorResponse setObject:@"connection_error" forKey:@"state"];
[errorResponse setObject:[error localizedDescription] forKey:@"description"];
completion(errorResponse, YES);
}];
}
以下是我在主视图控制器
上进行调用的方法- (void)MyMethods {
[self.fcEngine fetchBusinessProfile:userID userAccessToken:userAccessToken completion:^(NSDictionary *json, BOOL success) {
/// Response here
}];
[self.fcEngine fetchNotifications:self.userID userAccessToken:self.userAccessToken completion:^(NSDictionary *json, BOOL success) {
//// Response here
}];
}
现在的问题是2个调用是一个接一个地进行的,当我获取一个例如“fetchBusinessProfile”两个竞争块都被调用。
我设置错了吗?如果有2个或更多个调用,我只希望为该特定块调用完成,而不是全部调用。
答案 0 :(得分:1)
我认为您不了解异步以及完成块。如果您按上述定义进行2次网络呼叫,则可以按任何顺序进行。 completion
和fetchBusinessProfile
中的fetchNotifications
将是不同的完成块... 除非使它们相同。
例如:
[self.fcEngine fetchBusinessProfile:userID userAccessToken:userAccessToken completion:^(NSDictionary *json, BOOL success) {
/// Handle response
// Note calling the SAME completion block
sameCompletionBlockAlreadyDefined();
}];
[self.fcEngine fetchNotifications:self.userID userAccessToken:self.userAccessToken completion:^(NSDictionary *json, BOOL success) {
//// Handle response
// Note calling the SAME completion block
sameCompletionBlockAlreadyDefined();
}];
在这种情况下,sameCompletionBlockAlreadyDefined()
是一些已定义的块。在这种情况下,每个呼叫的块体确实是,但通过sameCompletionBlockAlreadyDefined
汇集到同一个呼叫。您可能会感到困惑,因为completion
恰好在您的第一个代码段中被命名为相同。
请注意,您的问题措辞非常差,因此并不完全明确您的意思。
更大的问题是你的目标是什么?您是否只想在结束时调用一个完成块?或者你想要完全不同的完成块?两者都需要不同的技术明确你的目标是什么。
前者是dispatch_group
的最佳服务。后者需要不同的完成块。
调度组的一个例子是:
dispatch_group_t group = dispatch_group_create();
dispatch_group_enter(group);
[self.fcEngine fetchBusinessProfile:userID userAccessToken:userAccessToken completion:^(NSDictionary *json, BOOL success) {
/// Handle response
dispatch_group_leave(group);
];
self.fcEngine fetchNotifications:self.userID userAccessToken:self.userAccessToken completion:^(NSDictionary *json, BOOL success) {
//// Handle response
dispatch_group_leave(group);
}];
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
// This would be some completion block which means all is done
completion();