I have an application with implemented local server which handles request from web front-end. UIWebView presents some GUI, user do some interaction, I handle his requests and send responses back to webview.
Sometimes I'm receiving some request which require to open second webview (e.g for facebook login) and wait in current method for results to return from that second webview.
When I'm running such case on iDevice with dual core processor it works as expected. But when I'm running it on single core iPhone 4, webview processing is blocked until i leave the current method (white page with waiting indcator).
I solved this by putting sleep for current thread so the main thread will have time to process events in his run loop (if I properly understand this)
- (void)requestProcessingMethod { // <-- on background thread
someCalculations ...
dispatch_sync(dispatch_get_main_queue(), ^{
[self displayFacebookLoginWebView];
});
while(!facebookReturnCondition){
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate distantFuture]];
[NSThread sleepForTimeInterval:0.5]; // <-- on single core without this
// facebook webview will not load the login page
}
return response; //
}
I'm not happy with that. Setting thread for sleep looks like very poor solution.
Is there any way to add working UIWebView from background thread without exiting current method (background thread)?
Or is it possible to manually switch/force run loop execution?
Now I am not sure if this will work, can't test at the moment. But:
You could try having the operations as separate operations in a queue, using Operation Dependencies.
in your case when you use this method:
From reading the Concurrency Guide, it appears you can have operation dependencies over different queues. Which means that you can ensure the UI runs in the main thread, web later running in an async thread, keeping the Main Thread clear and responsive on all devices.
Apple Concurrency Guide