应用程序不会将核心数据更改与iCloud同步

时间:2013-01-22 16:53:57

标签: iphone ios objective-c core-data icloud

我设置了一些NSLog以确保我的应用程序与iCloud同步更改,但我等了半个小时,并且没有同步更改。这是我对核心数据的代码。核心数据存储存储在文档目录中,因为如果它失去对iCloud的访问权限,我希望它继续工作,并且不希望它丢失数据库以便在该实例中丢失。

核心数据正常设置并对数据进行更改,但似乎与iCloud不同步。

- (void)mergeChangesFromiCloud:(NSNotification *)notification {
    NSLog(@"changes from iCloud");
    NSManagedObjectContext* moc = [self managedObjectContext];

    [moc performBlock:^{

        [moc mergeChangesFromContextDidSaveNotification:notification];

        NSNotification* refreshNotification = [NSNotification notificationWithName:@"SomethingChanged"
                                                                            object:self
                                                                          userInfo:[notification userInfo]];

        [[NSNotificationCenter defaultCenter] postNotification:refreshNotification];
    }];
}

#pragma mark - Core Data Objects

- (NSManagedObjectContext *)managedObjectContext
{
    if (_managedObjectContext != nil) {
        return _managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {

        _managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];

        [_managedObjectContext performBlockAndWait:^{
        [_managedObjectContext setPersistentStoreCoordinator:coordinator];
        [[NSNotificationCenter defaultCenter]addObserver:self
                                                selector:@selector(mergeChangesFromiCloud:)
                                                    name:NSPersistentStoreDidImportUbiquitousContentChangesNotification
                                                  object:coordinator];
        }];

    }
    return _managedObjectContext;
}

- (NSManagedObjectModel *)managedObjectModel {

    if (_managedObjectModel != nil) {
        return _managedObjectModel;
    }

    _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:DATABASE_MODEL_URL];

    return _managedObjectModel;
}

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

    NSURL *storeURL = [NSURL fileURLWithPath:STORE_PATH];

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

    NSMutableDictionary *options = [NSMutableDictionary dictionary];
    [options setObject:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption];
    [options setObject:[NSNumber numberWithBool:YES] forKey:NSInferMappingModelAutomaticallyOption];

    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSURL *iCloud = [fileManager URLForUbiquityContainerIdentifier:nil];
    NSLog(@"icloud: %@", iCloud);
    if (iCloud) {

        NSString *iCloudLogsDirectoryName = @"Logs";
        NSURL *iCloudLogsPath = [NSURL fileURLWithPath:[[iCloud path] stringByAppendingPathComponent:iCloudLogsDirectoryName]];

        //Create logs directory, in case it doesn't exist
        if([fileManager fileExistsAtPath:[iCloudLogsPath path]] == NO) {
            NSLog(@"logs directory doesn't exist");
            NSError *fileSystemError;
            [fileManager createDirectoryAtPath:[[iCloud path] stringByAppendingPathComponent:iCloudLogsDirectoryName]
                   withIntermediateDirectories:YES
                                    attributes:nil
                                         error:&fileSystemError];
            if(fileSystemError != nil) {
                NSLog(@"Error creating database directory %@", fileSystemError);
            }

        NSString *iCloudEnabledAppID = @"ID REMOVED FROM STACK OVERFLOW";

        [options setObject:iCloudEnabledAppID            forKey:NSPersistentStoreUbiquitousContentNameKey];
        [options setObject:iCloudLogsPath                forKey:NSPersistentStoreUbiquitousContentURLKey];

        [_persistentStoreCoordinator lock];

        [_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                          configuration:nil
                                    URL:storeURL
                                options:options
                                  error:nil];

        [_persistentStoreCoordinator unlock];

        }

        else {

            [_persistentStoreCoordinator lock];

            [_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                              configuration:nil
                                        URL:storeURL
                                    options:options
                                      error:nil];

            [_persistentStoreCoordinator unlock];
        }

    }

    [[NSNotificationCenter defaultCenter] postNotificationName:@"SomethingChanged" object:self userInfo:nil];


    return _persistentStoreCoordinator;
}

编辑:问题在于,就括号而言,addPersistantStore代码位于错误的位置。应该是一个较低的支架。

0 个答案:

没有答案