In my app for iOS i have set my database for sync with iCloud. When i insert a new record the data is correctly updated and i can watch it at my container folder. Now I want to set a switch to let user to enable or disable the core data syncing proccess with iCloud while app is running, but i dont know how to do this.
I am new with iOS and ICloud. Need help please. Thanks in advance.
This is my code in 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;
}
Do not use ubiquity container. Have a look at apple documentation for excluding file from icloud sync. https://developer.apple.com/library/ios/qa/qa1719/_index.html If you set the bool value to NO. The file will be again included in files to backup. Create the core data(database) in documents for it to auto sync. Using this code you can disable sync programmatically anytime on click of a button. But be warned that the app may be rejected if the database size grows too much, because only "user data" should be syncing with icloud.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With