Cannot add sql file when adding master data to my project

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

It throws an error in 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 don't know why it crashes and how I can solve it. If you need more information to help, please let me know.

+4
source share
2 answers

I followed the same tutorial and received the same error message. I found out what the problem is.

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

In my helper method [self applicationDocumentDirectory] (which you did not specify in your question), I typed an example code in the same way as in the tutorial:

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

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

There is a typo of stupid code that generates an invalid path. You see?

It lists the NSDocumentationDirectory for the input parameter NSSearchPathDirectory, instead it should be NSDocumentDirectory.

The correct code and URL:

 - (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 /

+12
source

to close this question. I started a new project with basic data and imported my other code and it worked. Never forced him to work in another project. Perhaps I forgot something, but did not find that

0
source

Source: https://habr.com/ru/post/1442030/


All Articles