2016-09-22 53 views
0

我有以下設置使用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個或更多的電話,我只想要完成這個特定的區塊而不是全部。

+0

我不明白。你想一個接一個地打電話嗎?將完成塊傳遞給第一個調用第二個的完成塊。 – danh

+0

不,我先調用抓取配置文件,然後幾乎馬上就要抓取通知。現在我認爲我在獲取配置文件完成之前調用了獲取通知,因此當獲取配置文件完成時,它會調用兩個完成塊。 – ORStudios

+0

是的,如果你沒有在調用#1的完成塊中嵌套調用#2,它們將或多或少地並行執行,並且將調用兩個完成。如果必須按順序調用它們,則必須在#1的塊中調用#2。 – danh

回答

1

我不認爲你理解異步以及完成塊。如果您按上述定義進行2個網絡呼叫,則可以按任意順序進行。 completionfetchBusinessProfilefetchNotifications將是不同完成塊... 除非你讓他們一樣。

例如:

[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(); 
+0

謝謝你解釋這個問題。我想我只是試圖做2 AFNetworking調用,但我不需要一個完成之前,我打電話給另一個。 – ORStudios

+0

你已經在做的是使呼叫獨立。我認爲你會感到困惑,因爲在你的第一個代碼片段中,兩種方法中的名稱都是「completion」。如果你輸入'NSLog'打印diff語句,你會看到它們被獨立調用。請記住,它們可能會發生故障。如前所述,如果處理響應的一部分調用另一個常見塊或方法,則可能看起來像您所描述的是您的問題。 –

+0

好吧我已經做了一些測試,看起來1號呼叫是首先執行罰款,然後當我NSLOG調用2的結果它似乎是呼叫1的副本。出於興趣會有什麼關係「NSURLSessionTask」? – ORStudios

相關問題