0

如果他/她插入重复的记录,我需要用警报停止用户:

列 1 列 2 列 3

(ABC) 允许

(BAL) 允许

(ABC) 不允许,因为它的记录重复
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"]; 
4

2 回答 2

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

而不是执行FetchRequest。甚至更好。

于 2012-06-14T19:01:50.520 回答
-1

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

谢谢 :)

于 2012-06-14T21:03:38.333 回答