2
0

Fix usage of dubious objectID in global context.

This commit is contained in:
Maarten Billemont 2017-04-29 23:52:57 -04:00
parent 6d9be3fdfe
commit 834e94ebd5
10 changed files with 58 additions and 58 deletions

View File

@ -74,11 +74,7 @@
- (void)setActiveUser:(MPUserEntity *)activeUser { - (void)setActiveUser:(MPUserEntity *)activeUser {
NSError *error; self.activeUserOID = activeUser.permanentObjectID;
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;
} }
- (void)handleCoordinatorError:(NSError *)error { - (void)handleCoordinatorError:(NSError *)error {

View File

@ -461,10 +461,6 @@ PearlAssociatedObjectProperty( NSNumber*, StoreCorrupted, storeCorrupted );
site.lastUsed = [NSDate date]; site.lastUsed = [NSDate date];
site.algorithm = algorithm; 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]; [context saveToStore];
completion( site, context ); completion( site, context );
@ -493,18 +489,14 @@ PearlAssociatedObjectProperty( NSNumber*, StoreCorrupted, storeCorrupted );
newSite.algorithm = site.algorithm; newSite.algorithm = site.algorithm;
newSite.loginName = site.loginName; 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 deleteObject:site];
[context saveToStore]; [context saveToStore];
[[NSNotificationCenter defaultCenter] postNotificationName:MPSiteUpdatedNotification object:site.objectID]; [[NSNotificationCenter defaultCenter] postNotificationName:MPSiteUpdatedNotification object:site.permanentObjectID];
site = newSite; site = newSite;
} }
[[NSNotificationCenter defaultCenter] postNotificationName:MPSiteUpdatedNotification object:site.objectID]; [[NSNotificationCenter defaultCenter] postNotificationName:MPSiteUpdatedNotification object:site.permanentObjectID];
return site; return site;
} }

View File

@ -22,6 +22,12 @@
@end @end
@interface NSManagedObject(MP)
- (NSManagedObjectID *)permanentObjectID;
@end
@interface MPSiteQuestionEntity(MP) @interface MPSiteQuestionEntity(MP)
- (NSString *)resolveQuestionAnswerUsingKey:(MPKey *)key; - (NSString *)resolveQuestionAnswerUsingKey:(MPKey *)key;

View File

