Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add/reload mapview annotations without blocking main thread

I can perfectly load my map view with annotations the first time. However, if i try to reload the map on a button click (after its already loaded), the user has to wait till the process completes. This problem has arisen because on reload, the new annotations dont appear until the mapview is moved significantly, that's when the viewForAnnotation fires. I've seen two other questions similar to mine with solutions involving 'performSelectorInBackground' & 'performSelectorOnMainThread'. The former didnt work for me :( & the latter i dont want to do (though it's the only option that works) as i want the user to be able to interact with the map while the annotations load without blocking the main thread. I'm aware that such animations are best done on the main thread, so the question(s) 1. Is there no other way to do it than having the user wait till the map reloads? 2. Suggestions on the best way to do it? Thanks in advance.

like image 944
batman Avatar asked Oct 08 '22 18:10

batman


1 Answers

You can use a dispatch queue block to achieve this here is the syntax
You can create your on private queue like this

dispatch_queue_t queue = dispatch_queue_create("com.MyApp.AppTask",NULL);
dispatch_queue_t main = dispatch_get_main_queue();
    dispatch_async(queue, 
    ^{
        //do the fetching of data here(Don't do any UI Updates)
        dispatch_async(main, 
        ^{
           // Do the UI Update here.
         });

     });

Apple has referred to this as recursive decomposition.
In this bit of code the computation is offloaded onto a background thread with dispatch_async() and
then dispatch_async() back into the main queue which will schedule our block to run with the updated data that we computed in the background thread.

like image 168
Krrish Avatar answered Oct 12 '22 01:10

Krrish