2013-08-24 45 views
0

我使用AFNetworking lib中檢索JSON數據,並希望添加如下完成塊:完成排防止AFJSONRequestOperation執行

-(void)getData{ 

    NSString *[email protected]"http://localhost:8080/address/address/address/"; 

    NSURL *url= [ NSURL URLWithString:link]; 

    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { 
     arrayofA=[JSON valueForKeyPath:@"a"]; 
     arrayofB=[JSON valueForKeyPath:@"b"]; 


     [[self techTableView] reloadData]; 

    } failure:nil]; 


    [operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) { 
     //-- This block gets invoked periodically as the download progress 

     dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(totalBytesRead * NSEC_PER_SEC)); 
     dispatch_after(popTime, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void){ 

      dispatch_async(dispatch_get_main_queue(), ^{ 
       // HUD treatment 
      }); 
     }); 



    }]; 

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 
     NSLog(@"Completed:"); 

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     NSLog(@"Error: %@", error); 

    }]; 

    [operation start]; 

} 

當我將完成集團我不再有一個結果,沒有什麼是顯示在屏幕上,並注意到AFJSONRequestOperation集團沒有執行,我在那裏打印了一個日誌,並沒有顯示。 可能是什麼問題? 非常感謝。

+1

你爲什麼要設置2個不同的成功完成塊?據推測,一個正在運行,一個不是? – Wain

+0

@Wain:我想檢查數組數是否等於0。但'setDownloadProgressBlock'只是爲了進步,不是? – androniennn

+1

重複使用'setCompletionBlockWithSuccess' – Wain

回答

2

拋出這個代碼遠:

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 
    NSLog(@"Completed:"); 
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
    NSLog(@"Error: %@", error); 
}]; 

相反,把你登錄到塊的JSONRequestOperationWithRequest:success:failure:(你現在有一個成功的塊,但沒有不良區,併成功塊由代碼中刪除你應該刪除)。

setDownloadProgressBlock,只需推到主線程並更新您的用戶界面。不要試圖造成延遲,這隻會導致明顯的隨機行爲。另外,不要嘗試使用下載結果,只能在success塊中完成。

+0

我做錯了事,你確認我的錯誤。問題現在解決了。非常感謝你,你總是在這裏幫助:)。 – androniennn