22

This is my first time with core data, and I am getting the following error.

I would really appreciate it if you could tell me how to fix it.

Unresolved error Error Domain=NSCocoaErrorDomain Code=134100 "The operation couldn’t be completed. (Cocoa error 134100.)" UserInfo=0x5927880 {metadata=<CFBasicHash 0x59269a0 [0x1007400]>{type = immutable dict, count = 7,
entries =>
    2 : <CFString 0x5926e10 [0x1007400]>{contents = "NSStoreModelVersionIdentifiers"} = <CFArray 0x5927240 [0x1007400]>{type = immutable, count = 0, values = ()}
    4 : <CFString 0x5927190 [0x1007400]>{contents = "NSPersistenceFrameworkVersion"} = <CFNumber 0x5926ca0 [0x1007400]>{value = +320, type = kCFNumberSInt64Type}
    6 : <CFString 0x59271c0 [0x1007400]>{contents = "NSStoreModelVersionHashes"} = <CFBasicHash 0x5927340 [0x1007400]>{type = immutable dict, count = 2,
entries =>
    0 : <CFString 0x5927280 [0x1007400]>{contents = "Details"} = <CFData 0x59272f0 [0x1007400]>{length = 32, capacity = 32, bytes = 0x434e180241ecf461e59580e640ff926b ... aa456d1410ed9d1b}
    2 : <CFString 0x5927260 [0x1007400]>{contents = "History"} = <CFData 0x59272a0 [0x1007400]>{length = 32, capacity = 32, bytes = 0x3437f77a5563363f66b9d72ea76e0ff1 ... be194eb9dd17cddc}
}

    7 : <CFString 0xe238b0 [0x1007400]>{contents = "NSStoreUUID"} = <CFString 0x5926fc0 [0x1007400]>{contents = "6C5FECCB-0B64-46EB-809B-E0A4577D1E90"}
    8 : <CFString 0xe23720 [0x1007400]>{contents = "NSStoreType"} = <CFString 0xe238f0 [0x1007400]>{contents = "SQLite"}
    9 : <CFString 0x59271f0 [0x1007400]>{contents = "NSStoreModelVersionHashesVersion"} = <CFNumber 0x4d13e20 [0x1007400]>{value = +3, type = kCFNumberSInt32Type}
    10 : <CFString 0x5927220 [0x1007400]>{contents = "_NSAutoVacuumLevel"} = <CFString 0x5927380 [0x1007400]>{contents = "2"}
}
, reason=The model used to open the store is incompatible with the one used to create the store}, {
    metadata =     {
        NSPersistenceFrameworkVersion = 320;
        NSStoreModelVersionHashes =         {
            Details = <434e1802 41ecf461 e59580e6 40ff926b 20004ce9 58eb965f aa456d14 10ed9d1b>;
            History = <3437f77a 5563363f 66b9d72e a76e0ff1 ed7f70fd cb7035e9 be194eb9 dd17cddc>;
        };
        NSStoreModelVersionHashesVersion = 3;
        NSStoreModelVersionIdentifiers =         (
        );
        NSStoreType = SQLite;
        NSStoreUUID = "6C5FECCB-0B64-46EB-809B-E0A4577D1E90";
        "_NSAutoVacuumLevel" = 2;
    };
    reason = "The model used to open the store is incompatible with the one used to create the store";
}
sharedlibrary apply-load-rules all
Current language:  auto; currently objective-c
kill
quit
Program ended with exit code: 0

My code is here: https://gist.github.com/898579

BenMorel
  • 34,448
  • 50
  • 182
  • 322
max_
  • 24,076
  • 39
  • 122
  • 211

6 Answers6

65

Here's the reason:

The model used to open the store is incompatible with the one used to create the store

And here's how you did it:

  1. You created some entities with some attributes and wrote some code
  2. Launched the app, probably added some content
  3. Quit the app and added/changed some more entities with attributes
  4. You probably launched the app again and now it's giving you the error

The reason for this is because your new managed object model is trying to use older version of storage (the one first time created when you launched the app).

The quick and dirty fix would be to remove the storage file (somewhere in ~/Library/Application Support/YOUR_APP/) and to launch your app again.

For future reference - if you release an app and in next release the app has changed managed object model - you have to write migrations for it. All this and more is covered in core data programming cookbook in apple documentation.

