I've the following problem:
I've an app with a UITableView as root view. In this tableView are different entries and for each entry (which can be created by the user) I also add a persistentstore to my app.
So, when I start the app from scratch (no saved data etc.) and add an entry, the persistentStore is also created and when I click on the tableViewCell, I can save Data in this generated persistentStore. This works fine if I'm creating an entry and also watch the entry during one single run.
BUT
If I close the app and start again, click on the same entry, I get the following error message:
NSFetchRequest *allUsers = [[NSFetchRequest alloc] init];
[allUsers setAffectedStores:[NSArray arrayWithObject:[[self.tableViewContext persistentStoreCoordinator] persistentStoreForURL:storeURL]]];
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSArray initWithObjects:count:]: attempt to insert nil object at objects[0]'
So it seems that I want to add a store which is actually nil, because (I guess so) the system can not find any store under this given storeURL (which is the same as the one where I created a store in the first run).
So I guess the persistentStore isn't there in the second run, so it isn't saved properly in the first run where it was created.
So how can I save a persistentStore, after I've added it, to the persistentStoreCoordinator?
Update:
I think I've identified the main problem. The persistentStoreCoordinator has changed after the first run. So how do I save all these things (managedObjectContext, persistentStoreCoordinator) before closing the app?
Can somebody help me?
Update 2
Here is how I create the store:
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [searchPaths objectAtIndex:0];
NSString* storeFileName = [NSString stringWithFormat:@"%@",newItem.name];
NSURL *storeURL = [NSURL fileURLWithPath:[documentPath stringByAppendingPathComponent:storeFileName]];
NSError* saveToStoreError;
if(![[self.addContext persistentStoreCoordinator] addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&saveToStoreError]){
NSLog(@"Error: %@, %@", saveToStoreError, [saveToStoreError userInfo]);
abort();
}
else
{
NSError *saveStoreError;
if(![self.addContext save:&saveStoreError])
{
NSLog(@"Saving Data wasn't possible!");
}
}
Update 3 So, after a lot of testing here are my results:
So something is changing with a restart of my app. I think the error must be associated with the managedObjectContext, the persistentStoreCoordinator or the SAVING of the stores.
Should I change any settings in my app delegate for setting up the managedObjectContext or the persistentStoreCoordinator?
Thanks!
It was simply easy.
I didn't set addPersistentStoreWithType
after restart in a view where I tried to fetch data from this store.
Sorry for this post. Simply easy ;-) bangingmyheadonthewall