gpt4 book ai didi

不允许 iOS 重复记录

转载 作者:行者123 更新时间:2023-11-28 18:40:54 25 4
gpt4 key购买 nike

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

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

最佳答案

您可能需要执行这样的检查:

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。甚至更好。

关于不允许 iOS 重复记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11038609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com