Eimantas
  • 48,927
  • 17
  • 132
  • 168
  • 2
    I am working in a team of two - when one of us installs onto the phone where the app has previously been installed hy the other, we get this error. The data model / structure hasn't changed. Is this related to the identity of the user signing the app? – Marc May 15 '12 at 10:34
  • Don't know that one. It could be affected by the GUID of the app. – Eimantas May 15 '12 at 10:40
  • Odd - we're using SVN as version control. I am concerned that if another developer was to take over the app after me, they might release to the app store with this crash. Will keep this thread posted if I find out why it is happening. – Marc May 16 '12 at 08:11
  • Perfect! Thanks by your solution. I've deleted the old database and all ok now! – Marcelo dos Santos Jan 11 '19 at 15:51
14

It should be enough if you remove the app from your simulator/device.

You don't have to change the files in your project (except choosing a current model and replacing the classes using menu: Editor/Create NSManaged Object Subclass).

The iOSDev
  • 5,237
  • 7
  • 41
  • 78
Wojtek
  • 141
  • 2
  • 4
1

You should do following steps

  1. Delete the application and Run it again, if it still shows the same error. Then it means that, you used the specific attributes/attribute with different type.
  2. Go to your .xcdatamodeled file and check the type of each attributes.
  3. Go to your code and check, while inserting the attributes/objects in core data, you used the same type of different. So, the point is type(NSString, NSDate...) of the attributes/objects in your code and .xcdatamodeled should be same. If not then it will give the error "Error Domain=NSCocoaErrorDomain Code=134100"
Eimantas
  • 48,927
  • 17
  • 132
  • 168
GameBegins
  • 580
  • 7
  • 12
0

Sometimes you need to load data from a store created by another application - e.g. one app is used just for loading data from external source and for saving into the store and the othe app uses this store full of data as a starting point.

I don`t want to say it is super-correct but in case you need it from time to time during the app development: just change the UUID in the store (by SQLVue etc.) to the right one which is expected by your app. You will find the UUID in the table called in most cases ZMETADATA in the only one column called Z_UUID. But remember - just for quick debugging / development purposes. Do not use this programmatically.

Vitek
  • 31
  • 6
  • How would I do this programmatically, e.g. when I have copied a UIManagedDocument and want to use its copy? – AlexR Jul 07 '13 at 15:42
0

If you are in developing mode and don't want to delete the app each time you change the entities I recommend to use this solution:

- (void)removeCoreDataStorage {
    // Where does the SQLite file go?
    NSArray *documentDirectories =
    NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                        NSUserDomainMask,
                                        YES);
    // Get one and only document directory from that list
    NSString *documentDirectory = [documentDirectories firstObject];
    NSString *path = [documentDirectory stringByAppendingPathComponent:@"model.sqlite"];

    NSError *error = nil;
    NSURL *storeURL = [NSURL fileURLWithPath:path];

    NSURL *storeURLWal = [NSURL URLWithString:[storeURL.absoluteString stringByReplacingOccurrencesOfString:@".sqlite" withString:@".sqlite-wal"]];
    NSURL *storeURLShm = [NSURL URLWithString:[storeURL.absoluteString stringByReplacingOccurrencesOfString:@".sqlite" withString:@".sqlite-shm"]];

    BOOL isRemoveItemAtURL = [[NSFileManager defaultManager] removeItemAtURL:storeURL error:&error];
    if (isRemoveItemAtURL == NO) {
        NSLog(@"NO RemoveItemAtURL. Reason: %@", error.localizedFailureReason);
    }

    BOOL isRemoveItemAtURLWal = [[NSFileManager defaultManager] removeItemAtURL:storeURLWal error:&error];
    if (isRemoveItemAtURLWal == NO) {
        NSLog(@"NO RemoveItemAtURL. Reason: %@", error.localizedFailureReason);
    }

    BOOL isRemoveItemAtURLShm = [[NSFileManager defaultManager] removeItemAtURL:storeURLShm error:&error];
    if (isRemoveItemAtURLShm == NO) {
        NSLog(@"NO RemoveItemAtURL. Reason: %@", error.localizedFailureReason);
    }
}

Don't forget to change the path with your own path.

Soheil Novinfard
  • 1,358
  • 1
  • 16
  • 43
0

It happens with me as well and by removing STORE URL I fixed my this with bellow code - [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]

Clarification : delete the file at the storeURL and recreate it, if we don’t want data to stick around between tests. Since the new tests have a new model version and the old file couldn’t be opened properly. Also if we changed managed object model - we have to write migrations for it.