6

I'm stuck trying to add Core Data to my existing iOS project. I don't have an existing sql database but I created a data model. I followed the following tutorial: http://wiresareobsolete.com/wordpress/2009/12/adding-core-data-existing-iphone-projects/

It produces an error at the following code:

(NSPersistentStoreCoordinator *) persistentStoreCoordinator{
if (persistentStoreCoordinator != nil){
    return persistentStoreCoordinator;
}
NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentDirectory]
           stringByAppendingPathComponent: @"MyPOC.sqlite"]];
NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc]
                              initWithManagedObjectModel:[self managedObjectModel]];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]){
    NSLog(@"Unresolved error OH NO %@, %@", error, [error userInfo]);
}
return persistentStoreCoordinator;
}

I get the following error:

2012-10-25 13:52:29.156 MyPOC[1994:11603] Unresolved error OH NO Error
Domain=NSCocoaErrorDomain Code=512 "The operation couldn’t be completed. 
(Cocoa error 512.)" UserInfo=0x8246240 {reason=Failed to create file; code = 2}, 
{reason = "Failed to create file; code = 2";}

I really have no idea why it crashes and how I can resolve it. If more information is needed to help please let me know.

2
  • check accepted answer for this question : stackoverflow.com/questions/6848394/… Commented Oct 25, 2012 at 13:24
  • 1
    You should check the value of storeUrl. The error code most probably means that the directory in which you want to create the SQLite file does not exist. Commented Oct 25, 2012 at 18:40

2 Answers 2

16

I was following nearly the same tutorial and receiving the same error message. I figured out what the problem is though.

In my case, the file could not be created because the storeURL path was incorrect and pointing to a folder that didn't exist.

In my helper method [self applicationDocumentDirectory] (which you did not provide in your question) I typed in the sample code just like in the tutorial:

- (NSString *) applicationDocumentsDirectory
{
    return [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) lastObject];
}

which produced the URL file:///Users/xxxxxx/Library/Application%20Support/iPhone%20Simulator/7.0.3/Applications/A6FDD8DB-475B-4C9B-B995-28CCE068DF82/Library/Documentation/

There is a silly code completion typo in there which generates an invalid path. Can you see it?

It's the enum NSDocumentationDirectory for the NSSearchPathDirectory input parameter, it should be NSDocumentDirectory instead.

The correct code and URL is:

- (NSString *) applicationDocumentsDirectory
{
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject;
}

URL: file:///Users/xxxxxx/Library/Application%20Support/iPhone%20Simulator/7.0.3/Applications/A6FDD8DB-475B-4C9B-B995-28CCE068DF82/Documents/

Sign up to request clarification or add additional context in comments.

3 Comments

I found my old code somewhere stored on my pc and looked into it. You are totally correct! Good one to spot the error!
Sufferin' succotash! Nice spot. For those who’s eyes are as blurry as mine from working on iCloud matters: NSDocumentDirectory (NOT NSDocumentationDirectory). Thanks.
Excellent catch. This is actually still in Apple's official core data documentation to this day, and is still a blatant error.
0

to close this question. i started a new core data project and imported my other code and it worked. Never got it to work in other project. Probably forgot something but did not find what

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.