0

我有一個UITableView其示出了(醫生圖像,醫生姓名)如何在IOS執行多AsynchronousRequests

的cellForRowAtIndexPath功能IAM提取的圖像如下:

[NSURLConnection sendAsynchronousRequest:request 
              queue:[NSOperationQueue mainQueue] 
           completionHandler:^(NSURLResponse * response, 
                NSData * data, 
                NSError * error) { 
            if (!error){ 

              dispatch_async(dispatch_get_main_queue(), ^{ 
             updateCell.DRImage.image = [[UIImage alloc] initWithData:data];  
           }]; 

存在的問題是:當我快速滾動時,圖像開始加載,並且加載了近100幅圖像,然後當我點擊行顯示醫生詳細信息時,我無法從服務器獲取數據,直到所有圖像完成加載(直到所有的異步請求完成)。

我使用下面的代碼,以獲得醫生的詳細信息:

SoapCall *Obj = [[SoapCall alloc] init]; 
NSString* Url=[[NSString alloc] initWithFormat:@"%@/doctorsDetails.php?i=%@" , ServerUrl ,DoctorDataFromTableView.Doctor_ID]; 

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul); 
dispatch_async(queue, ^{ 

[Obj CallUrl:Url OnCompletion:^(NSMutableArray * Results , NSError * Error){ 

    dispatch_async(dispatch_get_main_queue(), ^{ 

     Nviews.text =[[Results objectAtIndex:0] objectForKey:@"numViews"]; 

     NComments.text =[[Results objectAtIndex:0] objectForKey:@"totComments"]; 
}); 
}]; 

注:的SOAPCall是一個自定義類我取得了令一個異步請求到服務器

我的問題是: 是否有任何方法取消加載圖像?

或者我可以加載醫生的詳細信息在不同的線程?

+0

你試過[ [NSOperationQueue mainQueue] cancelAllOperations]? – rocky 2014-09-28 20:11:20

+0

是的,我做了,但它沒有工作 一旦[NSURLConnection sendAsynchronousRequest:queue:completionHandler:]被調用,我無法阻止它 – user967555 2014-09-29 05:29:12

回答

0

我發現了一個很大的項目,該項目是「SDWebImage」

https://github.com/rs/SDWebImage

它使用NSoperationQueueNSOperation在一個非常先進的方式 該項目節省了大量的時間,我