iOS重复记录不允许

时间:2012-06-14 17:51:53

标签: ios core-data

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

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"]; 

2 个答案:

答案 0 :(得分: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
}

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

编辑:你也可以使用:

NSUInteger count = [context countForFetchRequest: fetchRequest error: &error];

而不是executeFetchRequest。甚至更好。

答案 1 :(得分:-1)

现在好了。 问题是因为秒数而无法复制日期/时间。

thanx:)