核心数据中的获取属性 [英] Fetched Property in Core Data

查看:125
本文介绍了核心数据中的获取属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的核心数据模型中, Person 有一个或多个 Cars ,由无序到多关系汽车。通常,我需要检索 datePurchased dateLastUsed 订购的人员汽车。

In my core data model, a Person has one or more Cars, specified by the unordered to-many relationship 'Cars'. Frequently, I need to retrieve a Person's cars ordered by datePurchased, or by dateLastUsed.

到目前为止,我一直在 Person carsByDatePurchased 添加自己的方法。这使用一个排序描述符来排序NSSet cars 并返回一个NSArray。

Until now, I have been adding my own method to Person for carsByDatePurchased. This uses a sort descriptor to sort the NSSet cars and return an NSArray.

可以/获取属性?我每次需要以一定顺序需要汽车时,使用排序描述符经历一些性能开销,甚至进行到实现自己的缓存 carsByDatePurchased 。看起来像抓取的属性为我缓存 - 是正确的吗?

Could/should I instead use a Fetched Property for this? I am experiencing some performance overhead using the sort descriptor every time I need the cars in a certain order, even going so far as implementing my own caching of carsByDatePurchased. It looks like the fetched property is cached for me - is that correct?

获取的属性与我自己的实现有什么限制?

What are the limitations of a fetched property vs my own implementation?

重要的是,价值持续执行之间?如果我更新抓取的属性并保存我的上下文,是下次启动应用程序时存储的值?

And crucially, does the fetched property's value persist between executions? If I update the fetched property and save my context, is the value stored for the next time I launch the application?

推荐答案

A获取的属性将工作,确实我在我自己的项目中使用它与Post-> Comment关系,需要按添加日期索引排序。

A fetched property will work, and indeed I used it in my own project with a Post->Comment relationship which needs to be sorted by 'date added index'.

一些注意事项:您不能在可视编辑器中指定排序描述符,并且必须在代码中指定。

There are a number of caveats: You cannot specify a sort descriptor in the visual editor and have to specify it in code.

我使用类似这样的

    // Find the fetched properties, and make them sorted...
for (NSEntityDescription *entity in [_managedObjectModel entities])
{
    for (NSPropertyDescription *property in [entity properties])
    {
        if ([property isKindOfClass:[NSFetchedPropertyDescription class]])
        {
            NSFetchedPropertyDescription *fetchedProperty = (NSFetchedPropertyDescription *)property;
            NSFetchRequest *fetchRequest = [fetchedProperty fetchRequest];

            // Only sort by name if the destination entity actually has a "index" field
            if ([[[[fetchRequest entity] propertiesByName] allKeys] containsObject:@"index"])
            {
                NSSortDescriptor *sortByName = [[NSSortDescriptor alloc] initWithKey:@"index"
                                                                           ascending:YES];

                [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortByName]];
            }
        }
    }
}

我的邮件实体我有一个名为sortedComments的获取属性,定义为:

In My Post entity I have a fetched property called "sortedComments" which is defined as:

post == $FETCH_SOURCE

其中帖子有一对多的注释关系,注释有一个后倒数

where posts have a to-many "comments" relationship and comments have a "post" inverse

在这里反对其他答案:使用像这样的获取属性的好处是,CoreData照顾缓存和无效的缓存作为一个帖子的评论或实际上拥有它们的帖子

In opposition to the other answers here: The benefits of using a fetched property like this, is CoreData takes care of the caching and invalidating the cache as comments for a post or indeed the post that owns them changes.

这篇关于核心数据中的获取属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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