Add support for excluding the SQLite store file from iCloud Backups to conform with the iOS Data Storage Guidelines. closes #929

This commit is contained in:
Blake Watters
2012-10-29 01:12:09 -04:00
parent c44f4010fa
commit 6d487dbc9e
3 changed files with 34 additions and 1 deletions

View File

@@ -133,6 +133,7 @@
@param nilOrOptions An optional dictionary of options with which to configure the persistent store. If `nil`, a dictionary of options enabling `NSMigratePersistentStoresAutomaticallyOption` and `NSInferMappingModelAutomaticallyOption` will be used.
@param error On input, a pointer to an error object. If an error occurs, this pointer is set to an actual error object containing the error information. You may specify nil for this parameter if you do not want the error information.
@bug Note that when built for iOS, this method will set a resource value on the SQLite file backing the persistent store for the `NSURLIsExcludedFromBackupKey` key set to `YES` to exclude the SQLite file from being backed up by iCloud to conform with the ["iOS Data Storage Guidelines"](https://developer.apple.com/icloud/documentation/data-storage/)
@warning If the seed database at the given path was created with an incompatible managed object model an application error may be raised.
*/
- (NSPersistentStore *)addSQLitePersistentStoreAtPath:(NSString *)storePath

View File

@@ -116,6 +116,7 @@ static RKManagedObjectStore *defaultStore = nil;
if (! self.persistentStoreCoordinator) [self createPersistentStoreCoordinator];
NSURL *storeURL = [NSURL fileURLWithPath:storePath];
if (seedPath) {
BOOL success = [self copySeedDatabaseIfNecessaryFromPath:seedPath toPath:storePath error:error];
if (! success) return nil;
@@ -143,7 +144,23 @@ static RKManagedObjectStore *defaultStore = nil;
if (! [self.persistentStoreCoordinator removePersistentStore:persistentStore error:error]) return nil;
NSDictionary *seedOptions = @{ RKSQLitePersistentStoreSeedDatabasePathOption: (seedPath ?: [NSNull null]) };
return [self.persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nilOrConfigurationName URL:storeURL options:seedOptions error:error];
persistentStore = [self.persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nilOrConfigurationName URL:storeURL options:seedOptions error:error];
if (! persistentStore) return nil;
/**
Exclude the SQLite database from iCloud Backups to conform to the iCloud Data Storage Guidelines
See https://developer.apple.com/icloud/documentation/data-storage/
*/
#if __IPHONE_OS_VERSION_MIN_REQUIRED
BOOL success = [storeURL setResourceValue:@(YES) forKey:NSURLIsExcludedFromBackupKey error:error];
if (!success) {
RKLogError(@"Failed to exclude SQLite store at path '%@' from iCloud Backup: %@", storePath, *error);
return nil;
}
#endif
return persistentStore;
}
- (BOOL)copySeedDatabaseIfNecessaryFromPath:(NSString *)seedPath toPath:(NSString *)storePath error:(NSError **)error

View File

@@ -236,4 +236,19 @@
assertThat(array, is(empty()));
}
#if __IPHONE_OS_VERSION_MIN_REQUIRED
- (void)testThatAddingASQLiteStoreExcludesThePathFromiCloudBackups
{
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] init];
NSError *error;
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"TestBackupExclusion.sqlite"];
[managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:nil withConfiguration:nil options:nil error:&error];
NSURL *storeURL = [NSURL fileURLWithPath:storePath];
id resourceValue = nil;
BOOL success = [storeURL getResourceValue:&resourceValue forKey:NSURLIsExcludedFromBackupKey error:&error];
expect(success).to.beTruthy();
expect(resourceValue).to.equal(@(YES));
}
#endif
@end