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 {
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 {

View File

@ -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;
}

View File

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

View File

@ -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 {

View File

@ -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." );
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self updateUsers];
[context saveToStore];
[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])

View File

@ -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];
}];
}

View File

@ -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];

View File

@ -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)

View File

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

View File

@ -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;