2012-06-14 85 views
0

我需要停下来与提醒用户,如果他/她插入重复记录:的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"]; 

回答

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

现在没事了。 问题是日期/时间无法重复,因为秒。

thanx :)