从不在集合中的核心数据中获取对象
我试图从不在给定集合中的核心数据中获取对象,但我无法让它工作。
例如,假设我们有一个名为 User 的核心数据实体,它具有一些属性,例如 userName、familyName、givenName 和 active。给定一个表示一组用户名的字符串数组,我们可以轻松获取与该用户名列表相对应的所有用户:
NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] init];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"User"
inManagedObjectContext:moc];
[request setEntity:entity];
NSArray *userNames = [NSArray arrayWithObjects:@"user1", @"user2", @"user3", nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"userName IN %@", userNames];
[request setPredicate:predicate];
NSArray *users = [moc executeFetchRequest:request error:nil];
但是,我想获取该组的补集,即,我想要核心数据中不存在的所有用户t 在 userNames 数组中指定用户名。有谁知道如何解决这个问题?我认为在谓词 中添加
,但是 Xcode 抛出一个异常,指出该谓词无法解析格式。我还尝试使用可用于获取请求的谓词构建器,但没有成功。该文档也不是特别有帮助。建议?评论?感谢您的帮助:)"NOT"
就足够简单了(即“userName NOT IN %@”)
I'm trying to fetch objects from core data that are not in a given set, but I haven't been able to get it to work.
For instance, suppose that we have a core data entity named User, which has a few attributes such as userName, familyName, givenName, and active. Given an array of strings representing a set of usernames, we can easily fetch all the users corresponding to that list of usernames:
NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] init];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"User"
inManagedObjectContext:moc];
[request setEntity:entity];
NSArray *userNames = [NSArray arrayWithObjects:@"user1", @"user2", @"user3", nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"userName IN %@", userNames];
[request setPredicate:predicate];
NSArray *users = [moc executeFetchRequest:request error:nil];
However, I want to fetch the complement of that set, i.e., I want all the users in core data that don't have the usernames specified in the userNames array. Does anyone have an idea how to approach this issue? I thought it would be simple enough to add a "NOT"
in the predicate (i.e., "userName NOT IN %@")
, but Xcode throws an exception saying the predicate format could not be parsed. I also tried using the predicate builder available for fetch requests with no luck. The documentation wasn't particularly helpful either. Suggestions? Comments? Thanks for all your help :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了找到不在数组中的对象,您所要做的就是这样:
这应该返回所有对象的请求,而不包含您指定的对象
In order to find the objects that aren't in your array, all you have to do is something like this:
That should return a request of all the objects without the ones you specified
我不擅长核心数据/objective-c,但谓词应该像下面的语句;
一个例子:
I am not strong at core data/objective-c but the predicate should be like the following statement;
An example:
这是另一个有用的示例,展示了如何获取字符串列表,并过滤掉任何不以字母 AZ 开头的字符串:
当我填充
时,我使用这种
带有 AZ 索引,并且希望为不以字母开头的项目提供“所有其他”部分。NSPredicate
UITableViewHere's another useful example, showing how to take a list of strings, and filter out any which DON'T start with the letters A-Z:
I use this kind of
NSPredicate
when I'm populating aUITableView
with an A-Z index, and want an "everything else" section for items which don't start with a letter.