2

我的persistentStoreCoordinator 中有以下代码。没有 iCloud 部分,它工作正常。对于 iCloud,它在 addPersistentStoreWithType 方法上停止。没有错误,它只是停止并且不会继续。

有什么想法吗?

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

    NSURL *storeURL = [NSURL fileURLWithPath:STORE_PATH];

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


    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    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 logs directory %@", fileSystemError);
                }
            }

            NSString *iCloudEnabledAppID = @"app id removed from stackoverflow";

            [options setObject:iCloudEnabledAppID            forKey:NSPersistentStoreUbiquitousContentNameKey];
            [options setObject:iCloudLogsPath                forKey:NSPersistentStoreUbiquitousContentURLKey];
            NSLog(@"logs path: %@", iCloudLogsPath);
        }

    [_persistentStoreCoordinator lock];

    NSError *error;

    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                                   configuration:nil
                                                             URL:storeURL
                                                         options:options
                                                           error:&error]) {

        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
    else {
        NSLog(@"done adding persistent store");
    }

    [_persistentStoreCoordinator unlock];

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

    });

    return _persistentStoreCoordinator;

}
4

3 回答 3

2

That's common with iCloud. Apple advises you to put it on a background queue since that call may block. It blocks because that's where Core Data actually makes its connection to the iCloud server and starts downloading any new data that's available. The call doesn't continue until it's finished. If there's any significant amount of data to download, you may wait a while.

Sometimes it takes a while for no readily apparent reason. It's flaky like that.

Ultimately what you're seeing is pretty much the way things are with iCloud right now. File a bug or two and hope that things improve.

于 2013-01-23T22:28:47.520 回答
1

我刚刚在我的一台测试设备(运行 iOS 6.1 的 iPhone 5)上体验到了这一点。恢复出厂设置,然后从手机的当前备份恢复修复它。

我不确定这是不是解释,但通过设备设置 > iCloud > 存储和备份删除应用程序的 iCloud 普遍存在容器数据似乎存在问题。这可能会使设备处于普遍存在容器的一些残余物仍然存在且无法移除的状态。在设备上进行完全擦除和恢复似乎可以修复它。

我发现了一篇看起来与此问题相关的旧帖子(http://stackmonthly.com/2011/11/core-data),以及 OpenRadar 上的此条目(http://openradar.appspot.com/10440734)。我搞砸了几个小时,直到我擦除并恢复了设备,它运行良好:addPersistentStoreWithType 几乎立即完成。

展望未来,我将尝试使用 WWDC 2012 示例应用程序 SharedCoreData(会话 227)中的 nukeAndPave 方法清除存储,而不是从设置中手动删除存储。希望这将防止这种情况再次发生。

于 2013-02-07T03:51:15.733 回答
0

我不完全确定为什么会出现这些问题,但通过重置手机设置,我在解决此类问题方面取得了很大成功:

设置 > 通用 > 重置 > 重置所有设置

于 2014-04-07T20:58:40.023 回答