user1031872
user1031872

Reputation: 1

Core Data: Not able to add persistent store to coordinator

I am making an app that displays organisms based around certain properties, such as Name, Color, Weight, etc. but those properties won't be known until run time. Which is why I am creating the NSManagedObjectModel programmatically. It seems I am able to initialize the model properly, as I use NSLog statement to show the organism entity's properties. But when I call the method:

[orgPersistentStoreCoordinator 
addPersistentStoreWithType:NSSQLiteStoreType                                        
configuration:nil URL:storUrl options:nil error:&error]

It returns NO and I am getting an error in my persistent store coordinator method that says:

"Could not add persistent store:The operation couldn’t be completed. (Cocoa error 256.)"

I have tried turning these options on, but I still got the same error:

NSString * const NSMigratePersistentStoresAutomaticallyOption;
NSString * const NSInferMappingModelAutomaticallyOption;

Here is the full code:

- (NSPersistentStoreCoordinator *)orgPersistentStoreCoordinator {

if (orgPersistentStoreCoordinator != nil) {
    return orgPersistentStoreCoordinator;
}
NSURL *storUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory]
                                          stringByAppendingPathComponent:@"organisms.sqlite"]];
NSError *error = nil;

orgPersistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc]
                              initWithManagedObjectModel:[self orgManagedObjectModel]];

NSLog(@"Entities: %@",[[self orgManagedObjectModel] entities]);

if (![orgPersistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType 
                                                 configuration:nil URL:storUrl options:nil error:&error]) {
        /*Error for store creation should be handled in here*/
    NSLog(@"Could not add persistent store:%@",[error localizedDescription]);
}

return orgPersistentStoreCoordinator;

}

And here is a snippet of how I am creating the ManagedObjectModel programmatically.

+ (NSManagedObjectModel *) orgManagedObjectModel 
{
FieldGuide *f = [FieldGuide sharedFieldGuide];



NSManagedObjectModel *orgModel = [[NSManagedObjectModel alloc]init];

NSEntityDescription *organismEntity = [[NSEntityDescription alloc]init];
[organismEntity setName:@"OrganismCoreData"];
[organismEntity setManagedObjectClassName:@"OrganismCoreData"];
[orgModel setEntities:[NSArray arrayWithObject:organismEntity]];


NSMutableArray *orgEntityProperties = [[NSMutableArray alloc]init];

 // 1. Name attributes
for (NameParameter *n in f.nameAttributes) 
{
    NSAttributeDescription *nameAttr = [[NSAttributeDescription alloc]init];
    [nameAttr setName:n.name];
    [nameAttr setAttributeType:NSStringAttributeType];
    [orgEntityProperties addObject:nameAttr];
}
[organismEntity setProperties:orgEntityProperties];


return orgModel;
}

Finally, when I try to insert some organisms and save via the NSManagedObjectContext, the app terminates and displays this in the log:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'This NSPersistentStoreCoordinator has no persistent stores.  It cannot perform a save operation.'

Any help or directions would be greatly appreciated, thanks!

Edit: And yes, I have tried deleting the app from the simulator, clean target, and deleting the organisms.sqlite from the documents folder via finder.

Edit #2: I forgot to add that the addPersistentStore method also returns another error:

2012-04-30 14:13:46.109 biodex[7510:14803] CoreData: error: (1) I/O error for database at /Users/junryandelacruz/Library/Application Support/iPhone Simulator/5.1/Applications/ACE957AF-2FC0-4EAF-B226-5019F70FAB90/Documents/organisms.sqlite.  SQLite error code:1, 'near "OR": syntax error'

Upvotes: 0

Views: 1992

Answers (1)

Mundi
Mundi

Reputation: 80265

Looks like there is a mistake in your storeURL. This is the "Unknown File Read Error", usually to do with the path to the file.

From the documentation:

NSFileReadUnknownError = 256,

The second error certainly has nothing to do with adding the persistent store. Check your code for the "OR" predicate.

Upvotes: 1

Related Questions