Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LastContext *user* not loaded if previous session didn't set either *tags* or *extra* #198

Closed
3 of 9 tasks
mberube09 opened this issue Aug 17, 2017 · 2 comments
Closed
3 of 9 tasks

Comments

@mberube09
Copy link

mberube09 commented Aug 17, 2017

Platform:

  • iOS
  • tvOS
  • MacOS
  • watchOS

Swift:

  • Yes -> If yes, which version?
  • No

sentry-cocoa installed with:

  • CocoaPods
  • Carthage
  • Manually

Version of sentry-cocoa:
3.4.3


I have following issue:

While playing around the iOS sdk I came accross the issue where all my reports sent didn't include user information. After digging through, I discovered that the method restoreContextBeforeCrash didn't restore the context properly. In my case, I had only set the SentryClient.sharedClient.user property as I didn't need the tags and extra properties.

The reason is simple:

self.lastContext = [[NSDictionary alloc] initWithObjectsAndKeys:
                        [[NSUserDefaults standardUserDefaults] objectForKey:@"sentry.io.tags"], @"tags",
                        [[NSUserDefaults standardUserDefaults] objectForKey:@"sentry.io.extra"], @"extra",
                        [[NSUserDefaults standardUserDefaults] objectForKey:@"sentry.io.user"], @"user", nil];

The initWithObjectsAndKeys method is a nil terminated variable list. In my case [[NSUserDefaults standardUserDefaults] objectForKey:@"sentry.io.tags"] was actually nil, thus preventing from going further.

Now there would be two solutions that I would propose you. One is to force the creation of NSUserDefaults entry in your initializer by calling [self setExtra:[NSDictionary new]];. Just like you do in the clearContext method.

The other solution would be to avoid the nil terminated varargs and use a mutable dictionary instead :

    NSMutableDictionary *lastContext = [NSMutableDictionary dictionary];
    lastContext[@"tags"] = [[NSUserDefaults standardUserDefaults] objectForKey:@"sentry.io.tags"];
    lastContext[@"extra"] = [[NSUserDefaults standardUserDefaults] objectForKey:@"sentry.io.extra"];
    lastContext[@"user"] = [[NSUserDefaults standardUserDefaults] objectForKey:@"sentry.io.user"];
    self.lastContext = lastContext.copy;

Hope I didn't miss any details, feel free to ask otherwise, thanks!

@HazAT
Copy link
Member

HazAT commented Aug 22, 2017

👏

This bug report is superb, thank you for pointing that out.
I've created a PR to fix that.

@mberube09
Copy link
Author

Thank you for the fix 🤗

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants