runloops and application:didFinishLaunch message order - iphone

My understanding of Runloops is basic so this may seem like a very trite question.
I have the following in my application:didFinishLaunchingWithOptions (or applicationDidFinishLaunching):
{
// 1. typical app setup work: create various views, create a tab bar, add
// navigation controller and views to the tab bar
// 2. perform some other app initialization tasks
// 3. Add main view to the window
[window addSubview:tabbarController.view];
// 4. Make window visible
[window makeKeyAndVisible];
// 5. Perform one final behind the scene task
[myTaskObject doSomeTaskHere];
}
Do each of these methods get executed in order listed or is there any chance that step #5 can happen before the app's main runloop completes the work of putting up the main window with '[window makeKeyAndVisible]'
Does the doSomeTaskHere need to get wrapped up into a performSelectorOnMainThread:withObject:waitUntilDone:YES to ensure that the runloop completes the displaying of the window and thus loading whatever view that is the topmost view before 'doSomeTaskHere' is invoked?

Those tasks will execute in order on the main thread's run loop. Since UI updates also occur on the main thread you will not allow your app to redraw the screen until after you return from -application:didFinishLaunchingWithOptions: so while [window makeKeyAndVisible]; will complete before [myTaskObject doSomeTaskHere]; you are still blocking the UI from updating until that doSomeTaskHere is complete.
If doSomeTaskHere is an expensive operation you should schedule it for a future iteration of the run loop or better yet do that work on a different thread so that the UI can update and respond to touches.
performSelectorOnMainThread:withObject:waitUntilDone:YES would not allow the main thread to update the UI as unless you passed NO as the last parameter. Telling the main thread to wait until the main thread finishes some other work isn't very useful or any different than invoking that selector directly.

Related

Understanding setNeedsDisplay/drawRect with Blocks

I'm trying to understand how things work in regards to concurrent programming and calling setNeedsDisplay. I basically have Three objects.
Main View - container with different UIView objects, the main one being a UIScrollView
Small Map View - a small UIView that draws a miniature version of one of the other UIView items on screem
Processor - a delegate of the Main View that calculates what's on screen and calls the Main View back with what's in view.
So a simple use case of what's going on is the user touches the ScrollView and then the processor updates what's in view of the scrollView (like calculating coordinates, center point, etc) It does this using blocks and does it asynchronously. This then posts a notification to the MainView object.
When the MainView receives the notification, it just calls
[smallMap setNeedsDisplay]; // example 1
I put some logs around this call, and I do see it gets called right away. However, the drawRect: of this function does not get called right away. It gets called after 2 seconds or so.
I remember reading that setNeedsDisplay just marks the view for redraw to happen on the next event of the run loop.
But if I add this code instead:
// example 2
dispatch_async(dispatch_get_main_queue(), ^{
[smallMap setNeedsDisplay];
});
My view gets redrawn right away.
I guess I'm confused as to why I have to ask for the main event loop to call setNeedsDisplay to immediately redraw something. Like in example 1, by me calling setNeedsDisplay, is that done in the background or something and that's why it doesn't get redrawn right away? I'm trying to understand the difference in what's going on behind the scenes so I know what to look for the in future. Like should I have all my calls that need to be immediately redrawn in something similar to the example 2 block? Or is it because I'm processing my data asynchronously that I need to then ask for the main queue? Thanks!
My guess is 1 of 2 things:
Your code that is running on a separate thread is calling your MainView methods from the separate thread instead of using performSelectorOnMainThread or a GCD call that invokes the code on the main thread. Thus your call to setNeedsDisplay is actually taking place on a background thread, which is a no-no, as the other poster said.
The second possibility is that your MainView code is running on the main thread, but it gets busy doing time-consuming processing, or waiting for a synchronous call to another thread to finish, and doesn't service the event loop.
You can rule out the first possibility by setting a breakpoint on your call to setNeedsDisplay and looking at the call trace in the debugger to see what thread it's running from.
Figuring out the second possibility will take a little more work. You might need to delve into instruments.
setNeedsDisplay is a UIKIT API call and has to be called from the main thread of the application, also known as the UI thread. That's why calling it in a background thread doesn't have any immediate effect and scheduling it on the main queue has immediate effects.
See this related question https://stackoverflow.com/a/6988115/172690 for a more detailed answer.

