为什么我们不能在NSFetchedResultsController上更改FetchRequest? [英] Why we can't change FetchRequest at NSFetchedResultsController?

查看:85
本文介绍了为什么我们不能在NSFetchedResultsController上更改FetchRequest?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例我在名为FetchController的ListController处调用了NSFetchedResultsController

Example I have NSFetchedResultsController called at ListController called FetchController

+(NSFetchRequest * )fetchRequestInContext: (NSString*) entityName : (NSPredicate *) predicate : (NSString*) sortKey : (BOOL) sortAscending;

+(NSFetchedResultsController *) searchControllerInContext: (NSString*) entityName : (NSPredicate *) predicate : (NSString*) sortKey : (BOOL) sortAscending 
{
    NSFetchRequest *request = [self fetchRequestInContext:entityName :predicate :sortKey :sortAscending];

    NSFetchedResultsController * FRC=[[[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:[ThreadClass managedObjectContext] sectionNameKeyPath:Nil cacheName:Nil]autorelease];
    NSLog(@"FRC : %@",FRC);
    return FRC;
}

看看该代码,当我想创建NSFetchedResultsController时,我调用searchControllerInContext用以下代码执行:

look at that code, I call searchControllerInContext when I want to make NSFetchedResultsController and then perfom it with this code :

if (![[self ListController].FetchController performFetch:&error]) {
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}

问题是我不希望总是执行请求更改时获取,我想要

the problem is I dont like to always performFetch when request is change, I want to make it easy with

[self ListController].FetchController.fetchRequest=[self FunctionTogetNewRequest];

但是[self ListController] .FetchController.fetchRequest是只读的。.

but the [self ListController].FetchController.fetchRequest is readonly..

我要这样做是因为我不想一次又一次地获取..我可以吗?

I want do this because I Don't want to perfomFetch again and again.. can I do that?

我的意思是,如果我有一个程序可以根据用户在搜索框中输入的内容提供记录,每次搜索框内容更改时,我都应该创建一个新的控制器吗?那太奇怪了。我以为使用NSFetchedResultsController的目的就是这样,所以我们不必这样做?

I mean, if I have a program that give records based on what user put in search box, should I create a new controller every time the search box content change? That would be strange. I thought the whole point of using NSFetchedResultsController is so we don't have to do that?

推荐答案

即使 fetchRequest 是只读属性,您可以对其进行修改。例如,只要您不缓存结果(或者只要先删除缓存),设置提取请求的谓词就可以很好地工作。我已经在许多项目中成功使用了这项技术。修改获取请求后,应再次调用 performFetch

Even though fetchRequest is a readonly property, you can modify it. For example, setting the predicate of the fetch request works perfectly well as long as you're not caching the results (or as long as you delete the cache first). I've used this technique successfully in a number of projects. After modifying the fetch request, you should call performFetch again.

底线:无需创建每次搜索字词更改时都显示FRC。只需删除缓存,更改获取请求的谓词,然后使用现有实例获取新结果集即可。

Bottom line: you don't need to create a FRC every time the search terms change. Just delete your cache, change the fetch request's predicate and fetch the new result set with your existing instance.

这篇关于为什么我们不能在NSFetchedResultsController上更改FetchRequest?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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