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 all 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.userDefaults setObject:appUserID forKey:RCAppUserDefaultsKey];
self.appUserIDHasBeenSet = YES;
}
}

- (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