secondary thread regarding

I have a main and an auxiliary thread in my app. Main thread as everyone knows is being used by UI. I use the secondary thread to do the background loading of my views.
I have a main controller which i call a dummy controller. From there i call my main controller, which is in landscape mode.
So basically what i have to do is, when i rotate my dummy controller, i want to be able to load my main landscape controller and vice versa.
Everything works fine when i rotate from dummy to main.
The main has a series of child controllers to load, so i have put up that process on a secondary thread, and by that time they are loading i show a loader screen.
The dummy is a normal view controller, and the main is a modal view controller.
Now the problem.
if a user is trying to rotate form dummy --> main, the rotate method kicks of the initialisation of the main view controller. But for some reason the user changes his mind and rotates back form main-->dummy ideally, the transition should be smooth as the UI thread(main theread) is free. What happens is the UI rotates back. But since the secondary thread on the Main controller is still on, even after the dismiss modal controller is called on the MainController by the main thread, when we call the willRotate function.
I've read in various post that we cannot simply kill the secondary thread, as we have to do clean up stuff ourselves. So can anyone please guide me through code, how do i kill my secondary thread and do clean up before i transit back to my calling form..??
Its kinda urgent... please dont point to other thread topics, cos i've already visited those.
I specifically want to know how should i stop my secondary thread, without causing a crash.
thanks.
-(void)viewDidLoad {
[super viewDidLoad];
workingThread = [[NSThread alloc]initWithTarget:self selector:#selector(parseMainTextFile) object:nil];
[workingThread start];
[NSThread detachNewThreadSelector:#selector(addLoader) toTarget:self withObject:nil];
//[self performSelectorOnMainThread:#selector(parseMainMatchTextFile) withObject:nil waitUntilDone:NO];
}
Generally saying, if you run a background thread it means you've got a Run Loop, which works in a while statement. You should have a flag which stops the Run Loop. this way it would transform the state of the thread to finished.
while (mRunLoop && [mLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]);
This is the way I do. Additionally read this Post and its sub posts from comments. Good luck!

UIViewController freezed during update

my main UIViewController should check for some data updates when it loads, if any update is found an opaque subview is added to show progress and so on.
Let's say that the method is called checkUpdates, so in my app delegate i do the following:
[window addSubview:viewController.view];
[window makeKeyAndVisible];
[viewController checkUpdates];
The method is like
- (void) checkUpdates {
// add the opaque progress view
[self.view addSubview:progress.view];
[progress setMessage: #"Checking for updates ..."];
// ... perform the update ...
// remote the progress view
[progress.view removeFromSuperview];
}
Theoretically the view should load and the update process should be seen, but the problem is that the view just get freezed during this phase, and when the update process is finished i'm able to interact with it.
Any idea?
Since the UI runs on the main thread of the app, if you perform "work" tasks on the main thread, you will also block the UI, which will therefore look frozen.
You might want to consider using NSOperationQueue or something equivalent to spin off another thread to do your work so the UI can continue to process updates independently.

viewDidAppear, but where is viewDidShowToUser?

I have an iphone3g with this function running in my ViewController
- (void)viewDidAppear:(BOOL)animated {
[super viewDidLoad];
}
I use a TabBar iphone app. But when I click from tab 1 to tab 2 and debug the secondView Controller it is stopped before the view is actually in the users view.
So there for when you click tab 2 until every function inside - (void)viewDidAppear:(BOOL)animated is complete the user gets to see the view.
Where is the function ViewDidShowToUser? Now I have a few functions running so it's sometimes slow and you're thinking the button is not working really..
First of all, you're calling [super viewDidLoad] instead of [super viewDidAppear:animated] inside your implementation of -viewDidAppear:
Secondly, using the debugger and breakpoints gives an artificial view of how your app behaves. In real world usage, users aren't going to notice that the -viewDidAppear: method returns before actually showing the view.
The real problem is your work that takes too long to complete and makes the app appear sluggish. You should consider performing the work asynchronously, and you have a couple of options to do that.
In your viewDidAppear: implementation you could use performSelector:withObject:afterDelay: to queue up the work. This method will return immediately and schedule your selector to be called in whatever time period you specify. If you pass 0 as the delay, it'll be queued up to run on the next iteration of the run loop, effectively giving you a way to return from the method and keep the user interface responsive.
You could use blocks, if you're not targeting anything below iOS4, and harness the power of Grand Central Dispatch to thread out your work nice and safely.
you are calling super on viewDidLoad: inside of viewDidAppear: ....change the line [super viewDidLoad]; to [super viewDidAppear:animated];
There's no easy way to tell.
UIKit makes UIViews. UIViews draw to CALayers. CALayers are handled by CoreAnimation.
CoreAnimation decides when to ask UIView to draw the layers. It draws all the layers on screen and then composites them on the GPU. Only after they're composited does the screen display the updated UI. This decoupling happens in order to allow CoreAnimation to do the majority of the work independently of the UI thread, but it means that you can't easily tell what's "actually on screen".
There is no easy way to tell when the screen has actually displayed something (apart from the now-private UIGetScreenImage()). viewDidAppear: gets called after UIKit finishes constructing (and animating) the views/layers. At that point, they will be "seen" by CoreAnimation after the next run loop, and displayed shortly thereafter. If you do lots of processing in viewDidAppear:, then CoreAnimation will never see the updated "model tree".

How do I create a reusable Loading Screen?

How do I create a loading screen that can be reused at any given time. I'm aware of the Default.png but I need the flexibility to plug in a loading screen at any point during the application life cycle.
This is what I have thus far.
//inside a method that gets called by a UIButton
LoadingViewController* loadController = [[LoadingViewController alloc] initWithNibName:#"Loading" bundle:nil vertical:NO];
[self.view addSubview: loadController.view];
//some method call that takes a few seconds to execute
[self doSomething];
//This loads some other view, my final view
[self.view addSubview: someOtherView]
but it seems that the loading view is never displayed. Instead the previous view stays there until the "someOtherView" gets added. I put trace logs and the code does seem to get executed, I even replaced [self doSomething] with a sleep(2), but the intermediate loading view is never displayed.
If I remove [self.view addSubview:someOtherView]; then after a few seconds...(after doSomething finishes executing) the load view is displayed since there is no view that is pushed on top of it, however this is obviously not the functionality I want.
Can explain this behavior? Is there something about the rendering cycle that I am misunderstanding because it doesn't seem like the view (on the screen at least) is instantly updated, even though I call a [self.view addSubview: loadController.view];
Would I need to create a separate thread?
In general, for changes in the UI to be made visible to the user, control must return to the main runLoop. You are only returning to the runLoop after taking the loading view down and replacing it with the other view. One strategy for dealing with this is to move the code that does the loading onto another thread. NSOperation and NSOperationQueue can be used for this.
An even simpler approach is to use performSelectorInBackground:withObject to do the processing. Once processing is complete the UI can be updated again to show the data. It is important to remember that the UI updates must be carried out on the main thread. Use performSelectorOnMainThread:withObject:waitUntilDone: to accomplish this from the loading thread.
This sounds like a lot of complication but it is really as simple as breaking your single method up into three separate methods as follows:
Display the loading view and start the background process - this is the button action method.
Do the background loading - called from the button action function with performSelectorInBackground:withObject.
Remove the loading view and update the display with the data - called from the background thread with performSelectorOnMainThread:withObject:waitUntilDone.
I created a subclass of UIView where I initialized how my loading-view should work and look like. (My view appeared and slided in from the bottom with an nice animation).
I then added code that handled whether the loading-view should be visible or not in a subclass of UIViewController.
I then let all my viewcontrollers be an subclass of my new viewcontrollerclass which made it possible for me to do:
[self showloadingMessage:#"loading..."];
in all my viewcontrollers...