1

我正在尝试设置我的 Core Data 数据库以与 iCloud 一起使用,我了解基本知识并遵循了一个关于如何设置它的好教程,有人知道可能出了什么问题吗?

我收到无法连接数据库的错误:

Unresolved error: Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa error 256.)" UserInfo=0x1c5de2a0 {NSUnderlyingException=unable to open database file, NSSQLiteErrorDomain=14}, { NSSQLiteErrorDomain = 14; NSUnderlyingException = "unable to open database file"; }

这是我返回持久存储的代码:

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

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"CategoryModel.sqlite"];

    NSError *error = nil;
    __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil 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:
         [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

         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();
    }

    //  iCloud support
    //
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{        
        NSString *iCloudEnabledAppID = @"app.ident.ifier";
        NSString *dataFileName = @"CategoryModel.sqlite";

        NSString *iCloudDataDirectoryName = @"Data.nosync";
        NSString *iCloudLogsDirectoryName = @"TransactionLogs";

        NSFileManager *fileManager = [NSFileManager defaultManager];

        //  Create directory
        //
        NSURL *storeURL = [fileManager URLForUbiquityContainerIdentifier:nil];
        NSURL *dataFolder = [storeURL URLByAppendingPathComponent:iCloudDataDirectoryName];
        NSURL *dataFile = [dataFolder URLByAppendingPathComponent:dataFileName];

        BOOL isDirectory = YES;
        NSError *directoryError;

        if (![fileManager fileExistsAtPath:dataFolder.path isDirectory:&isDirectory]) {

            if (![fileManager createDirectoryAtURL:dataFolder
                       withIntermediateDirectories:YES
                                        attributes:nil
                                             error:&directoryError]) {
                NSLog(@"Create Directory Error: %@, %@", directoryError, directoryError.userInfo);
            }

        }

        NSLog(@"Data file: %@", dataFile.path);

        //  Set persistent store coordinator
        //
        NSError *pscError;

        NSLog(@"Store URL path: %@", storeURL.path);

        NSDictionary *options = @{ NSPersistentStoreUbiquitousContentNameKey : iCloudEnabledAppID,
                                   NSPersistentStoreUbiquitousContentURLKey : storeURL,
                                   NSMigratePersistentStoresAutomaticallyOption : @YES,
                                   NSInferMappingModelAutomaticallyOption : @YES};

        id result = [__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                                               configuration:nil
                                                                         URL:storeURL
                                                                     options:options
                                                                       error:&pscError];

        if (!result) {
            NSLog(@"Unresolved error: %@, %@", pscError, pscError.userInfo);
        }

        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(iCloudChangesImported:)
                                                     name:NSPersistentStoreDidImportUbiquitousContentChangesNotification
                                                   object:nil];
    });

    return __persistentStoreCoordinator;
}

编辑

这是我的 managedObjectModel 方法(CategoryModel 是 .xcdatamodeld 的名称):

- (NSManagedObjectModel *)managedObjectModel
{
    if (__managedObjectModel != nil) {
        return __managedObjectModel;
    }
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"CategoryModel" withExtension:@"momd"];
    __managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    return __managedObjectModel;
}

编辑 2

用日志编辑:

