iOS 11 app lifecycle methods apparently called out of order?

In iOS 11 I am seeing didRegisterForRemoteNotificationsWithDeviceToken called before didFinishLaunchingWithOptions. It is as if they are being called concurrently and I somtimes get one before the other. Is didFinishLaunchingWithOptions guaranteed to be called before other lifecycle methods or should I need to guard for this?

Accepted Reply

Thanks. After digging into it more I found that registration was being triggered by the intiialization of a property on our AppDelegate class and I guess it was a race condition such that if the registration happened fast enough the callback (which was on the main thread) could happen before application didFinishLauchingWithOptions was invoked.

Replies

It is as if they are being called concurrently and I somtimes get one before the other.

You should be able to tell whether they’re called concurrently by adding an

NSLog
to each one. The
NSLog
output contains the thread ID. I expect that the thread IDs will be the same and match the thread ID of the main thread (that is, both of these delegate callbacks are called on the main thread). If you see anything different that would be a serious cause for concern.

Coming back to your bigger issue, most folks call

-registerForRemoteNotifications
in their
-application:didFinishLaunchingWithOptions:
method, so the latter is always called after the former. Where you calling
-registerForRemoteNotifications
from?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks. After digging into it more I found that registration was being triggered by the intiialization of a property on our AppDelegate class and I guess it was a race condition such that if the registration happened fast enough the callback (which was on the main thread) could happen before application didFinishLauchingWithOptions was invoked.