iOS重复记录不允许

Hes*_*eed 0 core-data ios

如果他/她插入重复的记录,我需要提醒用户:

Column1    Column2    Column3

(A          B          C)    Allowed

(B          A          L) Allowed

(A          B          C)    Not Allowed because its duplicated records
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSManagedObject *newAdds;
    newAdds = [NSEntityDescription insertNewObjectForEntityForName:@"Matches" inManagedObjectContext:context];


    [newAdds setValue:club01.text forKey:@"club01"];
    [newAdds setValue:club02.text forKey:@"club02"];
    [newAdds setValue:matchDateVar forKey:@"matchDate"];


    [newAdds setValue:matchTaypVar forKey:@"matchType"]; 
Run Code Online (Sandbox Code Playgroud)

fab*_*ier 6

你可能需要实现这样的检查:

NSFetchRequest* fetchRequest = [NSFetchRequest fetchRequestWithEntityName: @"Matches"];
fetchRequest.predicate = [NSPredicate predicateWithFormat: @"club01 == %@ AND club02 == %@ AND matchDate == %@", club01.text, club02.text, matchDateVar];
fetchRequest.fetchLimit = 1;

NSError* error = nil;
NSArray* tuples = [context executeFetchRequest: fetchRequest error: &error];
if ( tuples.count > 0 )
{
  // OOPS ! Entity already exists
}
Run Code Online (Sandbox Code Playgroud)

在性能方面,您可能想要为club01,club02,matchDate添加索引.这将有很大帮助.

编辑:您还可以使用:

NSUInteger count = [context countForFetchRequest: fetchRequest error: &error];
Run Code Online (Sandbox Code Playgroud)

而不是executeFetchRequest.甚至更好.