Question

I have a table view, and when the user selects a row, i push them to a new ViewController. At first, I initialized all my view objects in the ViewDidLoad method (involving web service calls) but I saw that it made the transition from my tableview to my new viewcontroller very long.

Instead, I moved most of my UI initialization in the ViewDidAppear method, and I like that it sped up my transition from tableview to new viewcontroller.

However, I cannot press any buttons in my NavigationBar at the top of the screen (like the back button) until my ViewDidAppear method completes and the UI is loaded.

What's the solution for this? Is there another way for me to load my UI without it preventing the user from interacting with the buttons in my NavigationBar?

Thanks!!

Was it helpful?

Solution

Try to initialize your UI in the background by using the following method

[self performSelectorInBackground:@selector(initYourUI) withObject:yourObj];

You can call this in the ViewDidLoad

OTHER TIPS

you do too much on the main thread. off load your longer operations like IO or longer computations BUT take care to not mess with the UI in the background thread.

Only touch the UI on the main thread. (Note sometimes it might seem safe, but in the long run it always end up producing weird issues)

one easy way is to use GCD:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^{
    //insert web service requests / computations / IO here 

    dispatch_async(dispatch_get_main_queue(),^{
        //back to the main thread for UI Work
    });
});

You could use grand central dispatch to make your web service calls asynchronously, which will keep the UI on the main thread responsive.

//create new queue
dispatch_queue_t backgroundQueue = dispatch_queue_create("com.siteName.projectName.bgqueue", NULL);

//run requests in background on new queue
dispatch_async(backgroundQueue, ^{
    //insert web service requests here
});

Here's a more in-depth tutorial:

http://www.raywenderlich.com/4295/multithreading-and-grand-central-dispatch-on-ios-for-beginners-tutorial

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top