Search code examples
iphonecore-datainsertdistinct-values

How to insert distinct records through core data in iPhone?


I want to insert only those records in core data in iPhone which are not already present in the sqllite table. In other words, I want to insert distinct records in my core data table. My code of insertion is

for(NSInteger j=0;j<[items count];j++){
            Story *story=(Story *)[NSEntityDescription insertNewObjectForEntityForName:@"Story" inManagedObjectContext:managedObjectContext];

            [story setTitle:[[items objectAtIndex:j] objectForKey:@"title"]];
            [story setDate:[[items objectAtIndex:j] objectForKey:@"date"]];
     }

Tell me the way to insert only distinct records in this.


Solution

  • Have a look at this page, the section 'Implementing Find-or-Create Efficiently' gives detailed info on how to implement an update/insert mechanism.