Note that there are some explanatory texts on larger screens.

plurals
  1. POTwo Contexts, 1 Persistent Store: Duplicate Fetched Entries
    primarykey
    data
    text
    <p>I am attempting to create a way for users to import contacts to their phone. How it works is this:</p> <ul> <li><p>There are two managed object contexts. The "real" context has the current data in their address book. The "other" context has incoming data from another source. Both share the same PersistentStoreCoordinator.</p></li> <li><p>I match people by e-mail, so if a contact in the "real" context matches one in the "other", I don't save the other. </p></li> <li><p>When I start the program, I have two entries in the "real" context that I can fetch fine.</p></li> <li><p>Then, I import two other contacts an add them to the "other" context.</p></li> <li><p>When I perform a fetch operation on the "other" context, I get FOUR results - two from the "real" context and two I just added to the "other" context.</p></li> <li><p>However, when I merge the changes, my scheme for detecting duplicates works.</p></li> </ul> <p>Is there something I'm missing with my understanding of Core Data? How can I make it so that my querying of the "other" context just returns the new results.</p> <p>The full code is really long, but here's the important part:</p> <pre><code>AppDelegate *appDel = (AppDelegate*)[[UIApplication sharedApplication] delegate]; // Check to see the original data NSManagedObjectContext *realContext = [appDel managedObjectContext]; NSFetchRequest *usersFetch= [[[NSFetchRequest alloc] init] autorelease]; [usersFetch setEntity:[NSEntityDescription entityForName:@"User" inManagedObjectContext:realContext]]; [usersFetch setSortDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"email" ascending:YES]]]; NSArray *users = [realContext executeFetchRequest:usersFetch error:&amp;error]; [usersFetch release]; NSLog(@"%@",users); // Returns 2 original objects already in database otherContext = [[NSManagedObjectContext alloc] init]; [otherContext setPersistentStoreCoordinator:[[appDel managedObjectContext] persistentStoreCoordinator]]; for (contacts in fetchedData){ User *newUser = (User*)[NSEntityDescription insertNewObjectForEntityForName:@"User" inManagedObjectContext:otherContext]; newUser.email = fetchedData.email; newUser.firstName = fetchedData.firstName; // etc. } NSFetchRequest *newUsersFetch = [[[NSFetchRequest alloc] init] autorelease]; [newUsersFetch setEntity:[NSEntityDescription entityForName:@"User" inManagedObjectContext:otherContext]]; [newUsersFetch setSortDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"email" ascending:YES]]]; NSLog(@"%@",[otherContext registeredObjects]); // 2 objects that were just added NSArray *newUsers = [otherContext executeFetchRequest:newUsersFetch error:&amp;error]; NSLog(@"%@",[otherContext registeredObjects]); // 4 objects - added AND original NSLog(@"Count: %i",[newUsers count]); // Count: 4 </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.
    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