2016-02-29 41 views
1

我正在使用iOS應用程序,並且正在使用AFnetworking與服務器API進行交互。 我的問題是我想發送電話,並不想限制用戶直到從服務器獲取響應,所以問題是崩潰。當用戶移回特定屏幕時,可以說我有列表屏幕,我正在獲取數據,這需要6-7秒,同時用戶移回到上一個屏幕,並且數據來自API並將該刪除回調到列出屏幕,但用戶移動到該屏幕然後應用程序崩潰 下面是獲取數據調用的代碼。如何停止在目標c中運行隊列中發送的呼叫

+ (void) getRequestForDocumentListing:(NSDictionary *)headerParams urlQuery: (NSString*)action parameters:(NSDictionary*)params 
         onComplete:(void (^)(id json, id code))successBlock 
          onError:(void (^)(id error, id code))errorBlock 
{ 
    NSString *authorizationValue = [self setAuthorizationValue:action]; 
    NSString *selectedLanguage = [ApplicationBaseViewController getDataFromDefaults:@"GLOBALLOCALE"]; 
    NSString *language = selectedLanguage; 

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 

//set headers values 
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
[manager.requestSerializer setValue:language forHTTPHeaderField:@"Accept-Language"]; 
[manager.requestSerializer setValue:authorizationValue forHTTPHeaderField:@"authorization"]; 
[manager.requestSerializer setValue:@"x-folder" forHTTPHeaderField:@"inbox"]; 
[manager GET:action parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { 
    NSLog(@"document listing success"); 

    NSInteger statusCode = [operation.response statusCode]; 
    NSNumber *statusObject = [NSNumber numberWithInteger:statusCode]; 

    successBlock(responseObject, statusObject); 
} 
    failure:^(AFHTTPRequestOperation *operation, NSError *error) 
{ 

    NSInteger statusCode = [operation.response statusCode]; 
    NSNumber *statusObject = [NSNumber numberWithInteger:statusCode]; 

    id responseObject = operation.responseData; 
    id json = nil; 
    id errorMessage = nil; 

    if (responseObject) { 

     json = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:&error]; 
     errorMessage = [(NSDictionary*)json objectForKey:@"Message"]; 
    }else{ 
     json = [error.userInfo objectForKey:NSLocalizedDescriptionKey]; 
     errorMessage = json; 
    } 

    errorBlock(errorMessage, statusObject); 

}]; 
} 

我需要的是在ViewdidDisappear查看委託停止呼叫

- (AFHTTPRequestOperation *)GET:(NSString *)URLString 
       parameters:(id)parameters 
        success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success 
        failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure 
{ 
    AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithHTTPMethod:@"GET" URLString:URLString parameters:parameters success:success failure:failure]; 
    [self.operationQueue addOperation:operation]; 

    return operation; 
} 

所以需要建議如何解決這一具體問題。需要幫助。 謝謝

+0

什麼是崩潰? – Paulw11

+0

將所有操作添加到數組中,並在ViewdidDisappear中的循環徹底數組後停止。 –

+0

@MOHAMMADISHAQ你認爲這是最好的辦法,也可能是其他操作需要而且重要。 – Aleem

回答

1

我明白了你的觀點,我認爲問題不在於AFNet工作或下載,而是關於如何組織你的視圖控制器。
總之,你需要確保數據和視圖的同步。
什麼導致你的崩潰是當用戶做某些操作時(例如,刪除,移動...),數據與視圖顯示不一樣。讓我們來回放一個例子:
一個包含12個對象並用表格視圖顯示的數組。
用戶調用web請求來更改數組。我們知道,它需要時間。
用戶離開並再次回來。在此視圖中,表視圖與舊數組一起顯示。
此時,Web請求返回。該數組被修改爲10個對象。但此時,回調不會導致表視圖加載新數據。
當用戶進行一些操作時,就像刪除表視圖中的第11個對象一樣。實際上,數組中沒有第11個對象。
所以崩潰了。

如何處理它是保持數據和視圖的同步。

1

首先由

AFHTTPRequestOperation *operation = [manager GET:action parameters:nil success:^...blah blah blah...]; 

去操作對象的引用,然後就可以設置完成塊零,當您移動從該屏幕上移開。

[operation setCompletionBlock:nil]; 

請注意,即使您離開屏幕,請求可能實際上成功執行。但是,您的應用現在不會崩潰。

0

感謝RuchiraRandana和childrenOurFuture爲您的答案,我從您的答案得到了幫助,最後我來解決方案,我不會取消操作並設置nil委託,因爲我的其他操作也在工作,這是觸發其他屏幕。

我創建單獨的類,登記入住公正BOOL,並設置YES默認值,並在那個特定的類和API類- (void)dealloc設置爲沒有在那裏我觸發該委託我補充道。

if ([SHAppSingleton sharedInstance].isDocListControllerPop == YES) { 
     [delegate documentListResponse:documentList andStatusCode:code]; 
    } 

我知道這可能不是完美的解決方案,但這解決了我的問題。 謝謝

+0

非常歡迎你@Aleem。 –