Do not finish the main thread until you finish another task launched in the background

0

I have a method with a completion that collects a data in the background. How do I use this data in the main thread before it finishes its execution?

That is, the main thread launches a method in the background and must wait to collect a data of this method before finishing.

    
asked by Popularfan 03.05.2017 в 12:57
source

2 answers

2

iOS does not let the main thread stand still for more than a few seconds and it's still not good practice in terms of usability.

I recommend that you do things in the background both if you need to stop the thread or use completions to communicate with the main thread when the process ends in the background and it does something.

    
answered by 09.05.2017 в 16:47
1

Maybe something like this:

dispatch_queue_t mi_thread = dispatch_queue_create("mi_thread", NULL);
    dispatch_async(mi_thread, ^{

        //... recoge tu dato en segundo plano

        [self miMetodoConCompletion:^{
            dispatch_async(dispatch_get_main_queue(), ^{

                //...usa el dato en el hilo principal

            });
        }];
    });
    
answered by 11.05.2017 в 18:46