2013-03-01 22:33:37.658 Clueless[1268:3e07] +[PFUbiquityLocation createUbiquityLocationForPath:withUbiquityRootPath:](821): CoreData: Ubiquity:  Unable to continue parse components of URL: /var/mobile/Library/Mobile Documents/app~ident~ifier/TransactionLogs/.baseline/current.nosync/app~ident~ifier/kySK3XJnAwbVqyiIiWGh3OEDvd2hq7_TYF6hZncfq24=/baseline.meta
Confused by: baseline.meta
2013-03-01 22:33:37.720 Clueless[1268:4d07] +[PFUbiquityLocation createUbiquityLocationForPath:withUbiquityRootPath:](821): CoreData: Ubiquity:  Unable to continue parse components of URL: /var/mobile/Library/Mobile Documents/app~ident~ifier/TransactionLogs/.baseline/app~ident~ifier/kySK3XJnAwbVqyiIiWGh3OEDvd2hq7_TYF6hZncfq24=/mobile.0543DD0C-48E2-5061-A42B-716AC22C02AE
Confused by: mobile.0543DD0C-48E2-5061-A42B-716AC22C02AE
2013-03-01 22:33:37.731 Clueless[1268:4d07] +[PFUbiquityLocation createUbiquityLocationForPath:withUbiquityRootPath:](821): CoreData: Ubiquity:  Unable to continue parse components of URL: /var/mobile/Library/Mobile Documents/app~ident~ifier/TransactionLogs/mobile.0543DD0C-48E2-5061-A42B-716AC22C02AE/app~ident~ifier/kySK3XJnAwbVqyiIiWGh3OEDvd2hq7_TYF6hZncfq24=
Confused by: kySK3XJnAwbVqyiIiWGh3OEDvd2hq7_TYF6hZncfq24=
2013-03-01 22:33:37.792 Clueless[1268:4903] +[PFUbiquityLocation createUbiquityLocationForPath:withUbiquityRootPath:](821): CoreData: Ubiquity:  Unable to continue parse components of URL: /var/mobile/Library/Mobile Documents/app~ident~ifier/TransactionLogs/.baseline/current.nosync/app~ident~ifier/kySK3XJnAwbVqyiIiWGh3OEDvd2hq7_TYF6hZncfq24=/baseline.gcmodel
Confused by: baseline.gcmodel
2013-03-01 22:33:37.800 Clueless[1268:4903] +[PFUbiquityLocation createUbiquityLocationForPath:withUbiquityRootPath:](821): CoreData: Ubiquity:  Unable to continue parse components of URL: /var/mobile/Library/Mobile Documents/app~ident~ifier/TransactionLogs/.baseline/current.nosync/app~ident~ifier/kySK3XJnAwbVqyiIiWGh3OEDvd2hq7_TYF6hZncfq24=/baseline.model
Confused by: baseline.model
2013-03-01 22:33:37.811 Clueless[1268:3e07] +[PFUbiquityLocation createUbiquityLocationForPath:withUbiquityRootPath:](821): CoreData: Ubiquity:  Unable to continue parse components of URL: /var/mobile/Library/Mobile Documents/app~ident~ifier/TransactionLogs/.baseline/app~ident~ifier/kySK3XJnAwbVqyiIiWGh3OEDvd2hq7_TYF6hZncfq24=/baseline.zip
Confused by: baseline.zip
2013-03-01 22:33:47.673 Clueless[1268:1103] Unresolved error: Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa error 256.)" UserInfo=0x1e546570 {NSUnderlyingException=unable to open database file, NSSQLiteErrorDomain=14}, {
    NSSQLiteErrorDomain = 14;
    NSUnderlyingException = "unable to open database file";
}

编辑 3

当我尝试从 developer.icloud.com 下载文件时,它不会下载,并且会向 Safari 控制台发送一条消息Failed to load resource: the server responded with a status of 409 (Conflict)

上传文件时,Safari 控制台会发出几个警告:

CloudOS:  Push notification has unknown topic: {"originating-dspsrid":"208020489","apsTopic":"5a5fc3a1fea1dfe3770aab71bc46d0aa8a4dad41","originating-peer":"4cfc48c6-a790-457c-6d80-02757cf88f07","originating-request":"ed063598-1501-40d7-aa53-0e305a2f0837"}
javascript-packed.js:132
Documents:  Local Rank has changed, updating the current visible Column.
    javascript-packed.js:132
CloudOS:  Push notification has unknown topic: {"originating-dspsrid":"208020489","apsTopic":"5a5fc3a1fea1dfe3770aab71bc46d0aa8a4dad41","originating-peer":"4cfc48c6-a790-457c-6d80-02757cf88f07","originating-request":"94622d8f-fe4d-4eb2-80b8-62f6fd6342cc"}
    javascript-packed.js:132
CloudOS:  Push notification has unknown topic: {"originating-dspsrid":"208020489","apsTopic":"5a5fc3a1fea1dfe3770aab71bc46d0aa8a4dad41","originating-peer":"4cfc48c6-a790-457c-6d80-02757cf88f07","originating-request":"5c73e9c2-952f-4452-b6a2-b86e7902c867"}
    javascript-packed.js:132
Documents:  Local Rank has changed, updating the current visible Column.

编辑[现在是什么数字]

记录持久存储时,NSLog(@"MOC: %@", self.managedObjectContext.persistentStoreCoordinator.persistentStores);

我得到以下输出:

2013-03-02 13:57:38.241 Clueless[2221:907] MOC: (
    "<NSSQLCore: 0x1c5da560> (URL: file://localhost/var/mobile/Applications/[some long number which looks like it should be private]/Documents/CategoryModel.sqlite)"
)

