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

Fix/race condition after uninstalling app #383

Merged
merged 4 commits into from
Nov 10, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions Purchases/Caching/RCDeviceCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ @interface RCDeviceCache ()
@property (nonatomic) NSNotificationCenter *notificationCenter;
@property (nonatomic, nonnull) RCInMemoryCachedObject<RCOfferings *> *offeringsCachedObject;

@property (nonatomic, assign) BOOL appUserIDHasBeenSet;

@end


Expand Down Expand Up @@ -55,6 +57,7 @@ - (instancetype)initWith:(NSUserDefaults *)userDefaults
userDefaults = NSUserDefaults.standardUserDefaults;
}
self.userDefaults = userDefaults;
self.appUserIDHasBeenSet = self.cachedAppUserID != nil;
[self observeAppUserIDChanges];
}

Expand All @@ -71,7 +74,7 @@ - (void)observeAppUserIDChanges {
}

- (void)handleUserDefaultsChanged:(NSNotification *)notification {
if (notification.object == self.userDefaults) {
if (self.appUserIDHasBeenSet && notification.object == self.userDefaults) {
if (!self.cachedAppUserID) {
NSAssert(false, @"[Purchases] - Cached appUserID has been deleted from user defaults. "
"This leaves the SDK in an undetermined state. Please make sure that RevenueCat "
Expand All @@ -96,7 +99,10 @@ - (nullable NSString *)cachedAppUserID {
}

- (void)cacheAppUserID:(NSString *)appUserID {
[self.userDefaults setObject:appUserID forKey:RCAppUserDefaultsKey];
@synchronized (self) {
self.appUserIDHasBeenSet = YES;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to move this right after the set in the userDefaults? Just in case it fails when calling setObject

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea! In practice, I don't think we're actually catching the error anywhere so it'd crash, but if we did add a try / catch this would lead to an inconsistent state.
Updated.

[self.userDefaults setObject:appUserID forKey:RCAppUserDefaultsKey];
}
}

- (void)clearCachesForAppUserID:(NSString *)oldAppUserID andSaveNewUserID:(NSString *)newUserID {
Expand Down
14 changes: 13 additions & 1 deletion PurchasesTests/Caching/DeviceCacheTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,11 @@ class DeviceCacheTests: XCTestCase {

func testCrashesWhenAppUserIDIsDeleted() {
let mockNotificationCenter = MockNotificationCenter()
mockUserDefaults.mockValues["com.revenuecat.userdefaults.appUserID.new"] = "Rage Against the Machine"

self.deviceCache = RCDeviceCache(mockUserDefaults,
offeringsCachedObject: nil,
notificationCenter: mockNotificationCenter)
mockUserDefaults.mockValues["com.revenuecat.userdefaults.appUserID.new"] = "Rage Against the Machine"

expect { mockNotificationCenter.fireNotifications() }.notTo(raiseException())

Expand All @@ -213,6 +214,17 @@ class DeviceCacheTests: XCTestCase {
expect { mockNotificationCenter.fireNotifications() }.to(raiseException())
}

func testDoesntCrashIfOtherSettingIsDeletedAndAppUserIDHadntBeenSet() {
let mockNotificationCenter = MockNotificationCenter()
mockUserDefaults.mockValues["com.revenuecat.userdefaults.appUserID.new"] = nil

self.deviceCache = RCDeviceCache(mockUserDefaults,
offeringsCachedObject: nil,
notificationCenter: mockNotificationCenter)

expect { mockNotificationCenter.fireNotifications() }.notTo(raiseException())
}

func testNewDeviceCacheInstanceWithExistingValidPurchaserInfoCacheIsntStale() {
let mockNotificationCenter = MockNotificationCenter()
let appUserID = "myUser"
Expand Down