From e7db1977b022b94e668a33527268f2f703c1e4d3 Mon Sep 17 00:00:00 2001 From: Kerem Erkan Date: Sun, 10 Sep 2017 14:08:56 +0300 Subject: [PATCH] Support for Core Data changes on iOS 11 Workaround for binary store changes on iOS 11. A better way to do this should be using NSBinaryStoreSecureDecodingClasses option in the future. --- Framework/Source/Events/CDEEventMigrator.m | 13 ++++++++++--- Framework/Tests/CDEBaseliningSyncTests.m | 7 ++++++- Framework/Tests/CDEEventMigratorTests.m | 7 ++++++- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/Framework/Source/Events/CDEEventMigrator.m b/Framework/Source/Events/CDEEventMigrator.m index c3d6d97e..2a187877 100644 --- a/Framework/Source/Events/CDEEventMigrator.m +++ b/Framework/Source/Events/CDEEventMigrator.m @@ -157,10 +157,17 @@ - (void)migrateEventsInFromFiles:(NSArray *)paths completion:(CDECompletionBlock NSDictionary *metadata = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType:nil URL:fileURL error:&error]; NSString *storeType = metadata[NSStoreTypeKey]; if (!storeType) @throw [[NSException alloc] initWithName:CDEException reason:@"" userInfo:nil]; - -#warning Using hard coded string here to get around problems in Xcode 9 Beta. Replace this with constant in final release - NSDictionary *options = @{NSMigratePersistentStoresAutomaticallyOption: @YES, NSInferMappingModelAutomaticallyOption: @YES, @"_NSBinaryStoreInsecureDecodingCompatibilityOption": @YES}; + + NSDictionary *options = nil; + if (@available(iOS 11.0, *)) { + options = @{NSMigratePersistentStoresAutomaticallyOption: @YES, NSInferMappingModelAutomaticallyOption: @YES, NSBinaryStoreInsecureDecodingCompatibilityOption: @YES}; + } else { + // Fallback on earlier versions + options = @{NSMigratePersistentStoresAutomaticallyOption: @YES, NSInferMappingModelAutomaticallyOption: @YES}; + } + fileStore = [importContext.persistentStoreCoordinator addPersistentStoreWithType:storeType configuration:nil URL:fileURL options:options error:&error]; + if (!fileStore) @throw [[NSException alloc] initWithName:CDEException reason:@"" userInfo:nil]; BOOL success = [self migrateObjectsInContext:importContext toContext:self.eventStore.managedObjectContext error:&error]; diff --git a/Framework/Tests/CDEBaseliningSyncTests.m b/Framework/Tests/CDEBaseliningSyncTests.m index f7927b2e..5fcb2ca2 100644 --- a/Framework/Tests/CDEBaseliningSyncTests.m +++ b/Framework/Tests/CDEBaseliningSyncTests.m @@ -355,7 +355,12 @@ - (NSManagedObjectContext *)eventFileContextForURL:(NSURL *)baselineURL NSManagedObjectModel *eventModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]; NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:eventModel]; baselineContext.persistentStoreCoordinator = coordinator; - NSDictionary *options = @{@"_NSBinaryStoreInsecureDecodingCompatibilityOption": @YES}; + NSDictionary *options = nil; + + if (@available(iOS 11.0, *)) { + options = @{NSBinaryStoreInsecureDecodingCompatibilityOption: @YES}; + } + NSPersistentStore *store = [coordinator addPersistentStoreWithType:NSBinaryStoreType configuration:nil URL:baselineURL options:options error:NULL]; XCTAssertNotNil(store, @"Store was nil"); return baselineContext; diff --git a/Framework/Tests/CDEEventMigratorTests.m b/Framework/Tests/CDEEventMigratorTests.m index 80c02685..00e984b5 100644 --- a/Framework/Tests/CDEEventMigratorTests.m +++ b/Framework/Tests/CDEEventMigratorTests.m @@ -115,7 +115,12 @@ - (NSManagedObjectContext *)makeFileContext NSURL *url = [NSURL fileURLWithPath:exportedEventsFile]; NSManagedObjectModel *model = self.eventStore.managedObjectContext.persistentStoreCoordinator.managedObjectModel; NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model]; - NSDictionary *options = @{@"_NSBinaryStoreInsecureDecodingCompatibilityOption" : @YES}; + NSDictionary *options = nil; + + if (@available(iOS 11.0, *)) { + options = @{NSBinaryStoreInsecureDecodingCompatibilityOption: @YES}; + } + [psc addPersistentStoreWithType:NSBinaryStoreType configuration:nil URL:url options:options error:NULL]; NSManagedObjectContext *newContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType]; newContext.persistentStoreCoordinator = psc;