1

I am trying to setup my Core Data database for use with iCloud, I understand the basic and followed a nice tutorial on how to set it up, does anyone know what might be going wrong?

I am getting the error that the database could not be connected to:

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"; }

Here is my code to return the persistent store:

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

edit

Here is my managedObjectModel method (CategoryModel is the name of the .xcdatamodeld):

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

edit 2

Edited with Log:

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";
}

Edit 3

When I try downloading the file from developer.icloud.com, it does not download and a message is sent to the Safari console Failed to load resource: the server responded with a status of 409 (Conflict)

When a file is uploaded, several warnings are issued in the Safari console:

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.

Edit [whatever number it is now]

When logging the persistent stores, NSLog(@"MOC: %@", self.managedObjectContext.persistentStoreCoordinator.persistentStores);

I get the following output:

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)"
)

So Im guessing the error unable to connect to database is called because only the local persistent store is there

New error

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

Edit - error with directory

I think the issue is with a directory not being created.

The following log says a directory is not there, when trying to transfer a file. I think should I be able to fix this, iCloud will work:

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\"}";
}
Adam Carter
  • 4,741
  • 5
  • 42
  • 103
  • Are you letting Core Data create the database or do you already have a SQLLite file you are trying to use? – radesix Mar 01 '13 at 21:39
  • I don't have a SQLite database, just the model which on first load is populated with data from a JSON file – Adam Carter Mar 01 '13 at 21:40
  • What does your [self managedObjectModel] method look like? – radesix Mar 01 '13 at 21:46
  • Added to post in edit – Adam Carter Mar 01 '13 at 21:49
  • A couple of thoughts... If I'm not mistaken the .momd extension only occurs after the datamodel has been versioned. Prior to any versioning I think it's just .mom. That being said, you can try this line instead: managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil]; If this is correct, let me know and I'll post this as a solution so you can accept. – radesix Mar 01 '13 at 21:51
  • Thanks, but this doesn't work either - I cant see why a lot of people don't bother with iCloud now though... any other ideas? – Adam Carter Mar 01 '13 at 22:06
  • Have you deleted your app, cleaned, and deleted derived data to start from scratch? The problem seems to be before your iCloud code but the setup of the model and persistent coordinator looks good. – radesix Mar 01 '13 at 22:28
  • Yeah, I've cleaned, deleted and quit everything and built from scratch, still nothing. I've added an update with the log – Adam Carter Mar 01 '13 at 22:34
  • Looking on http://developer.icloud.com, when I am supposed to update a record, a .cdt file gets put in the folder, [well pending], its only 1KB, but it shows something works... – Adam Carter Mar 01 '13 at 23:36
  • *sometimes pends, sometimes uploads – Adam Carter Mar 01 '13 at 23:43
  • Does anyone have any ideas? – Adam Carter Mar 02 '13 at 00:41
  • Hi @radesix, I believe the final edit ("Edit - error with directory") will work, should the log error be resolved. Do you have any ideas? – Adam Carter Mar 02 '13 at 15:01
  • My first bet would be there is something wrong with the path, and more specifically the folder app~ident~ifier. – radesix Mar 04 '13 at 15:13
  • It turns out, the problem was with persistent stores. When I have one store with nil config, it saves. But this is not a great model, as only two of my entities need to be synced. For some reason when I try adding two stores, the data is not synced – Adam Carter Mar 04 '13 at 15:35

0 Answers0