我有以下設置使用AFNetworking撥打我的服務器。我使用了一個我在互聯網上找到的例子來包含一個完成模塊,以便知道何時完成了呼叫。一個接一個地調用兩個NSURLSessionTask完成塊?
文件 「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個或更多的電話,我只想要完成這個特定的區塊而不是全部。
我不明白。你想一個接一個地打電話嗎?將完成塊傳遞給第一個調用第二個的完成塊。 – danh
不,我先調用抓取配置文件,然後幾乎馬上就要抓取通知。現在我認爲我在獲取配置文件完成之前調用了獲取通知,因此當獲取配置文件完成時,它會調用兩個完成塊。 – ORStudios
是的,如果你沒有在調用#1的完成塊中嵌套調用#2,它們將或多或少地並行執行,並且將調用兩個完成。如果必須按順序調用它們,則必須在#1的塊中調用#2。 – danh