帮助CoreData做一个NSPredicate - 递归搜索 [英] Help with CoreData making a NSPredicate - Recursive Searching

查看:93
本文介绍了帮助CoreData做一个NSPredicate - 递归搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用CoreData为iPhone项目,我被卡住试图建立一个谓词。

I am using CoreData for an iPhone project and I am stuck trying to build a predicate.

我的核心资料实体是

Folder 
   parent  - Point to the folder class parent, can be null and is one to one.
   secure  - An enum that holds the security type.

我遇到的问题是我试图使它,所以我不显示任何文件夹

The problem I have is that I am trying to make it so I don't show any folder that are in a secure folder.

现在我的谓词看起来像这样。

Right now my predicate looks something like this.

NSPredicate *pred = [NSPredicate predicateWithFormat:@"secure = $@ AND (parent = %@ OR parent.secure = %@)",[NSNumber numberWithInteger:kNoSecurity], [NSNull null], [NSNumber numberWithInteger:kNoSecurity]];

这个工作发现当我只有一个链如folder1 - > folder2和folder1是安全的。但如果我有folder1 - > folder2 - > folder3(folder2和folder3不安全)。 Folder3被返回,因为我只检查一个级别。有没有办法让谓词做整个链的检查?

This works find when I only have a chain like folder1 -> folder2 and folder1 is secure. But if I have folder1 -> folder2 -> folder3 (folder2 and folder3 are not secure). Folder3 gets returned because I only check one level up. Is there a way to get the predicate to do the check for a entire chain?

谢谢。

推荐答案


问题是这只会增加
一级。所以如果我有folder1 - >
folder2 - > folder3 - > folder4,和
folder1是安全的。那么folder2不是
,而是folder3和folder4。

The problem is that this only goes up one level. So if I have folder1 -> folder2 -> folder3 -> folder4, and folder1 is secure. Then folder2 is not show but folder3 and folder4 are.

你不能递归地在谓词中处理关系,因为只有keypaths描述抽象实体之间的关系,而不是实际包含数据的具体的,活的管理对象。实体图可以非常简单,但在运行时生成活动对象的非常复杂的图。

You can't recursively walk relationships in predicates because keypaths only describe the relationship between the abstract entities and not the concrete, living managed objects that actually contain the data. An entity graph can be very simple yet generate a vastly complex graph of live objects when populated at runtime. You can't logically capture the complexity of that live graph with a simple keypath.

在这种情况下,您有一个文件夹实体与自身的关系为 parent secure 的属性。因此,keypath只能描述最多具有路径 parent.secure 的两个属性。您不能创建 parent.parent.secure 的键入,因为实体图形中实际上不存在此类关系。这样的路径有时仅存在于活动对象图中。在逻辑上不可能在任何给定时间根据数据的细节硬编码可能或可能不存在的路径。

In this case, you have a Folder entity which has a relationship to itself called parent and an attribute of secure. Therefore, a keypath can only describe at most those two properties with path parent.secure. You can't create a keypath of parent.parent.secure because no such relationship actually exists in the entity graph. Such a path only exist sometimes in the live object graph. It would be logically impossible to hard code a path that might or might not exist depending on the particulars of the data at any given time.

这种情况是创建自定义NSManagedObject子类的能力真的很方便。您的文件夹 entites不一定是愚蠢的数据,您可以添加行为,以便每个对象可以访问自己的状态,并根据需要返回不同的数据。

This type of situation is where the ability to create customized NSManagedObject subclasses really comes in handy. Your Folder entites don't have to be just dumb data, you can add behaviors to them so that each object can access its own state and return different data as needed.

在这种情况下,我建议添加一个名为 hasSecureAncestor 的瞬态布尔属性。然后创建一个自定义的getter方法,如:

In this case, I would recommend adding a transient boolean property named something like hasSecureAncestor. Then create a custom getter method like:

- (BOOL) hasSecureAncestor{
    BOOL hasSecureAncestor=NO;
    if (self.parent.secure==kNoSecurity) {
        hasSecureAncestor=YES;
    }else {
        if (self.parent.parent!=nil) {
            hasSecureAncestor=self.parent.hasSecureAncestor;
        }else {
            hasSecureAncestor=NO;
        }
    }
    return hasSecureAncestor;
}

然后只需创建一个谓词来测试hasSecureAncestor == YES。自定义访问器将走一个任意深的递归关系,寻找一个安全的祖先。

Then just create a predicate to test for "hasSecureAncestor==YES". The custom accessor will walk an arbitrarily deep recursive relationship looking for a secure ancestor.

这篇关于帮助CoreData做一个NSPredicate - 递归搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