Skip to content

Commit

Permalink
Fix Core Data concurrency issues in AccountService
Browse files Browse the repository at this point in the history
  • Loading branch information
crazytonyli committed Mar 22, 2023
1 parent 8f01121 commit 99a9c6e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
2 changes: 1 addition & 1 deletion WordPress/Classes/Services/AccountService.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ extern NSNotificationName const WPAccountEmailAndDefaultBlogUpdatedNotification;
Updates the default blog for the specified account. The default blog will be the one whose siteID matches
the accounts primaryBlogID.
*/
- (void)updateDefaultBlogIfNeeded:(WPAccount *)account;
- (void)updateDefaultBlogIfNeeded:(WPAccount *)account inContext:(NSManagedObjectContext *)context;

/**
Syncs the details for the account associated with the provided auth token, then
Expand Down
27 changes: 20 additions & 7 deletions WordPress/Classes/Services/AccountService.m
Original file line number Diff line number Diff line change
Expand Up @@ -351,12 +351,14 @@ - (void)updateAccountWithID:(NSManagedObjectID *)objectID withUserDetails:(Remot
// Make sure the account is saved before updating its default blog.
[self.coreDataStack performAndSaveUsingBlock:^(NSManagedObjectContext *context) {
WPAccount *account = [context existingObjectWithID:objectID error:nil];
[self updateDefaultBlogIfNeeded:account];
[self updateDefaultBlogIfNeeded:account inContext:context];
}];
}

- (void)updateDefaultBlogIfNeeded:(WPAccount *)account
- (void)updateDefaultBlogIfNeeded:(WPAccount *)account inContext:(NSManagedObjectContext *)context
{
NSParameterAssert(account.managedObjectContext == context);

if (!account.primaryBlogID || [account.primaryBlogID intValue] == 0) {
return;
}
Expand All @@ -375,17 +377,27 @@ - (void)updateDefaultBlogIfNeeded:(WPAccount *)account

// Update app extensions if needed.
if ([account isDefaultWordPressComAccount]) {
[self setupAppExtensionsWithDefaultAccount];
[self setupAppExtensionsWithDefaultAccount:account inContext:context];
}
}

- (void)setupAppExtensionsWithDefaultAccount
{
WPAccount * __block defaultAccount = nil;
[self.coreDataStack.mainContext performBlockAndWait:^{
defaultAccount = [WPAccount lookupDefaultWordPressComAccountInContext:self.coreDataStack.mainContext];
NSManagedObjectContext *context = self.coreDataStack.mainContext;
[context performBlockAndWait:^{
WPAccount *account = [WPAccount lookupDefaultWordPressComAccountInContext:context];
if (account == nil) {
return;
}
[self setupAppExtensionsWithDefaultAccount:account inContext:context];
}];
}

- (void)setupAppExtensionsWithDefaultAccount:(WPAccount *)defaultAccount inContext:(NSManagedObjectContext *)context
{
NSParameterAssert(defaultAccount.managedObjectContext == context);

NSManagedObjectID *defaultAccountObjectID = defaultAccount.objectID;
Blog *defaultBlog = [defaultAccount defaultBlog];
NSNumber *siteId = defaultBlog.dotComID;
NSString *blogName = defaultBlog.settings.name;
Expand All @@ -412,7 +424,7 @@ - (void)setupAppExtensionsWithDefaultAccount
NSString *todayExtensionBlogName = [sharedDefaults objectForKey:AppConfigurationWidgetStatsToday.userDefaultsSiteNameKey];
NSString *todayExtensionBlogUrl = [sharedDefaults objectForKey:AppConfigurationWidgetStatsToday.userDefaultsSiteUrlKey];

Blog *todayExtensionBlog = [Blog lookupWithID:todayExtensionSiteID in:self.coreDataStack.mainContext];
Blog *todayExtensionBlog = [Blog lookupWithID:todayExtensionSiteID in:context];
NSTimeZone *timeZone = [todayExtensionBlog timeZone];

if (todayExtensionSiteID == NULL || todayExtensionBlog == nil) {
Expand All @@ -423,6 +435,7 @@ - (void)setupAppExtensionsWithDefaultAccount
}

dispatch_async(dispatch_get_main_queue(), ^{
WPAccount *defaultAccount = [self.coreDataStack.mainContext existingObjectWithID:defaultAccountObjectID error:nil];

TodayExtensionService *service = [TodayExtensionService new];
[service configureTodayWidgetWithSiteID:todayExtensionSiteID
Expand Down
2 changes: 1 addition & 1 deletion WordPress/Classes/Services/BlogService.m
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ - (void)mergeBlogs:(NSArray<RemoteBlog *> *)blogs withAccountID:(NSManagedObject

// Ensure that the account has a default blog defined (if there is one).
AccountService *service = [[AccountService alloc] initWithCoreDataStack:self.coreDataStack];
[service updateDefaultBlogIfNeeded:account];
[service updateDefaultBlogIfNeeded:account inContext:context];
}

- (void)updateBlogWithRemoteBlog:(RemoteBlog *)remoteBlog account:(WPAccount *)account inContext:(NSManagedObjectContext *)context
Expand Down

0 comments on commit 99a9c6e

Please sign in to comment.