Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to save a PersistentStore
    primarykey
    data
    text
    <p>I've the following problem:</p> <p>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.</p> <p>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.</p> <p><strong>BUT</strong></p> <p>If I close the app and start again, click on the same entry, I get the following error message:</p> <pre><code>NSFetchRequest *allUsers = [[NSFetchRequest alloc] init]; [allUsers setAffectedStores:[NSArray arrayWithObject:[[self.tableViewContext persistentStoreCoordinator] persistentStoreForURL:storeURL]]]; </code></pre> <p><strong>* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*</strong> -[NSArray initWithObjects:count:]: attempt to insert nil object at objects[0]'</p> <p>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).</p> <p>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.</p> <p><strong>So how can I save a persistentStore, after I've added it, to the persistentStoreCoordinator?</strong></p> <p><strong>Update:</strong></p> <p>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?</p> <p>Can somebody help me?</p> <p><strong>Update 2</strong></p> <p>Here is how I create the store:</p> <pre><code> 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:&amp;saveToStoreError]){ NSLog(@"Error: %@, %@", saveToStoreError, [saveToStoreError userInfo]); abort(); } else { NSError *saveStoreError; if(![self.addContext save:&amp;saveStoreError]) { NSLog(@"Saving Data wasn't possible!"); } } </code></pre> <p><strong>Update 3</strong> So, after a lot of testing here are my results:</p> <ul> <li>Creation of the stores works fine. I can create multiple stores and I can access and populate them as long as I'm in a single run of my app.</li> </ul> <p>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.</p> <p>Should I change any settings in my app delegate for setting up the managedObjectContext or the persistentStoreCoordinator?</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload