Rework thread safety in profile manager.

pull/1/head
Matthew Chen 8 years ago
parent 74efcb9041
commit 3ea901044a

@ -76,11 +76,10 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
#pragma mark - Update With... Methods #pragma mark - Update With... Methods
- (OWSUserProfileCompletion)updateWithCompletion:(nullable OWSUserProfileCompletion)externalCompletion - (void)finalizeWithCompletion:(nullable OWSUserProfileCompletion)externalCompletion
{ {
return ^{
if (externalCompletion) { if (externalCompletion) {
externalCompletion(); dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), externalCompletion);
} }
BOOL isLocalUserProfile = [self.recipientId isEqualToString:kLocalProfileUniqueId]; BOOL isLocalUserProfile = [self.recipientId isEqualToString:kLocalProfileUniqueId];
@ -94,8 +93,7 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
[CurrentAppContext() doMultiDeviceUpdateWithProfileKey:self.profileKey]; [CurrentAppContext() doMultiDeviceUpdateWithProfileKey:self.profileKey];
} }
[[NSNotificationCenter defaultCenter] [[NSNotificationCenter defaultCenter] postNotificationNameAsync:kNSNotificationName_LocalProfileDidChange
postNotificationNameAsync:kNSNotificationName_LocalProfileDidChange
object:nil object:nil
userInfo:nil]; userInfo:nil];
} else { } else {
@ -113,7 +111,6 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
}]; }];
} }
}); });
};
} }
- (void)updateWithProfileName:(nullable NSString *)profileName - (void)updateWithProfileName:(nullable NSString *)profileName
@ -122,15 +119,15 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
dbConnection:(YapDatabaseConnection *)dbConnection dbConnection:(YapDatabaseConnection *)dbConnection
completion:(nullable OWSUserProfileCompletion)completion completion:(nullable OWSUserProfileCompletion)completion
{ {
[dbConnection asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[self applyChangeToSelfAndLatestCopy:transaction [self applyChangeToSelfAndLatestCopy:transaction
changeBlock:^(OWSUserProfile *userProfile) { changeBlock:^(OWSUserProfile *userProfile) {
[userProfile setProfileName:[profileName ows_stripped]]; [userProfile setProfileName:[profileName ows_stripped]];
[userProfile setAvatarUrlPath:avatarUrlPath]; [userProfile setAvatarUrlPath:avatarUrlPath];
[userProfile setAvatarFileName:avatarFileName]; [userProfile setAvatarFileName:avatarFileName];
}]; }];
} }];
completionBlock:[self updateWithCompletion:completion]]; [self finalizeWithCompletion:completion];
} }
- (void)updateWithProfileName:(nullable NSString *)profileName - (void)updateWithProfileName:(nullable NSString *)profileName
@ -140,7 +137,7 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
dbConnection:(YapDatabaseConnection *)dbConnection dbConnection:(YapDatabaseConnection *)dbConnection
completion:(nullable OWSUserProfileCompletion)completion completion:(nullable OWSUserProfileCompletion)completion
{ {
[dbConnection asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[self applyChangeToSelfAndLatestCopy:transaction [self applyChangeToSelfAndLatestCopy:transaction
changeBlock:^(OWSUserProfile *userProfile) { changeBlock:^(OWSUserProfile *userProfile) {
[userProfile setProfileName:[profileName ows_stripped]]; [userProfile setProfileName:[profileName ows_stripped]];
@ -148,8 +145,8 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
[userProfile setAvatarFileName:avatarFileName]; [userProfile setAvatarFileName:avatarFileName];
[userProfile setLastUpdateDate:lastUpdateDate]; [userProfile setLastUpdateDate:lastUpdateDate];
}]; }];
} }];
completionBlock:[self updateWithCompletion:completion]]; [self finalizeWithCompletion:completion];
} }
- (void)updateWithProfileName:(nullable NSString *)profileName - (void)updateWithProfileName:(nullable NSString *)profileName
@ -158,15 +155,15 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
dbConnection:(YapDatabaseConnection *)dbConnection dbConnection:(YapDatabaseConnection *)dbConnection
completion:(nullable OWSUserProfileCompletion)completion completion:(nullable OWSUserProfileCompletion)completion
{ {
[dbConnection asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[self applyChangeToSelfAndLatestCopy:transaction [self applyChangeToSelfAndLatestCopy:transaction
changeBlock:^(OWSUserProfile *userProfile) { changeBlock:^(OWSUserProfile *userProfile) {
[userProfile setProfileName:[profileName ows_stripped]]; [userProfile setProfileName:[profileName ows_stripped]];
[userProfile setAvatarUrlPath:avatarUrlPath]; [userProfile setAvatarUrlPath:avatarUrlPath];
[userProfile setLastUpdateDate:lastUpdateDate]; [userProfile setLastUpdateDate:lastUpdateDate];
}]; }];
} }];
completionBlock:[self updateWithCompletion:completion]]; [self finalizeWithCompletion:completion];
} }
- (void)updateWithProfileName:(nullable NSString *)profileName - (void)updateWithProfileName:(nullable NSString *)profileName
@ -176,7 +173,7 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
dbConnection:(YapDatabaseConnection *)dbConnection dbConnection:(YapDatabaseConnection *)dbConnection
completion:(nullable OWSUserProfileCompletion)completion completion:(nullable OWSUserProfileCompletion)completion
{ {
[dbConnection asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[self applyChangeToSelfAndLatestCopy:transaction [self applyChangeToSelfAndLatestCopy:transaction
changeBlock:^(OWSUserProfile *userProfile) { changeBlock:^(OWSUserProfile *userProfile) {
[userProfile setProfileName:[profileName ows_stripped]]; [userProfile setProfileName:[profileName ows_stripped]];
@ -184,8 +181,8 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
[userProfile setAvatarUrlPath:avatarUrlPath]; [userProfile setAvatarUrlPath:avatarUrlPath];
[userProfile setAvatarFileName:avatarFileName]; [userProfile setAvatarFileName:avatarFileName];
}]; }];
} }];
completionBlock:[self updateWithCompletion:completion]]; [self finalizeWithCompletion:completion];
} }
- (void)updateWithAvatarUrlPath:(nullable NSString *)avatarUrlPath - (void)updateWithAvatarUrlPath:(nullable NSString *)avatarUrlPath
@ -193,40 +190,40 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
dbConnection:(YapDatabaseConnection *)dbConnection dbConnection:(YapDatabaseConnection *)dbConnection
completion:(nullable OWSUserProfileCompletion)completion completion:(nullable OWSUserProfileCompletion)completion
{ {
[dbConnection asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[self applyChangeToSelfAndLatestCopy:transaction [self applyChangeToSelfAndLatestCopy:transaction
changeBlock:^(OWSUserProfile *userProfile) { changeBlock:^(OWSUserProfile *userProfile) {
[userProfile setAvatarUrlPath:avatarUrlPath]; [userProfile setAvatarUrlPath:avatarUrlPath];
[userProfile setAvatarFileName:avatarFileName]; [userProfile setAvatarFileName:avatarFileName];
}]; }];
} }];
completionBlock:[self updateWithCompletion:completion]]; [self finalizeWithCompletion:completion];
} }
- (void)updateWithAvatarFileName:(nullable NSString *)avatarFileName - (void)updateWithAvatarFileName:(nullable NSString *)avatarFileName
dbConnection:(YapDatabaseConnection *)dbConnection dbConnection:(YapDatabaseConnection *)dbConnection
completion:(nullable OWSUserProfileCompletion)completion completion:(nullable OWSUserProfileCompletion)completion
{ {
[dbConnection asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[self applyChangeToSelfAndLatestCopy:transaction [self applyChangeToSelfAndLatestCopy:transaction
changeBlock:^(OWSUserProfile *userProfile) { changeBlock:^(OWSUserProfile *userProfile) {
[userProfile setAvatarFileName:avatarFileName]; [userProfile setAvatarFileName:avatarFileName];
}]; }];
} }];
completionBlock:[self updateWithCompletion:completion]]; [self finalizeWithCompletion:completion];
} }
- (void)updateWithLastUpdateDate:(nullable NSDate *)lastUpdateDate - (void)updateWithLastUpdateDate:(nullable NSDate *)lastUpdateDate
dbConnection:(YapDatabaseConnection *)dbConnection dbConnection:(YapDatabaseConnection *)dbConnection
completion:(nullable OWSUserProfileCompletion)completion completion:(nullable OWSUserProfileCompletion)completion
{ {
[dbConnection asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[self applyChangeToSelfAndLatestCopy:transaction [self applyChangeToSelfAndLatestCopy:transaction
changeBlock:^(OWSUserProfile *userProfile) { changeBlock:^(OWSUserProfile *userProfile) {
[userProfile setLastUpdateDate:lastUpdateDate]; [userProfile setLastUpdateDate:lastUpdateDate];
}]; }];
} }];
completionBlock:[self updateWithCompletion:completion]]; [self finalizeWithCompletion:completion];
} }
- (void)clearWithProfileKey:(OWSAES256Key *)profileKey - (void)clearWithProfileKey:(OWSAES256Key *)profileKey
@ -235,7 +232,7 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
{ {
OWSAssert(profileKey); OWSAssert(profileKey);
[dbConnection asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[self applyChangeToSelfAndLatestCopy:transaction [self applyChangeToSelfAndLatestCopy:transaction
changeBlock:^(OWSUserProfile *userProfile) { changeBlock:^(OWSUserProfile *userProfile) {
[userProfile setProfileKey:profileKey]; [userProfile setProfileKey:profileKey];
@ -244,8 +241,8 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
[userProfile setAvatarFileName:nil]; [userProfile setAvatarFileName:nil];
[userProfile setLastUpdateDate:nil]; [userProfile setLastUpdateDate:nil];
}]; }];
} }];
completionBlock:[self updateWithCompletion:completion]]; [self finalizeWithCompletion:completion];
} }
- (void)updateImmediatelyWithProfileKey:(OWSAES256Key *)profileKey - (void)updateImmediatelyWithProfileKey:(OWSAES256Key *)profileKey
@ -256,13 +253,13 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
self.profileKey = profileKey; self.profileKey = profileKey;
[dbConnection asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[self applyChangeToSelfAndLatestCopy:transaction [self applyChangeToSelfAndLatestCopy:transaction
changeBlock:^(OWSUserProfile *userProfile) { changeBlock:^(OWSUserProfile *userProfile) {
[userProfile setProfileKey:profileKey]; [userProfile setProfileKey:profileKey];
}]; }];
} }];
completionBlock:[self updateWithCompletion:completion]]; [self finalizeWithCompletion:completion];
} }
#pragma mark - Database Connection Accessors #pragma mark - Database Connection Accessors

Loading…
Cancel
Save