0

i have a UITableView which shows (doctor image , doctor Name)

In the cellForRowAtIndexPath function iam fetching the images as follow:

 [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];      
                                }];

The Problem is: when i'm scrolling down fast , the images start loading , and almost 100 images being loaded , then when i click on row to show the doctor details , i can't get the data from the server until all the images finish loading (until all async requests finish).

i'm using the following code to get the doctor details:

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"];
});
}];

Note: SoapCall is a custom class i made to make an Async request to the server

my question is : Is there is any way to cancel loading the images???

Or can i load the Doctor details in different thread ???

  • Have you tried [[NSOperationQueue mainQueue] cancelAllOperations] ? – rocky Sep 28 '14 at 20:11
  • Yes i did , but it didn't work once the [NSURLConnection sendAsynchronousRequest: queue: completionHandler:] is called , i can't stop it – user967555 Sep 29 '14 at 05:29

1 Answers1

0

i found a great project which is "SDWebImage"

https://github.com/rs/SDWebImage

it's using NSoperationQueue and NSOperation in a very advanced way This project saved a lot of time for me