且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检查CoreData中是否已存在名称属性

更新时间:2023-11-27 19:19:46

您可以使用谓词对象匹配某些属性。
如果您只对给定键的对象的存在
感兴趣,请使用 countForFetchRequest ,而不是实际提取对象,并将结果集限制为一个对象:

You can use a fetch request with a predicate to find objects matching certain attributes. If you are only interested in the existence of an object with the given key, use countForFetchRequest instead of actually fetching the objects, and limit the result set to one object:

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"BankInfo"];
[request setPredicate:[NSPredicate predicateWithFormat:@"name = %@", theName]];
[request setFetchLimit:1];
NSUInteger count = [context countForFetchRequest:request error:&error];
if (count == NSNotFound)
    // some error occurred
else if (count == 0)
    // no matching object
else
    // at least one matching object exists