@ -34,6 +34,23 @@
@end @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) @implementation MPSiteQuestionEntity(MP)
- (NSString *)resolveQuestionAnswerUsingKey:(MPKey *)key { - (NSString *)resolveQuestionAnswerUsingKey:(MPKey *)key {

View File

@ -171,17 +171,17 @@ static OSStatus MPHotKeyHander(EventHandlerCallRef nextHandler, EventRef theEven
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender { - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender {
// Save changes in the application's managed object context before the application terminates. // Save changes in the application's managed object context before the application terminates.
NSManagedObjectContext *context = [MPMacAppDelegate managedObjectContextForMainThreadIfReady]; NSManagedObjectContext *mainContext = [MPMacAppDelegate managedObjectContextForMainThreadIfReady];
if (!context) if (!mainContext)
return NSTerminateNow; return NSTerminateNow;
if (![context commitEditing]) if (![mainContext commitEditing])
return NSTerminateCancel; return NSTerminateCancel;
if (![context hasChanges]) if (![mainContext hasChanges])
return NSTerminateNow; return NSTerminateNow;
[context saveToStore]; [mainContext saveToStore];
return NSTerminateNow; return NSTerminateNow;
} }
@ -388,20 +388,16 @@ static OSStatus MPHotKeyHander(EventHandlerCallRef nextHandler, EventRef theEven
return; return;
NSString *name = [(NSSecureTextField *)alert.accessoryView stringValue]; NSString *name = [(NSSecureTextField *)alert.accessoryView stringValue];
[MPMacAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *moc) { [MPMacAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *context) {
MPUserEntity *newUser = [NSEntityDescription insertNewObjectForEntityForName:NSStringFromClass( [MPUserEntity class] ) MPUserEntity *newUser = [NSEntityDescription insertNewObjectForEntityForName:NSStringFromClass( [MPUserEntity class] )
inManagedObjectContext:moc]; inManagedObjectContext:context];
newUser.name = name; newUser.name = name;
[moc saveToStore]; [context saveToStore];
// NSError *error = nil; [self setActiveUser:newUser];
// if (![moc obtainPermanentIDsForObjects:@[ newUser ] error:&error])
// MPError( error, @"Failed to obtain permanent object ID for new user." );
[[NSOperationQueue mainQueue] addOperationWithBlock:^{ PearlMainQueue( ^{
[self updateUsers];
[self setActiveUser:newUser];
[self showPasswordWindow:nil]; [self showPasswordWindow:nil];
}]; } );
}]; }];
} }
@ -415,10 +411,10 @@ static OSStatus MPHotKeyHander(EventHandlerCallRef nextHandler, EventRef theEven
if ([alert runModal] != NSAlertFirstButtonReturn) if ([alert runModal] != NSAlertFirstButtonReturn)
return; return;
[MPMacAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *moc) { [MPMacAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *context) {
[moc deleteObject:[self activeUserInContext:moc]]; [context deleteObject:[self activeUserInContext:context]];
[self setActiveUser:nil]; [self setActiveUser:nil];
[moc saveToStore]; [context saveToStore];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{ [[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self updateUsers]; [self updateUsers];
@ -581,7 +577,7 @@ static OSStatus MPHotKeyHander(EventHandlerCallRef nextHandler, EventRef theEven
for (MPUserEntity *user in users) { for (MPUserEntity *user in users) {
NSMenuItem *userItem = [[NSMenuItem alloc] initWithTitle:user.name action:@selector( selectUser: ) keyEquivalent:@""]; NSMenuItem *userItem = [[NSMenuItem alloc] initWithTitle:user.name action:@selector( selectUser: ) keyEquivalent:@""];
[userItem setTarget:self]; [userItem setTarget:self];
[userItem setRepresentedObject:[user objectID]]; [userItem setRepresentedObject:user.permanentObjectID];
[[self.usersItem submenu] addItem:userItem]; [[self.usersItem submenu] addItem:userItem];
if (!mainActiveUser && [user.name isEqualToString:[MPMacConfig get].usedUserName]) if (!mainActiveUser && [user.name isEqualToString:[MPMacConfig get].usedUserName])

View File

@ -160,10 +160,10 @@
- (IBAction)doUnlockUser:(id)sender { - (IBAction)doUnlockUser:(id)sender {
[self.progressView startAnimation:self]; [self.progressView startAnimation:self];
[MPMacAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *moc) { [MPMacAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *context) {
MPUserEntity *activeUser = [[MPMacAppDelegate get] activeUserInContext:moc]; MPUserEntity *activeUser = [[MPMacAppDelegate get] activeUserInContext:context];
NSString *userName = activeUser.name; 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( ^{ PearlMainQueue( ^{
self.masterPassword = nil; self.masterPassword = nil;
@ -633,9 +633,9 @@
return; return;
} }
[MPMacAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *moc) { [MPMacAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *context) {
[[self.selectedSite entityInContext:moc] use]; [[self.selectedSite entityInContext:context] use];
[moc saveToStore]; [context saveToStore];
}]; }];
} }

View File

@ -52,9 +52,9 @@
- (void)setEntity:(MPSiteEntity *)entity fuzzyGroups:(NSArray *)fuzzyGroups { - (void)setEntity:(MPSiteEntity *)entity fuzzyGroups:(NSArray *)fuzzyGroups {
if ([_entityOID isEqual:entity.objectID]) if ([_entityOID isEqual:entity.permanentObjectID])
return; return;
_entityOID = entity.objectID; _entityOID = entity.permanentObjectID;
NSString *siteName = entity.name; NSString *siteName = entity.name;
NSMutableAttributedString *attributedSiteName = [[NSMutableAttributedString alloc] initWithString:siteName]; NSMutableAttributedString *attributedSiteName = [[NSMutableAttributedString alloc] initWithString:siteName];

View File

@ -80,7 +80,7 @@
- (void)setSite:(MPSiteEntity *)site { - (void)setSite:(MPSiteEntity *)site {
_siteOID = [site objectID]; _siteOID = site.permanentObjectID;
_multiple = [site.questions count] > 0; _multiple = [site.questions count] > 0;
[self.tableView reloadData]; [self.tableView reloadData];
[self updateAnimated:NO]; [self updateAnimated:NO];
@ -301,8 +301,8 @@
- (void)setQuestion:(MPSiteQuestionEntity *)question forSite:(MPSiteEntity *)site inVC:(MPAnswersViewController *)answersVC { - (void)setQuestion:(MPSiteQuestionEntity *)question forSite:(MPSiteEntity *)site inVC:(MPAnswersViewController *)answersVC {
_siteOID = site.objectID; _siteOID = site.permanentObjectID;
_questionOID = question.objectID; _questionOID = question.permanentObjectID;
_answersVC = answersVC; _answersVC = answersVC;
[self updateAnswerForQuestion:question ofSite:site]; [self updateAnswerForQuestion:question ofSite:site];
@ -333,14 +333,7 @@
question.keyword = keyword; question.keyword = keyword;
if ([context saveToStore]) { if ([context saveToStore]) {
// if ([question.objectID isTemporaryID]) { _questionOID = question.permanentObjectID;
// NSError *error = nil;
// [context obtainPermanentIDsForObjects:@[ question ] error:&error];
// if (error)
// MPError( error, @"Failed to obtain permanent object ID: %@" );
// }
_questionOID = question.objectID;
[self updateAnswerForQuestion:question ofSite:site]; [self updateAnswerForQuestion:question ofSite:site];
if (didAddQuestionObject) if (didAddQuestionObject)

View File

@ -171,7 +171,7 @@
- (void)setSite:(MPSiteEntity *)site animated:(BOOL)animated { - (void)setSite:(MPSiteEntity *)site animated:(BOOL)animated {
_siteOID = [site objectID]; _siteOID = site.permanentObjectID;
[self updateAnimated:animated]; [self updateAnimated:animated];
} }

View File

@ -381,7 +381,7 @@ referenceSizeForFooterInSection:(NSInteger)section {
MPAvatarCell *userAvatar = [self selectedAvatar]; MPAvatarCell *userAvatar = [self selectedAvatar];
userAvatar.spinnerActive = YES; userAvatar.spinnerActive = YES;
if (!isNew && mainUser && [MPiOSAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *context) { 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]; BOOL signedIn = [[MPiOSAppDelegate get] signInAsUser:user saveInContext:context usingMasterPassword:nil];
PearlMainQueue( ^{ PearlMainQueue( ^{
@ -424,10 +424,10 @@ referenceSizeForFooterInSection:(NSInteger)section {
BOOL isNew = NO; BOOL isNew = NO;
MPUserEntity *user = [self userForAvatar:avatarCell inContext:mainContext isNew:&isNew]; MPUserEntity *user = [self userForAvatar:avatarCell inContext:mainContext isNew:&isNew];
NSManagedObjectID *userID = user.objectID;
if (isNew || !user) if (isNew || !user)
return; return;
NSManagedObjectID *userID = user.permanentObjectID;
[PearlSheet showSheetWithTitle:user.name [PearlSheet showSheetWithTitle:user.name
viewStyle:UIActionSheetStyleBlackTranslucent viewStyle:UIActionSheetStyleBlackTranslucent
initSheet:nil tappedButtonBlock:^(UIActionSheet *sheet, NSInteger buttonIndex) { initSheet:nil tappedButtonBlock:^(UIActionSheet *sheet, NSInteger buttonIndex) {
@ -737,7 +737,7 @@ referenceSizeForFooterInSection:(NSInteger)section {
NSMutableArray *userIDs = [NSMutableArray arrayWithCapacity:[users count]]; NSMutableArray *userIDs = [NSMutableArray arrayWithCapacity:[users count]];
for (MPUserEntity *user in users) for (MPUserEntity *user in users)
[userIDs addObject:user.objectID]; [userIDs addObject:user.permanentObjectID];
self.userIDs = userIDs; self.userIDs = userIDs;
}]) }])
self.userIDs = nil; self.userIDs = nil;
@ -765,7 +765,7 @@ referenceSizeForFooterInSection:(NSInteger)section {
NSManagedObjectID *selectUserID = [MPiOSAppDelegate get].activeUserOID; NSManagedObjectID *selectUserID = [MPiOSAppDelegate get].activeUserOID;
if (!selectUserID) if (!selectUserID)
selectUserID = [self selectedUserInContext:[MPiOSAppDelegate managedObjectContextForMainThreadIfReady] selectUserID = [self selectedUserInContext:[MPiOSAppDelegate managedObjectContextForMainThreadIfReady]
isNew:&isNew].objectID; isNew:&isNew].permanentObjectID;
[self.avatarCollectionView reloadData]; [self.avatarCollectionView reloadData];
NSUInteger selectedAvatarItem = isNew? [_userIDs count]: selectUserID? [_userIDs indexOfObject:selectUserID]: NSNotFound; NSUInteger selectedAvatarItem = isNew? [_userIDs count]: selectUserID? [_userIDs indexOfObject:selectUserID]: NSNotFound;