I have an app that allows users to save favorites. I am using Core Data to store the favorites as managed objects. I have written some code to prevent the possibility of storing
CoreData does no uniquing by itself. It has no notion of two entries being identical.
To enable such a behavior you have to implement it yourself by doing a 'search before insert' aka a 'fetch before create'.
NSFetchRequest *fetch = [NSFetchRequest fetchRequestWithEntityName:@"Favorite"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"stationIdentifier == %@", stID];
[fetch setPredicate:predicate];
YourObject *obj = [ctx executeRequest:fetch];
if(!obj) {
//not there so create it and save
obj = [ctx insertNewManagedObjectForEntity:@"Favorite"]; //typed inline, dont know actual method
obj.stationIdentifier = stID;
[ctx save];
}
//use obj... e.g.
NSLog(@"%@", obj.stationIdentifier);