Note that there are some explanatory texts on larger screens.

plurals
  1. POManaging multiple NSPersistentStores with PersistentStoreCoodinator
    primarykey
    data
    text
    <p>I am trying to get NSPersistentStoreCoordinator to manage the deletion and insertion of multiple persistent stores. So far I have managed to configure the PSC with two stores and I have been able to remove either store by specifying its index. </p> <p>Like this…</p> <pre><code>NSPersistentStore *store = [[self.persistentStoreCoordinator persistentStores] objectAtIndex:0]; if (![self.persistentStoreCoordinator removePersistentStore:store error:&amp;error]) { NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } [[NSFileManager defaultManager] removeItemAtURL:store.URL error:&amp;error]; </code></pre> <p>But I'm finding that when I add the store back in to the PSC the index value is incorrect and it cannot be specified with the existing class methods. The consequence of this is that the new data is downloaded and added to the wrong store.</p> <p>Does anyone have any suggestions on how to this should be done?</p> <p><strong>Background</strong> (Updated)</p> <p>The reason for using two persistent stores is so that I can designate a unique store for the two xml documents that will be downloaded over the network. As both these files are relatively large I am hoping to reduce network traffic. So a check is done to see if either file has been modified. If they have then the corresponding persistent store is deleted and a new store added. It's at this point the problem starts. Adding a new store always adds it to the end of the Persistent Stores Array. This appears to create a mismatch in the stores when merging the data back with the MOC.</p> <p><strong>Code</strong> Here's what I've tried so far which removes and adds the persistent store but the new data is added to the wrong store.</p> <pre><code>static NSString * const kLastDateStoreUpdateKey = @"eventLastStoreUpdateKey"; - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { NSString *last_modified = [NSString stringWithFormat:@"%@",[[(NSHTTPURLResponse *)response allHeaderFields] objectForKey:@"Last-Modified"]]; NSDateFormatter *dFormatter = [[NSDateFormatter alloc] init]; [dFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss zzz"]; [dFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"]]; [dFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]]; dateModified = [dFormatter dateFromString:last_modified]; NSDate *previousDate = [[NSUserDefaults standardUserDefaults] objectForKey:kLastDateStoreUpdateKey]; if (!previousDate || [previousDate compare:dateModified] != NSOrderedSame) { [self.managedObjectContext lock]; [self.managedObjectContext reset]; if ([[NSFileManager defaultManager] fileExistsAtPath:self.persistentStorePath]) { NSError *error = nil; NSArray *stores = [self.persistentStoreCoordinator persistentStores]; NSURL *storeUrls = [NSURL fileURLWithPath:persistentStorePath]; for (NSPersistentStore *store in stores){ if ([[store.URL absoluteURL] isEqual:[storeUrls absoluteURL]]) { if (![self.persistentStoreCoordinator removePersistentStore:store error:&amp;error]) { NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } [[NSFileManager defaultManager] removeItemAtURL:store.URL error:&amp;error]; NSLog(@"Check store removed %@", [self.persistentStoreCoordinator persistentStores]); } } } NSURL *storeUrl = [NSURL fileURLWithPath:persistentStorePath]; NSError *error = nil; [self.persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&amp;error]; if (error) { NSLog(@"event error %@ %@",error, [[self.persistentStoreCoordinator persistentStores] objectAtIndex:0]); } NSLog(@"Check store added %@",self.persistentStoreCoordinator.persistentStores); [self.managedObjectContext unlock]; }else { [self cancelDownload]; NSLog(@"event cancel %@ %@ %@",previousDate, dateModified, [self.persistentStoreCoordinator persistentStores]); } } </code></pre> <p><strong>Solved</strong> As Hunter pointed out below my PSC Configuration was set to as nil. So data was not being added to each individual Persistent Store. When I created a Configuration in the Core Data Model and targeted the relevant Entities I set the PSC to have that configuration. It then worked as expected. See below.</p> <pre><code> NSURL *storeUrl = [NSURL fileURLWithPath:persistentStorePath]; NSError *error = nil; [self.persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:@"ConfigEvent" URL:storeUrl options:nil error:&amp;error]; if (error) { NSLog(@"event error %@ %@",error, [[self.persistentStoreCoordinator persistentStores] objectAtIndex:0]); } NSLog(@"Check store added %@",self.persistentStoreCoordinator.persistentStores); </code></pre>
    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