我有以下使用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);
}];
}
以下是我如何在Main View Controller上进行呼叫的方法
- (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
}];
}
现在的问题是,这两个调用是一个接一个地进行的,当我获取一个数据时,例如“ fetchBusinessProfile”在两者上的竞争块均被调用。
我把这个设置错了吗?如果有2个或更多调用,我只希望针对该特定块而不是全部调用完成。
最佳答案
我认为您不了解异步以及完成块。如果您按照上述定义进行了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();
关于ios - 一次调用两个NSURLSessionTask完成块吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39645458/