1

I am just a beginner in iOS so please ignore my stupidity but kindly clear my doubt. I am showing a parsed data in a UITableView. In that i am downloading a give image using the URL stored in my NSDictionary.

I need a good explanation whether I should use

dispatch_sync(,{
                dispact_async (,{}));

or

dispatch_async(,{
                    dispact_async (,{}));

This my code for populating UITableview data.

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    CustomClassCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell" forIndexPath:indexPath ];

    BookMyShow *bookMyShow = [_jsonArray objectAtIndex:indexPath.row];

    cell.eventCode.text = bookMyShow.eventCode;
    cell.eventName.text = bookMyShow.eventName;





    NSURL *url = [NSURL URLWithString:bookMyShow.imageString];

    //NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //[request setHTTPMethod:@"GET"];

    dispatch_queue_t loadQ = dispatch_queue_create("DownloadQueue", NULL);
    dispatch_sync(loadQ,
                  ^{
                      NSData *data = [NSData dataWithContentsOfURL:url];
                      dispatch_async(dispatch_get_main_queue(),
                                     ^{
                                         cell.myImageView.image = [UIImage imageWithData:data];
                                     });


                  });


    // [cell.imageView setImageWithURL:[NSURL URLWithString:[[array objectAtIndex:indexPath.row] objectForKey:@"image"]] placeholderImage:nil];

    //cell.imageView.image = [UIImage imageNamed:bookMyShow.imageString];

    return cell;
}
Sharad Chauhan
  • 4,821
  • 2
  • 25
  • 50
  • It may be irrelevant to this question, try to abstract your downloading part from cellForRowAtIndexPath for cleaner code. Coming to your question See this link http://stackoverflow.com/questions/19822700/difference-between-dispatch-async-and-dispatch-sync-on-serial-queue – Feroz May 10 '16 at 11:32

6 Answers6

2

The only difference is that dispatch_sync only return after the block is finished whereas dispatch_async return after it is added to the queue and may not finished.

following code:

dispatch_async(_serialQueue, ^{ printf("1"); });
printf("2");
dispatch_async(_serialQueue, ^{ printf("3"); });
printf("4");

Result is :
It may print 2413 or 2143 or 1234 but 1 always before 3

For This code:

 dispatch_sync(_serialQueue, ^{ printf("1"); });
 printf("2");
 dispatch_sync(_serialQueue, ^{ printf("3"); });
 printf("4");

Result is : 1234

What may happened is

  1. Thread 1: dispatch_async a time consuming task (task 1) to serial queue
  2. Thread 2: start executing task 1
  3. Thread 1: dispatch_async another task (task 2) to serial queue
  4. Thread 2: task 1 finished. start executing task 2
  5. Thread 2: task 2 finished.

and you always see 12

Vijay Kachhadiya
  • 366
  • 2
  • 11
  • thanks for such a nice explanations. It cleared my other doubts too. But I also need to know which among the options I provided is a better way to achieve efficiency for downloading images in such scenario. – Sharad Chauhan May 10 '16 at 10:31
  • for Downloading you can use ASIHTTPRequest. Check this link : https://allseeing-i.com/asihttprequest/how-to-use. It may be help full to you. Read it carefully – Vijay Kachhadiya May 10 '16 at 10:34
2

Here is the great example shows good way to fill TableView. It written in Swift, uses GCD, NSOperation and NSOperationQueue.

Hantok
  • 67
  • 7
1

The only difference is that dispatch_sync only return after the block is finished whereas dispatch_async return after it is added to the queue and may not finished.

dispatch_sync Wait until your block is not completed and dispatch_async add the task in queue.

use dispatch_async because it not stuck your ui if you run log task .

balkaran singh
  • 2,754
  • 1
  • 17
  • 32
1

try to use dispatch_async using json bcs when thousand of record is come to the tableview than you can easily scroll bcs dispatch_async is flaxible.

dispatch_sync is when thousand of record is come than tableview is hang or stop in your app.

Jigar
  • 1,801
  • 1
  • 14
  • 29
1

What you do is absolutely wrong for multiple reasons.

Cells are reused. Did you notice the call to dequeueReusableCellWithIdentifier? By the time you try to store the image, it is quite likely that the cell is reused to display something totally different.

dispatch_sync waits until the block is executed. You are creating a queue and then dispatch a single task on it, so that is absolutely pointless. Just dispatch on the background queue. When your block executes, it first does a synchronous download. Your code will wait for that download to finish. If you have problems with your internet connection, it can take 60 seconds until the call fails. (Yes, it can fail). So your app will hang for 60 seconds.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
1

You should also look at the caching point since every time you scroll, image would be downloaded which would not be pocket friendly for a user. You must cache whatever you have downloaded.

Piyush Vaish
  • 107
  • 8