Ios 停止核心数据与iCloud同步

Ios 停止核心数据与iCloud同步,ios,objective-c,core-data,icloud,Ios,Objective C,Core Data,Icloud,为什么大家, 我正在制作一个应用程序,利用核心数据和iCloud在他的设备上同步用户文件,我设法实现了核心数据同步,现在我想添加一个开关,让用户打开和关闭iCloud同步,我正在尝试使用下一个代码方法停止核心数据sqlite数据库文件的同步: - (void)migrateiCloudStoreToLocalStore:(NSNotification *)notification{ [self addSkipBackupAttributeToItemAtURL:[[[NSFileMana

为什么大家,

我正在制作一个应用程序,利用核心数据和iCloud在他的设备上同步用户文件,我设法实现了核心数据同步,现在我想添加一个开关,让用户打开和关闭iCloud同步,我正在尝试使用下一个代码方法停止核心数据sqlite数据库文件的同步:

- (void)migrateiCloudStoreToLocalStore:(NSNotification *)notification{
    [self addSkipBackupAttributeToItemAtURL:[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]];

}

- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL {

    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"iCloudOn"]) {
        NSLog(@"nsuserdefaults iCloud value is on");
    }

    assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);

    NSError *error = nil;
    BOOL success = [URL setResourceValue: [NSNumber numberWithBool: [[NSUserDefaults standardUserDefaults] boolForKey:@"iCloudOn"]]
                                  forKey: NSURLIsExcludedFromBackupKey error: &error];
    if(!success){
        NSLog(@"Error excluding %@ from backup %@", [URL lastPathComponent], error);
    }
    return success;
}
我还绑定传递url,如下所示:

NSString *dataFileName = @"APP.sqlite";
 NSURL *localStore = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:dataFileName];
但应用程序在断言中停止

这是我在AppDelegate.m中的持久存储协调器:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
    if((_persistentStoreCoordinator != nil)) {
        return _persistentStoreCoordinator;
    }

    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
    NSPersistentStoreCoordinator *psc = _persistentStoreCoordinator;

    // Set up iCloud in another thread:

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        // ** Note: if you adapt this code for your own use, you MUST change this variable:
        NSString *iCloudEnabledAppID = @"APPID";

        // ** Note: if you adapt this code for your own use, you should change this variable:
        NSString *dataFileName = @"APPNAME.sqlite";

        // ** Note: For basic usage you shouldn't need to change anything else

        NSString *iCloudDataDirectoryName = @"Data.nosync";
        NSString *iCloudLogsDirectoryName = @"Logs";
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSURL *localStore = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:dataFileName];
        NSURL *iCloud = [fileManager URLForUbiquityContainerIdentifier:nil];

        if (iCloud) {

            NSLog(@"iCloud is working");

            NSURL *iCloudLogsPath = [NSURL fileURLWithPath:[[iCloud path] stringByAppendingPathComponent:iCloudLogsDirectoryName]];

            NSLog(@"iCloudEnabledAppID = %@",iCloudEnabledAppID);
            NSLog(@"dataFileName = %@", dataFileName);
            NSLog(@"iCloudDataDirectoryName = %@", iCloudDataDirectoryName);
            NSLog(@"iCloudLogsDirectoryName = %@", iCloudLogsDirectoryName);
            NSLog(@"iCloud = %@", iCloud);
            NSLog(@"iCloudLogsPath = %@", iCloudLogsPath);

            if([fileManager fileExistsAtPath:[[iCloud path] stringByAppendingPathComponent:iCloudDataDirectoryName]] == NO) {
                NSError *fileSystemError;
                [fileManager createDirectoryAtPath:[[iCloud path] stringByAppendingPathComponent:iCloudDataDirectoryName]
                       withIntermediateDirectories:YES
                                        attributes:nil
                                             error:&fileSystemError];
                if(fileSystemError != nil) {
                    NSLog(@"Error creating database directory %@", fileSystemError);
                }
            }

            NSString *iCloudData = [[[iCloud path]
                                     stringByAppendingPathComponent:iCloudDataDirectoryName]
                                    stringByAppendingPathComponent:dataFileName];

            NSLog(@"iCloudData = %@", iCloudData);

            options = [NSMutableDictionary dictionary];
            [options setObject:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption];
            [options setObject:[NSNumber numberWithBool:YES] forKey:NSInferMappingModelAutomaticallyOption];
            [options setObject:iCloudEnabledAppID            forKey:NSPersistentStoreUbiquitousContentNameKey];
            [options setObject:iCloudLogsPath                forKey:NSPersistentStoreUbiquitousContentURLKey];

            [psc performBlockAndWait:^{
                [psc addPersistentStoreWithType:NSSQLiteStoreType
                                  configuration:nil
                                            URL:[NSURL fileURLWithPath:iCloudData]
                                        options:options
                                          error:nil];
            }];

        }
        else {
            NSLog(@"iCloud is NOT working - using a local store");
            options = [NSMutableDictionary dictionary];
            [options setObject:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption];
            [options setObject:[NSNumber numberWithBool:YES] forKey:NSInferMappingModelAutomaticallyOption];

            [psc performBlockAndWait:^{
                [psc addPersistentStoreWithType:NSSQLiteStoreType
                                  configuration:nil
                                            URL:localStore
                                        options:options
                                          error:nil];

            }];
        }

        dispatch_async(dispatch_get_main_queue(), ^{
            [[NSNotificationCenter defaultCenter] postNotificationName:@"SomethingChanged" object:nil userInfo:nil];
        });
    });

    return _persistentStoreCoordinator;

}

如果有人知道如何解决这个问题,我将不胜感激。

你不能只打开和关闭iCloud sync。您必须在iCloud之间迁移存储,这将影响其他设备。有一些API可以实现这一点,请首先查看
migratePersistentStore:toURL:withOptions
。或者在这里查看可能有帮助的示例应用程序