diff --git a/platform-darwin/Source/MPAppDelegate_Shared.m b/platform-darwin/Source/MPAppDelegate_Shared.m index 3b867874..f17e38d4 100644 --- a/platform-darwin/Source/MPAppDelegate_Shared.m +++ b/platform-darwin/Source/MPAppDelegate_Shared.m @@ -74,11 +74,7 @@ - (void)setActiveUser:(MPUserEntity *)activeUser { - NSError *error; - if (activeUser.objectID.isTemporaryID && ![activeUser.managedObjectContext obtainPermanentIDsForObjects:@[ activeUser ] error:&error]) - MPError( error, @"Failed to obtain a permanent object ID after setting active user." ); - - self.activeUserOID = activeUser.objectID; + self.activeUserOID = activeUser.permanentObjectID; } - (void)handleCoordinatorError:(NSError *)error { diff --git a/platform-darwin/Source/MPAppDelegate_Store.m b/platform-darwin/Source/MPAppDelegate_Store.m index 66af701c..71e732b6 100644 --- a/platform-darwin/Source/MPAppDelegate_Store.m +++ b/platform-darwin/Source/MPAppDelegate_Store.m @@ -461,10 +461,6 @@ PearlAssociatedObjectProperty( NSNumber*, StoreCorrupted, storeCorrupted ); site.lastUsed = [NSDate date]; site.algorithm = algorithm; - NSError *error = nil; - if (site.objectID.isTemporaryID && ![context obtainPermanentIDsForObjects:@[ site ] error:&error]) - MPError( error, @"Failed to obtain a permanent object ID after creating new site." ); - [context saveToStore]; completion( site, context ); @@ -493,18 +489,14 @@ PearlAssociatedObjectProperty( NSNumber*, StoreCorrupted, storeCorrupted ); newSite.algorithm = site.algorithm; newSite.loginName = site.loginName; - NSError *error = nil; - if (![context obtainPermanentIDsForObjects:@[ newSite ] error:&error]) - MPError( error, @"Failed to obtain a permanent object ID after changing object type." ); - [context deleteObject:site]; [context saveToStore]; - [[NSNotificationCenter defaultCenter] postNotificationName:MPSiteUpdatedNotification object:site.objectID]; + [[NSNotificationCenter defaultCenter] postNotificationName:MPSiteUpdatedNotification object:site.permanentObjectID]; site = newSite; } - [[NSNotificationCenter defaultCenter] postNotificationName:MPSiteUpdatedNotification object:site.objectID]; + [[NSNotificationCenter defaultCenter] postNotificationName:MPSiteUpdatedNotification object:site.permanentObjectID]; return site; } diff --git a/platform-darwin/Source/MPEntities.h b/platform-darwin/Source/MPEntities.h index 347fd811..0d89c2a2 100644 --- a/platform-darwin/Source/MPEntities.h +++ b/platform-darwin/Source/MPEntities.h @@ -22,6 +22,12 @@ @end +@interface NSManagedObject(MP) + +- (NSManagedObjectID *)permanentObjectID; + +@end + @interface MPSiteQuestionEntity(MP) - (NSString *)resolveQuestionAnswerUsingKey:(MPKey *)key; diff --git a/platform-darwin/Source/MPEntities.m b/platform-darwin/Source/MPEntities.m index 0c413529..67f4b7dc 100644 --- a/platform-darwin/Source/MPEntities.m +++ b/platform-darwin/Source/MPEntities.m @@ -34,6 +34,23 @@ @end +@implementation NSManagedObject(MP) + +- (NSManagedObjectID *)permanentObjectID { + + NSManagedObjectID *objectID = self.objectID; + if ([objectID isTemporaryID]) { + NSError *error = nil; + if (![self.managedObjectContext obtainPermanentIDsForObjects:@[ self ] error:&error]) + MPError( error, @"Failed to obtain permanent object ID." ); + objectID = self.objectID; + } + + return objectID.isTemporaryID? nil: objectID; +} + +@end + @implementation MPSiteQuestionEntity(MP) - (NSString *)resolveQuestionAnswerUsingKey:(MPKey *)key { diff --git a/platform-darwin/Source/Mac/MPMacAppDelegate.m b/platform-darwin/Source/Mac/MPMacAppDelegate.m index 60680018..1bd50449 100644 --- a/platform-darwin/Source/Mac/MPMacAppDelegate.m +++ b/platform-darwin/Source/Mac/MPMacAppDelegate.m @@ -171,17 +171,17 @@ static OSStatus MPHotKeyHander(EventHandlerCallRef nextHandler, EventRef theEven - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender { // Save changes in the application's managed object context before the application terminates. - NSManagedObjectContext *context = [MPMacAppDelegate managedObjectContextForMainThreadIfReady]; - if (!context) + NSManagedObjectContext *mainContext = [MPMacAppDelegate managedObjectContextForMainThreadIfReady]; + if (!mainContext) return NSTerminateNow; - if (![context commitEditing]) + if (![mainContext commitEditing]) return NSTerminateCancel; - if (![context hasChanges]) + if (![mainContext hasChanges]) return NSTerminateNow; - [context saveToStore]; + [mainContext saveToStore]; return NSTerminateNow; } @@ -388,20 +388,16 @@ static OSStatus MPHotKeyHander(EventHandlerCallRef nextHandler, EventRef theEven return; NSString *name = [(NSSecureTextField *)alert.accessoryView stringValue]; - [MPMacAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *moc) { + [MPMacAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *context) { MPUserEntity *newUser = [NSEntityDescription insertNewObjectForEntityForName:NSStringFromClass( [MPUserEntity class] ) - inManagedObjectContext:moc]; + inManagedObjectContext:context]; newUser.name = name; - [moc saveToStore]; -// NSError *error = nil; -// if (![moc obtainPermanentIDsForObjects:@[ newUser ] error:&error]) -// MPError( error, @"Failed to obtain permanent object ID for new user." ); + [context saveToStore]; + [self setActiveUser:newUser]; - [[NSOperationQueue mainQueue] addOperationWithBlock:^{ - [self updateUsers]; - [self setActiveUser:newUser]; + PearlMainQueue( ^{ [self showPasswordWindow:nil]; - }]; + } ); }]; } @@ -415,10 +411,10 @@ static OSStatus MPHotKeyHander(EventHandlerCallRef nextHandler, EventRef theEven if ([alert runModal] != NSAlertFirstButtonReturn) return; - [MPMacAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *moc) { - [moc deleteObject:[self activeUserInContext:moc]]; + [MPMacAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *context) { + [context deleteObject:[self activeUserInContext:context]]; [self setActiveUser:nil]; - [moc saveToStore]; + [context saveToStore]; [[NSOperationQueue mainQueue] addOperationWithBlock:^{ [self updateUsers]; @@ -581,7 +577,7 @@ static OSStatus MPHotKeyHander(EventHandlerCallRef nextHandler, EventRef theEven for (MPUserEntity *user in users) { NSMenuItem *userItem = [[NSMenuItem alloc] initWithTitle:user.name action:@selector( selectUser: ) keyEquivalent:@""]; [userItem setTarget:self]; - [userItem setRepresentedObject:[user objectID]]; + [userItem setRepresentedObject:user.permanentObjectID]; [[self.usersItem submenu] addItem:userItem]; if (!mainActiveUser && [user.name isEqualToString:[MPMacConfig get].usedUserName]) diff --git a/platform-darwin/Source/Mac/MPPasswordWindowController.m b/platform-darwin/Source/Mac/MPPasswordWindowController.m index 50963200..6ffe74c7 100644 --- a/platform-darwin/Source/Mac/MPPasswordWindowController.m +++ b/platform-darwin/Source/Mac/MPPasswordWindowController.m @@ -160,10 +160,10 @@ - (IBAction)doUnlockUser:(id)sender { [self.progressView startAnimation:self]; - [MPMacAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *moc) { - MPUserEntity *activeUser = [[MPMacAppDelegate get] activeUserInContext:moc]; + [MPMacAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *context) { + MPUserEntity *activeUser = [[MPMacAppDelegate get] activeUserInContext:context]; NSString *userName = activeUser.name; - BOOL success = [[MPMacAppDelegate get] signInAsUser:activeUser saveInContext:moc usingMasterPassword:self.masterPassword]; + BOOL success = [[MPMacAppDelegate get] signInAsUser:activeUser saveInContext:context usingMasterPassword:self.masterPassword]; PearlMainQueue( ^{ self.masterPassword = nil; @@ -633,9 +633,9 @@ return; } - [MPMacAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *moc) { - [[self.selectedSite entityInContext:moc] use]; - [moc saveToStore]; + [MPMacAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *context) { + [[self.selectedSite entityInContext:context] use]; + [context saveToStore]; }]; } diff --git a/platform-darwin/Source/Mac/MPSiteModel.m b/platform-darwin/Source/Mac/MPSiteModel.m index be650560..59eb674c 100644 --- a/platform-darwin/Source/Mac/MPSiteModel.m +++ b/platform-darwin/Source/Mac/MPSiteModel.m @@ -52,9 +52,9 @@ - (void)setEntity:(MPSiteEntity *)entity fuzzyGroups:(NSArray *)fuzzyGroups { - if ([_entityOID isEqual:entity.objectID]) + if ([_entityOID isEqual:entity.permanentObjectID]) return; - _entityOID = entity.objectID; + _entityOID = entity.permanentObjectID; NSString *siteName = entity.name; NSMutableAttributedString *attributedSiteName = [[NSMutableAttributedString alloc] initWithString:siteName]; diff --git a/platform-darwin/Source/iOS/MPAnswersViewController.m b/platform-darwin/Source/iOS/MPAnswersViewController.m index f5f7df6f..363327e3 100644 --- a/platform-darwin/Source/iOS/MPAnswersViewController.m +++ b/platform-darwin/Source/iOS/MPAnswersViewController.m @@ -80,7 +80,7 @@ - (void)setSite:(MPSiteEntity *)site { - _siteOID = [site objectID]; + _siteOID = site.permanentObjectID; _multiple = [site.questions count] > 0; [self.tableView reloadData]; [self updateAnimated:NO]; @@ -301,8 +301,8 @@ - (void)setQuestion:(MPSiteQuestionEntity *)question forSite:(MPSiteEntity *)site inVC:(MPAnswersViewController *)answersVC { - _siteOID = site.objectID; - _questionOID = question.objectID; + _siteOID = site.permanentObjectID; + _questionOID = question.permanentObjectID; _answersVC = answersVC; [self updateAnswerForQuestion:question ofSite:site]; @@ -333,14 +333,7 @@ question.keyword = keyword; if ([context saveToStore]) { -// if ([question.objectID isTemporaryID]) { -// NSError *error = nil; -// [context obtainPermanentIDsForObjects:@[ question ] error:&error]; -// if (error) -// MPError( error, @"Failed to obtain permanent object ID: %@" ); -// } - - _questionOID = question.objectID; + _questionOID = question.permanentObjectID; [self updateAnswerForQuestion:question ofSite:site]; if (didAddQuestionObject) diff --git a/platform-darwin/Source/iOS/MPPasswordCell.m b/platform-darwin/Source/iOS/MPPasswordCell.m index cef71a3e..6462d23d 100644 --- a/platform-darwin/Source/iOS/MPPasswordCell.m +++ b/platform-darwin/Source/iOS/MPPasswordCell.m @@ -171,7 +171,7 @@ - (void)setSite:(MPSiteEntity *)site animated:(BOOL)animated { - _siteOID = [site objectID]; + _siteOID = site.permanentObjectID; [self updateAnimated:animated]; } diff --git a/platform-darwin/Source/iOS/MPUsersViewController.m b/platform-darwin/Source/iOS/MPUsersViewController.m index 869a20ca..6f141860 100644 --- a/platform-darwin/Source/iOS/MPUsersViewController.m +++ b/platform-darwin/Source/iOS/MPUsersViewController.m @@ -381,7 +381,7 @@ referenceSizeForFooterInSection:(NSInteger)section { MPAvatarCell *userAvatar = [self selectedAvatar]; userAvatar.spinnerActive = YES; if (!isNew && mainUser && [MPiOSAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *context) { - MPUserEntity *user = [MPUserEntity existingObjectWithID:mainUser.objectID inContext:context]; + MPUserEntity *user = [MPUserEntity existingObjectWithID:mainUser.permanentObjectID inContext:context]; BOOL signedIn = [[MPiOSAppDelegate get] signInAsUser:user saveInContext:context usingMasterPassword:nil]; PearlMainQueue( ^{ @@ -424,10 +424,10 @@ referenceSizeForFooterInSection:(NSInteger)section { BOOL isNew = NO; MPUserEntity *user = [self userForAvatar:avatarCell inContext:mainContext isNew:&isNew]; - NSManagedObjectID *userID = user.objectID; if (isNew || !user) return; + NSManagedObjectID *userID = user.permanentObjectID; [PearlSheet showSheetWithTitle:user.name viewStyle:UIActionSheetStyleBlackTranslucent initSheet:nil tappedButtonBlock:^(UIActionSheet *sheet, NSInteger buttonIndex) { @@ -737,7 +737,7 @@ referenceSizeForFooterInSection:(NSInteger)section { NSMutableArray *userIDs = [NSMutableArray arrayWithCapacity:[users count]]; for (MPUserEntity *user in users) - [userIDs addObject:user.objectID]; + [userIDs addObject:user.permanentObjectID]; self.userIDs = userIDs; }]) self.userIDs = nil; @@ -765,7 +765,7 @@ referenceSizeForFooterInSection:(NSInteger)section { NSManagedObjectID *selectUserID = [MPiOSAppDelegate get].activeUserOID; if (!selectUserID) selectUserID = [self selectedUserInContext:[MPiOSAppDelegate managedObjectContextForMainThreadIfReady] - isNew:&isNew].objectID; + isNew:&isNew].permanentObjectID; [self.avatarCollectionView reloadData]; NSUInteger selectedAvatarItem = isNew? [_userIDs count]: selectUserID? [_userIDs indexOfObject:selectUserID]: NSNotFound;