1

I made alert informing user about saving action, i add it to view, save some image and dismiss alert. However it's not working in the way i hoped it would. Looking at code below ofc firstly in console i get "saved.." then "dispath". I would like to get opposite effect firstly get "dispath" then "saved.." (so write alert on screen, then save in background and finally dismiss alert). But i change image of imageView1, so i cant move merging out of dispath_async because its an UI action.. how to do it then..? I need to firstly merge images and after it to save them and all of this calculating time to keep alert in front.

//adding alert to view
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), 
^{
    dispatch_async(dispatch_get_main_queue(), ^{
        //i want this to complete->
        imageView1.image = [self merge:imageView1.image and:imageView2.image];
        NSLog(@"dispatch");
    });

    //and then to do this action->
    UIImageWriteToSavedPhotosAlbum(imageView1.image, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);
    NSLog(@"saved..");

    dispatch_async(dispatch_get_main_queue(), ^{
        [alert dismissWithClickedButtonIndex:0 animated:YES];
    });
});

1 Answer 1

6

You should simply use dispatch_sync instead of dispatch_async. That function will not return until the block has executed on the main thread.

Sign up to request clarification or add additional context in comments.

3 Comments

I forgot to add that i have actionSheet where i click the button to call above method. When i use dispatch_sync the actionSheet isn't hiding so user can see my alert (that appears under actionSheet menu in this case).
Sorry, I should be more clear. You should still use dispatch_async(dispatch_get_global_queue... but dispatch_sync(dispatch_get_main_queue... inside it.
Thats a great option! Thanks ^^

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.