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
[final] fix: subscriber attributes deleted when aliasing #222
[final] fix: subscriber attributes deleted when aliasing #222
Conversation
Two thoughts:
|
b6cee53
to
05aa509
Compare
…he ones for other users if they're completely synced
|
||
// MARK: Subscriber Attributes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved these to a new file
@vegaro this is ready for another pass |
@@ -104,6 +115,10 @@ - (void)setPurchaserInfoCacheTimestampToNow { | |||
self.purchaserInfoCachesLastUpdated = [NSDate date]; | |||
} | |||
|
|||
- (NSString *)purchaserInfoUserDefaultCacheKeyForAppUserID:(NSString *)appUserID { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this one and clearOfferingsCache
were just moved for organizational purposes
found a bug, moving back to WIP |
Purchases/Caching/RCDeviceCache.m
Outdated
[self.offeringsCachedObject clearCache]; | ||
} | ||
|
||
#pragma mark - subscriber attributes | ||
|
||
- (void)storeSubscriberAttribute:(RCSubscriberAttribute *)attribute appUserID:(NSString *)appUserID { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe put appUserID
parameter in next line for consistency with the next function
Purchases/Caching/RCDeviceCache.m
Outdated
subscriberAttributesForAppUserID[attribute.key] = attribute.asDictionary; | ||
groupedSubscriberAttributes[appUserID] = subscriberAttributesForAppUserID; | ||
[self.userDefaults setObject:groupedSubscriberAttributes | ||
forKey:RCSubscriberAttributesKey]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you could remove the logic in this fucntion and reuse the other one by doing something like:
forKey:RCSubscriberAttributesKey]; | |
[self.storeSubscriberAttributes:@{attribute.key: attribute} appUserID: appUserID |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good call
Purchases/Caching/RCDeviceCache.m
Outdated
? allSubscriberAttributesByKey.mutableCopy | ||
: [[NSMutableDictionary alloc] init]; | ||
NSMutableDictionary *groupedSubscriberAttributes = self.unsyncedAttributesForAllUsersAsDict.mutableCopy; | ||
NSMutableDictionary *subscriberAttributesForAppUserID = ((NSDictionary *)groupedSubscriberAttributes[appUserID] ?: @{}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can reuse subscriberAttributesForAppUserID
. Although it would call self. unsyncedAttributesForAllUsersAsDict
again
NSMutableDictionary *subscriberAttributesForAppUserID = ((NSDictionary *)groupedSubscriberAttributes[appUserID] ?: @{}) | |
NSMutableDictionary *subscriberAttributesForAppUserID = [self subscriberAttributesForAppUserID].mutableCopy; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I kinda wanted to avoid an extra read, if possible
#pragma MARK - Private methods | ||
|
||
- (void)setAttributeWithKey:(NSString *)key value:(nullable NSString *)value appUserID:(NSString *)appUserID { | ||
[self storeAttributeLocallyIfNeededWithKey:key value:value appUserID:appUserID]; | ||
} | ||
|
||
- (void)syncAttributesWithCompletion:(void (^)(NSError *_Nullable error))completion appUserID:(NSString *)appUserID { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extra line
} else { | ||
completion(nil); | ||
} | ||
} | ||
|
||
- (void)syncAttributesForAllUsersWithCurrentAppUserID:(NSString *)currentAppUserID { | ||
NSDictionary < NSString * , RCSubscriberAttributeDict > *unsyncedAttributesForAllUsers = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NSDictionary < NSString * , RCSubscriberAttributeDict > *unsyncedAttributesForAllUsers = | |
NSDictionary <NSString *, RCSubscriberAttributeDict> *unsyncedAttributesForAllUsers = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch, no clue what happened with formatting there
[self.deviceCache deleteAttributesIfSyncedForAppUserID:syncingAppUserID]; | ||
RCLog(@"Subscriber attributes synced successfully for appUserID: %@", syncingAppUserID); | ||
} else { | ||
RCErrorLog(@"error when syncing subscriber attributes. Details: %@\n UserInfo:%@", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If error is nil but syncingAppUserID == currentAppUserID
it will also print this error right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch. there was also a problem if where we wouldn't log success if the current app user id was the one being synced. fixed and extracted to a separate method
} | ||
NSMutableDictionary <NSString *, NSDictionary *> | ||
*groupedAttributes = self.storedAttributesForAllUsers.mutableCopy; | ||
[groupedAttributes removeObjectForKey:appUserID]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are removing all attributes for that user id. We can be sure they have been synced right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
they should be - we check that numberOfUnsyncedAttributesForAppUserID == 0
a couple of lines before doing this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oohhh got it
@vegaro ready for another pass |
NSMutableDictionary *groupedSubscriberAttributes = self.storedAttributesForAllUsers.mutableCopy; | ||
[groupedSubscriberAttributes removeObjectForKey:appUserID]; | ||
[self.userDefaults setObject:groupedSubscriberAttributes forKey:RCSubscriberAttributesKey]; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably synchronize this right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep! great catch
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
} | ||
NSMutableDictionary <NSString *, NSDictionary *> | ||
*groupedAttributes = self.storedAttributesForAllUsers.mutableCopy; | ||
[groupedAttributes removeObjectForKey:appUserID]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oohhh got it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if this was waiting for another review to get shipped :)
Fixes a bug where subscriber attributes are deleted when an alias is created.
This was happening because we clear everything in user defaults when creating an alias.
This PR ensures that instead of deleting subscriber attributes, we transfer them over to the new user, if any exist.
I tested this locally against the production server by:
This also means changing the format in which subscriber attributes are stored.
Before:
After:
To do: