Jagveer Singh
Jagveer Singh

Reputation: 584

objective c how to execute for loop in background queue and update view on each cycle of execution of for loop?

I am making a chat messenger . I am selecting multiple images from the photo gallery and then sending them in a chat . I have run a for loop where each image is picked and send . This is working fine but UI updates when loop completes execution.

My Code is-

for (UIImage *cImage in selectedImages) {        
    NSBubbleData *rBubble = [NSBubbleData dataWithImage:cImage date:createdAt     type:BubbleTypeMine load:@"upload" key:key chatType:[chatWithUser objectForKey:@"type"]    sender:@"you"];
    rBubble.avatar = myPic;
    [bubbleData addObject:rBubble];
    [bubbleTable reloadData];
    [bubbleTable scrollRectToVisible:CGRectMake(0, bubbleTable.contentSize.height -    bubbleTable.bounds.size.height, bubbleTable.bounds.size.width, bubbleTable.bounds.size.height) animated:YES];
    [globalManager.imageLoadingStatus setValue:path forKey:key];
}

Upvotes: 0

Views: 641

Answers (2)

alishaik786
alishaik786

Reputation: 3726

dispatch_get_global_queue is one of the solution for your requirement. This below link gives the whole detail that how to update the UIView using GCD(Global Central Despatch)

Using Grand Central Dispatch for Asynchronous Table View Cells

This may not be the solution, but you have to use the concept like this for updating the view on each cycle.

Upvotes: 1

Martin Koles
Martin Koles

Reputation: 5247

Wrap the code into a block sending it to a background thread. Then each UI update is wrapped into another block, this time executed on the main thred.

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

    dispatch_async(dispatch_get_main_queue(), ^{
        // perform on main

    });
});

Upvotes: 1

Related Questions