问题
My app has a rootViewController setup in a storyboard. I originally asked this question, but I can see that it is the wrong question to ask now:
Is the
viewDidLoad
method of the rootViewController always called beforeapplication:didBecomeActive
in the app delegate? Does thatviewDidLoad
method always finish beforeapplication:didBecomeActive
is called, or can the two run concurrently?I can run tests, but I know that these tests don't take into account every situation. I want to understand if there are hard and fast rules that apply here, or if I might possibly be dealing with race conditions at some point. From what I can tell,
viewDidLoad
of the rootViewController seems to be called and finished beforeapplication:didbecomeActive
is called, but I can't figure out if this is something I can rely on.I am using iOS 11 and Xcode 9.
My rootViewController serves to show my appUpdating view, passcode view, my legal terms view, my handleImportedFile view, and my tabbarcontroller. It is like a launch coordinator of sorts, because my loading process is extremely complicated. Now I am moving the location of the user's sqlite database, and some of my user's databases are huge. It has to be done in the background, but while a hud is running on the main queue, because the app cannot show data until this is done. If I do this during application:didFinishLaunchingWithOptions
, some users are getting the watchdog timer. So I want to move the loading process to application:didBecomeActive
(I have a flag to tell me if app is launching from terminated). When it is done running, the rootViewController performs a segue to the pertinent view. But I think rootViewController needs to have loaded by that time, and I am not sure that that is always the case.
Basically, in the code below, I am trying to figure out if [self.masterController showApplicableView]
, which causes the rootViewController (which I call masterController) to perform a segue, is called in a 'safe place', or if, in any case, it could possibly be too early to have the rootViewController perform a segue at this point.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.masterController = (MasterViewController*)self.window.rootViewController;
self.activatingFromTerminated = YES;
return YES;
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
self.activatingFromTerminated = NO;
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
if (_activatingFromTerminated) {
self.activatingFromTerminated = NO;
[[CoreDataController sharedManager] loadStoreWithCompletionHandler:^(NSError * _Nullable error) {
if ([CoreDataController sharedManager].storeLoaded) {
[self.masterController showApplicableView];//performs applicable segue
}
}];
}
}
回答1:
From what I can tell, viewDidLoad of the rootViewController seems to be called and finished before application:didbecomeActive is called, but I can't figure out if this is something I can rely on.
It isn’t and you shouldn’t. You shouldn’t be concerned about this at all. The fact that you think you need to know this is a Bad Smell in your code. These are lifetime events from two completely different areas (the app and a view controller) and their relative timing should be no concern of yours. Do in each what is appropriate to the meaning of that event.
来源:https://stackoverflow.com/questions/51884528/is-rootviewcontroller-always-ready-to-present-a-segue-by-the-time-applicationdi