From c8f9f79bb2ca1062f2fca0766d54e1211c31f616 Mon Sep 17 00:00:00 2001 From: Maarten Billemont Date: Wed, 9 May 2012 19:34:00 +0200 Subject: [PATCH] Handle store loading errors. [ADDED] Detect store loading errors and attempt to recover. --- External/iCloudStoreManager | 2 +- MasterPassword/MPAppDelegate_Store.m | 48 +++++++++++++++-------- MasterPassword/iOS/MPAppDelegate.m | 31 +++++++++++++++ MasterPassword/iOS/MPMainViewController.m | 4 +- 4 files changed, 66 insertions(+), 19 deletions(-) diff --git a/External/iCloudStoreManager b/External/iCloudStoreManager index 303b3a3e..b456a349 160000 --- a/External/iCloudStoreManager +++ b/External/iCloudStoreManager @@ -1 +1 @@ -Subproject commit 303b3a3e5c092c21d8e0c92cbe9145b6492b6c7b +Subproject commit b456a34975e65fba8e2228b790706d852188ecb2 diff --git a/MasterPassword/MPAppDelegate_Store.m b/MasterPassword/MPAppDelegate_Store.m index b07d125b..51ee7a15 100644 --- a/MasterPassword/MPAppDelegate_Store.m +++ b/MasterPassword/MPAppDelegate_Store.m @@ -13,17 +13,7 @@ static NSDateFormatter *rfc3339DateFormatter = nil; -- (void)loadRFC3339DateFormatter { - - if (rfc3339DateFormatter) - return; - - rfc3339DateFormatter = [NSDateFormatter new]; - NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]; - [rfc3339DateFormatter setLocale:enUSPOSIXLocale]; - [rfc3339DateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"]; - [rfc3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; -} +#pragma mark - Core Data setup + (NSManagedObjectContext *)managedObjectContext { @@ -124,11 +114,6 @@ static NSDateFormatter *rfc3339DateFormatter = nil; return storeManager; } -- (NSManagedObjectContext *)managedObjectContextForUbiquityStoreManager:(UbiquityStoreManager *)usm { - - return self.managedObjectContext; -} - - (void)saveContext { [self.managedObjectContext performBlock:^{ @@ -184,6 +169,37 @@ static NSDateFormatter *rfc3339DateFormatter = nil; }]; } +#pragma mark - UbiquityStoreManagerDelegate + +- (NSManagedObjectContext *)managedObjectContextForUbiquityStoreManager:(UbiquityStoreManager *)usm { + + return self.managedObjectContext; +} + +- (void)ubiquityStoreManager:(UbiquityStoreManager *)manager log:(NSString *)message { + + dbg(@"StoreManager: %@", message); +} + +- (void)ubiquityStoreManager:(UbiquityStoreManager *)manager didEncounterError:(NSError *)error cause:(UbiquityStoreManagerErrorCause)cause context:(id)context { + + err(@"StoreManager: cause=%d, context=%@, error=%@", cause, context, error); +} + +#pragma mark - Import / Export + +- (void)loadRFC3339DateFormatter { + + if (rfc3339DateFormatter) + return; + + rfc3339DateFormatter = [NSDateFormatter new]; + NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]; + [rfc3339DateFormatter setLocale:enUSPOSIXLocale]; + [rfc3339DateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"]; + [rfc3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; +} + - (MPImportResult)importSites:(NSString *)importedSitesString withPassword:(NSString *)password askConfirmation:(BOOL(^)(NSUInteger importCount, NSUInteger deleteCount))confirmation { diff --git a/MasterPassword/iOS/MPAppDelegate.m b/MasterPassword/iOS/MPAppDelegate.m index 53b4e2e9..f281dbd2 100644 --- a/MasterPassword/iOS/MPAppDelegate.m +++ b/MasterPassword/iOS/MPAppDelegate.m @@ -394,6 +394,37 @@ [controller dismissModalViewControllerAnimated:YES]; } +#pragma mark - UbiquityStoreManagerDelegate + +- (void)ubiquityStoreManager:(UbiquityStoreManager *)manager didEncounterError:(NSError *)error cause:(UbiquityStoreManagerErrorCause)cause context:(id)context { + + err(@"StoreManager: cause=%d, context=%@, error=%@", cause, context, error); + + switch (cause) { + case UbiquityStoreManagerErrorCauseDeleteStore: + case UbiquityStoreManagerErrorCauseCreateStorePath: + case UbiquityStoreManagerErrorCauseClearStore: + break; + case UbiquityStoreManagerErrorCauseOpenLocalStore: { + [PearlAlert showError:@"Could not open your local database.\n\n" + @"The database may be too old. It will be deleted, the application will quit, " + @"and on the next launch a fresh database will be created." tappedButtonBlock:^(UIAlertView *alert, NSInteger buttonIndex) { + wrn(@"Local store could not be opened, deleting it."); + [[NSFileManager defaultManager] removeItemAtURL:context error:nil]; + exit(0); + } otherTitles:nil]; + break; + } + case UbiquityStoreManagerErrorCauseOpenCloudStore: { + [PearlAlert showError:@"Could not use iCloud-synced storage. Will use a local database for now.\n\n" + @"If this problem persists, you may need to reset your Master Password database on iCloud."]; + wrn(@"iCloud store could not be opened, moving to local store."); + [manager useiCloudStore:NO alertUser:NO]; + break; + } + } +} + #pragma mark - TestFlight diff --git a/MasterPassword/iOS/MPMainViewController.m b/MasterPassword/iOS/MPMainViewController.m index cb227f52..275b2e51 100644 --- a/MasterPassword/iOS/MPMainViewController.m +++ b/MasterPassword/iOS/MPMainViewController.m @@ -426,12 +426,12 @@ break; #ifdef ADHOC case 7: - [[MPAppDelegate get].storeManager useiCloudStore:![MPAppDelegate get].storeManager.iCloudEnabled]; + [[MPAppDelegate get].storeManager useiCloudStore:![MPAppDelegate get].storeManager.iCloudEnabled alertUser:YES]; break; case 8: #else case 6: - [[MPAppDelegate get].storeManager useiCloudStore:![MPAppDelegate get].storeManager.iCloudEnabled]; + [[MPAppDelegate get].storeManager useiCloudStore:![MPAppDelegate get].storeManager.iCloudEnabled alertUser:YES]; break; case 7: #endif