Skip to content

Commit

Permalink
fix: Use transactionWithBlock to avoid leaving realm in a write trans…
Browse files Browse the repository at this point in the history
…action

Signed-off-by: Ivan Sein <[email protected]>
  • Loading branch information
Ivansss committed Feb 25, 2025
1 parent 5e981ba commit 4f62050
Showing 1 changed file with 45 additions and 51 deletions.
96 changes: 45 additions & 51 deletions NextcloudTalk/NCSettingsController.m
Original file line number Diff line number Diff line change
Expand Up @@ -374,41 +374,39 @@ - (void)getUserProfileForAccountId:(NSString *)accountId withCompletionBlock:(Up
email = @"";
}
RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];

NSPredicate *query = [NSPredicate predicateWithFormat:@"accountId = %@", account.accountId];
TalkAccount *managedActiveAccount = [TalkAccount objectsWithPredicate:query].firstObject;

if (!managedActiveAccount) {
NSError *error = [NSError errorWithDomain:NSCocoaErrorDomain code:0 userInfo:nil];
block(error);

return;
}
[realm transactionWithBlock:^{
NSPredicate *query = [NSPredicate predicateWithFormat:@"accountId = %@", account.accountId];
TalkAccount *managedActiveAccount = [TalkAccount objectsWithPredicate:query].firstObject;

managedActiveAccount.userId = [userProfile objectForKey:kUserProfileUserId];
// "display-name" is returned by /cloud/user endpoint
// change to kUserProfileDisplayName ("displayName") when using /cloud/users/{userId} endpoint
managedActiveAccount.userDisplayName = [userProfile objectForKey:@"display-name"];
managedActiveAccount.userDisplayNameScope = [userProfile objectForKey:kUserProfileDisplayNameScope];
managedActiveAccount.phone = [userProfile objectForKey:kUserProfilePhone];
managedActiveAccount.phoneScope = [userProfile objectForKey:kUserProfilePhoneScope];
managedActiveAccount.email = email;
managedActiveAccount.emailScope = [userProfile objectForKey:kUserProfileEmailScope];
managedActiveAccount.address = [userProfile objectForKey:kUserProfileAddress];
managedActiveAccount.addressScope = [userProfile objectForKey:kUserProfileAddressScope];
managedActiveAccount.website = [userProfile objectForKey:kUserProfileWebsite];
managedActiveAccount.websiteScope = [userProfile objectForKey:kUserProfileWebsiteScope];
managedActiveAccount.twitter = [userProfile objectForKey:kUserProfileTwitter];
managedActiveAccount.twitterScope = [userProfile objectForKey:kUserProfileTwitterScope];
managedActiveAccount.avatarScope = [userProfile objectForKey:kUserProfileAvatarScope];
if (!managedActiveAccount) {
NSError *error = [NSError errorWithDomain:NSCocoaErrorDomain code:0 userInfo:nil];
block(error);

[realm commitWriteTransaction];
return;
}

TalkAccount *unmanagedUpdatedAccount = [[TalkAccount alloc] initWithValue:managedActiveAccount];
[[NCAPIController sharedInstance] saveProfileImageForAccount:unmanagedUpdatedAccount];
managedActiveAccount.userId = [userProfile objectForKey:kUserProfileUserId];
// "display-name" is returned by /cloud/user endpoint
// change to kUserProfileDisplayName ("displayName") when using /cloud/users/{userId} endpoint
managedActiveAccount.userDisplayName = [userProfile objectForKey:@"display-name"];
managedActiveAccount.userDisplayNameScope = [userProfile objectForKey:kUserProfileDisplayNameScope];
managedActiveAccount.phone = [userProfile objectForKey:kUserProfilePhone];
managedActiveAccount.phoneScope = [userProfile objectForKey:kUserProfilePhoneScope];
managedActiveAccount.email = email;
managedActiveAccount.emailScope = [userProfile objectForKey:kUserProfileEmailScope];
managedActiveAccount.address = [userProfile objectForKey:kUserProfileAddress];
managedActiveAccount.addressScope = [userProfile objectForKey:kUserProfileAddressScope];
managedActiveAccount.website = [userProfile objectForKey:kUserProfileWebsite];
managedActiveAccount.websiteScope = [userProfile objectForKey:kUserProfileWebsiteScope];
managedActiveAccount.twitter = [userProfile objectForKey:kUserProfileTwitter];
managedActiveAccount.twitterScope = [userProfile objectForKey:kUserProfileTwitterScope];
managedActiveAccount.avatarScope = [userProfile objectForKey:kUserProfileAvatarScope];

TalkAccount *unmanagedUpdatedAccount = [[TalkAccount alloc] initWithValue:managedActiveAccount];
[[NCAPIController sharedInstance] saveProfileImageForAccount:unmanagedUpdatedAccount];

block(nil);
block(nil);
}];
} else {
NSLog(@"Error while getting the user profile");
block(error);
Expand All @@ -427,18 +425,16 @@ - (void)getUserGroupsAndTeamsForAccountId:(NSString *)accountId
[[NCAPIController sharedInstance] getUserGroupsForAccount:account completionBlock:^(NSArray * _Nullable groupIds, NSError * _Nullable error) {
if (!error) {
RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];

NSPredicate *query = [NSPredicate predicateWithFormat:@"accountId = %@", account.accountId];
TalkAccount *managedActiveAccount = [TalkAccount objectsWithPredicate:query].firstObject;
[realm transactionWithBlock:^{
NSPredicate *query = [NSPredicate predicateWithFormat:@"accountId = %@", account.accountId];
TalkAccount *managedActiveAccount = [TalkAccount objectsWithPredicate:query].firstObject;

if (!managedActiveAccount) {
return;
}

managedActiveAccount.groupIds = groupIds;
if (!managedActiveAccount) {
return;
}

[realm commitWriteTransaction];
managedActiveAccount.groupIds = groupIds;
}];
} else {
NSLog(@"Error while getting user's groups");
}
Expand All @@ -447,18 +443,16 @@ - (void)getUserGroupsAndTeamsForAccountId:(NSString *)accountId
[[NCAPIController sharedInstance] getUserTeamsForAccount:account completionBlock:^(NSArray * _Nullable teamIds, NSError * _Nullable error) {
if (!error) {
RLMRealm *realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
[realm transactionWithBlock:^{
NSPredicate *query = [NSPredicate predicateWithFormat:@"accountId = %@", account.accountId];
TalkAccount *managedActiveAccount = [TalkAccount objectsWithPredicate:query].firstObject;

NSPredicate *query = [NSPredicate predicateWithFormat:@"accountId = %@", account.accountId];
TalkAccount *managedActiveAccount = [TalkAccount objectsWithPredicate:query].firstObject;

if (!managedActiveAccount) {
return;
}

managedActiveAccount.teamIds = teamIds;
if (!managedActiveAccount) {
return;
}

[realm commitWriteTransaction];
managedActiveAccount.teamIds = teamIds;
}];
} else {
NSLog(@"Error while getting user' teams");
}
Expand Down

0 comments on commit 4f62050

Please sign in to comment.