所以我猜测错误unable to connect to database被调用是因为只有本地持久存储存在

新错误

Safe save failed for file, error: Error Domain=NSCocoaErrorDomain Code=512 "The file upload timed out." UserInfo=0x1c5cb220 {NSLocalizedDescription=The file upload timed out.}

编辑 - 目录错误

我认为问题在于未创建目录。

以下日志表明在尝试传输文件时目录不存在。我想我应该能够解决这个问题,iCloud 可以工作:

Had trouble moving baseline from: file://localhost/private/var/mobile/Library/Mobile%20Documents/app/Data.nosync/.baseline/app~ident~ifier/kySK3XJnAwbVqyiIiWGh3OEDvd2hq7_TYF6hZncfq24=/mobile.0543DD0C-48E2-5061-A42B-716AC22C02AE to: file://localhost/private/var/mobile/Library/Mobile%20Documents/app~ident~ifier/Data.nosync/.baseline/app~ident~ifier/kySK3XJnAwbVqyiIiWGh3OEDvd2hq7_TYF6hZncfq24=/baseline.zip
Error: Error Domain=NSCocoaErrorDomain Code=512 "The operation couldn’t be completed. (Cocoa error 512.)" UserInfo=0x1f5a0bd0 {NSFileNewItemLocationKey=file://localhost/private/var/mobile/Library/Mobile%20Documents/app~ident~ifier/Data.nosync/.baseline/app~ident~ifier/kySK3XJnAwbVqyiIiWGh3OEDvd2hq7_TYF6hZncfq24=/mobile.0543DD0C-48E2-5061-A42B-716AC22C02AE, NSFileOriginalItemLocationKey=file://localhost/private/var/mobile/Library/Mobile%20Documents/app~ident~ifier/Data.nosync/.baseline/app~ident~ifier/kySK3XJnAwbVqyiIiWGh3OEDvd2hq7_TYF6hZncfq24=/baseline.zip, NSUnderlyingError=0x1f591e20 "The operation couldn’t be completed. (Cocoa error 260.)", NSURL=file://localhost/private/var/mobile/Library/Mobile%20Documents/app~ident~ifier/Data.nosync/.baseline/app~ident~ifier/kySK3XJnAwbVqyiIiWGh3OEDvd2hq7_TYF6hZncfq24=/baseline.zip}
userInfo: {
    NSFileNewItemLocationKey = "file://localhost/private/var/mobile/Library/Mobile%20Documents/app~ident~ifier/Data.nosync/.baseline/app~ident~ifier/kySK3XJnAwbVqyiIiWGh3OEDvd2hq7_TYF6hZncfq24=/mobile.0543DD0C-48E2-5061-A42B-716AC22C02AE";
    NSFileOriginalItemLocationKey = "file://localhost/private/var/mobile/Library/Mobile%20Documents/app~ident~ifier/Data.nosync/.baseline/app~ident~ifier/kySK3XJnAwbVqyiIiWGh3OEDvd2hq7_TYF6hZncfq24=/baseline.zip";
    NSURL = "file://localhost/private/var/mobile/Library/Mobile%20Documents/app~ident~ifier/Data.nosync/.baseline/app~ident~ifier/kySK3XJnAwbVqyiIiWGh3OEDvd2hq7_TYF6hZncfq24=/baseline.zip";
    NSUnderlyingError = "Error Domain=NSCocoaErrorDomain Code=260 \"The operation couldn\U2019t be completed. (Cocoa error 260.)\" UserInfo=0x1f5505b0 {NSURL=file://localhost/private/var/mobile/Library/Mobile%20Documents/app~ident~ifier/Data.nosync/.baseline/app~ident~ifier/kySK3XJnAwbVqyiIiWGh3OEDvd2hq7_TYF6hZncfq24=/mobile.0543DD0C-48E2-5061-A42B-716AC22C02AE, NSFilePath=/private/var/mobile/Library/Mobile Documents/app~ident~ifier/Data.nosync/.baseline/app~ident~ifier/kySK3XJnAwbVqyiIiWGh3OEDvd2hq7_TYF6hZncfq24=/mobile.0543DD0C-48E2-5061-A42B-716AC22C02AE, NSUnderlyingError=0x1f5c38c0 \"The operation couldn\U2019t be completed. No such file or directory\"}";
}
4

0 回答 0