在我的 iOS 应用程序中,我已将我的数据库设置为与 iCloud 同步。当我插入一条新记录时,数据会正确更新,我可以在我的容器文件夹中观看它。 现在我想设置一个开关,让用户在应用程序运行时启用或禁用与 iCloud 的核心数据同步过程,但我不知道该怎么做。
我是 iOS 和 ICloud 的新手。请需要帮助。提前致谢。
这是我在 AppDelegate 中的代码:
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"myStore.sqlite"];
NSURL *cloudRootURL=[fileManager URLForUbiquityContainerIdentifier:nil];
NSString *pathToCloudFile = [[cloudRootURL path]stringByAppendingPathComponent:@"Documents"];
pathToCloudFile = [pathToCloudFile stringByAppendingPathComponent:@"myCloudLogs"];
NSURL *cloudURL = [NSURL fileURLWithPath:pathToCloudFile];
NSString *cloudStoreTitle = @"myStoreCloud";
NSDictionary *options = @{NSMigratePersistentStoresAutomaticallyOption : @YES,
NSInferMappingModelAutomaticallyOption : @YES,
NSPersistentStoreUbiquitousContentURLKey: cloudURL,
NSPersistentStoreUbiquitousContentNameKey: cloudStoreTitle};
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
//Replace this implementation with code to handle the error appropriately.
//abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
//Typical reasons for an error here include:
// The persistent store is not accessible;
// The schema for the persistent store is incompatible with current managed object model.
//Check the error message to determine what the actual problem was.
//If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.
//If you encounter schema incompatibility errors during development, you can reduce their frequency by:
// Simply deleting the existing store:
//[[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]
// Performing automatic lightweight migration by passing the following dictionary as the options parameter:
//@{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES}
//Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self
selector:@selector(processStoresWillChange:)
name:NSPersistentStoreCoordinatorStoresWillChangeNotification
object:_persistentStoreCoordinator];
[notificationCenter addObserver:self
selector:@selector(processStoresDidChange:)
name:NSPersistentStoreCoordinatorStoresDidChangeNotification
object:_persistentStoreCoordinator];
[notificationCenter addObserver:self
selector:@selector(processContentChanges:)
name:NSPersistentStoreDidImportUbiquitousContentChangesNotification
object:_persistentStoreCoordinator];
return _persistentStoreCoordinator;
}
最佳答案
不要使用无处不在的容器。查看苹果文档以从 icloud 同步中排除文件。 https://developer.apple.com/library/ios/qa/qa1719/_index.html如果将 bool 值设置为 NO。该文件将再次包含在要备份的文件中。在文档中创建核心数据(数据库)以使其自动同步。使用此代码,您可以随时通过单击按钮以编程方式禁用同步。但请注意,如果数据库大小增长太多,应用程序可能会被拒绝,因为只有“用户数据”应该与 icloud 同步。
关于iOS ICloud 核心数据从应用程序打开关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31427519/