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

それはiCloudでは一般的です。Apple は、呼び出しがブロックされる可能性があるため、バックグラウンド キューに入れることをお勧めします。Core Data が実際に iCloud サーバーに接続し、利用可能な新しいデータのダウンロードを開始する場所であるため、ブロックされます。通話は終了するまで続きません。ダウンロードするデータが大量にある場合は、しばらくお待ちください。

明確な理由がなくても、しばらく時間がかかることがあります。そのようにバラバラです。

最終的に、あなたが見ているのは、現在の iCloud の状況とほぼ同じです。バグを 1 つまたは 2 つ報告し、状況が改善されることを願っています。

于 2013-01-23T22:28:47.520 に答える
1

これは、テスト デバイスの 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 に答える