'Understanding uber startup screen reporter code

I am React Native engineer trying to understand/learn objective c.

I was going through Uber Startup screen reporter code, and have some difficulties understanding the code

Here is the code sample they have shared

Can someone explain me the flow of code?

What I have been doing (not sure if it is correct).

In AppDelegate.h, Added notification relay

@property (nonatomic, strong) UBApplicationStartupReasonReporterNotificationRelay *notificationRelay;

In AppDelegate.mm

I have added these delegate they shared..

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    [self.notificationRelay updateApplicationStateNotification:[NSNotification notificationWithName:UIApplicationDidBecomeActiveNotification object:nil]];
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    [self.notificationRelay updateApplicationStateNotification:[NSNotification notificationWithName:UIApplicationWillResignActiveNotification object:nil]];
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    [self.notificationRelay updateApplicationStateNotification:[NSNotification notificationWithName:UIApplicationWillTerminateNotification object:nil]];
}

then I created a new Cocoa Touch class and add this code there (inside shared instance), something like this.

- (instancetype)init
{
    if (self = [super init]) {
      
      // Determine whether the app crashed on a prior launch
      BOOL crashedOnPriorLaunch = false;

      // Initialize a storage mechanism implementing UBApplicationStartupReasonReporterPriorRunInfoProtocol
      id<UBApplicationStartupReasonReporterPriorRunInfoProtocol> runInfo = ...

      // Initialize the notification relay
      id<UBApplicationStartupReasonReporterNotificationRelayProtocol> = [[UBApplicationStartupReasonReporterNotificationRelay alloc] init]

      // Initialize the startup reason reporter
      UBApplicationStartupReasonReporter *startupReasonReporter = [[UBApplicationStartupReasonReporter alloc] initWithPreviousRunDidCrash:crashedOnPriorLaunch
              previousRunInfo:runInfo
              notificationRelay: notificationRelay
              debugging:[UBBuildType isDebugBuild]];

      // Profit
      UBStartupReason startupReason = startupReasonReporter.startupReason;

 
    }
    return self;
}

[Question:] I am not sure how is the value of UBStartupReason startupReason = startupReasonReporter.startupReason; going to update? For example we have applicationDidBecomeActive and applicationWillResignActive which are two separate event.



Solution 1:[1]

If you see the code in github link that is provided, the startupReasonReporter.startupReason is updated based on the previousRunInfo data whenever startupReasonReporter is instantiated with initWithPreviousRunDidCrash with the previousRunInfo (or runInfo in your case) when the app is run the next time.

You have to use below public method to get the runInfo instance which is

/**
 *  Returns the prior run information stored to disk at the given directory URL.
 *  @param directoryURL The directory to use to to store the startup reason data.
 *  @return the previous startup reason data if it was present on disk, or empty startup reason object.
 */
+ (nonnull instancetype)priorRunAtDirectoryURL:(nullable NSURL *)directoryURL;

The way you are initializing runInfo seems to be incorrect. You have to do something like below:

id<UBApplicationStartupReasonReporterPriorRunInfoProtocol> runInfo = [[UBApplicationStartupReasonReporterPriorRunInfo] alloc] priorRunAtDirectoryURL:<yourDirectoryURL>];

These delegates applicationDidBecomeActive, applicationWillResignActive and applicationWillTerminate whenever called will just update the runInfo for the current run which will be store in a file in directory that you have provided in <yourDirectoryURL> in above call and the data will be retrieved from this file into startupReasonReporter.startupReason when the app is run next time.

Every time when you run the app the previous run data will be retrieved and startupReasonReporter.startupReason is set based on the data available in previousRunInfo

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nandish