xcode 为什么我们不能在 NSFetchedResultsController 中更改 FetchRequest?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7200855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:48:44  来源:igfitidea点击:

Why we can't change FetchRequest at NSFetchedResultsController?

objective-cxcodecore-data

提问by user4951

Example I have NSFetchedResultsController called at ListController called FetchController

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

+(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;
}

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

看看那个代码,当我想创建 NSFetchedResultsController 时,我调用 searchControllerInContext 然后使用以下代码执行它:

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];

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

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

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

我想这样做是因为我不想一次又一次地执行提取..我可以这样做吗?

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?

我的意思是,如果我有一个程序根据用户在搜索框中输入的内容提供记录,我是否应该在每次搜索框内容更改时创建一个新控制器?那会很奇怪。我认为使用 NSFetchedResultsController 的全部意义在于我们不必这样做?

回答by Cameron Spickert

Even though fetchRequestis 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 performFetchagain.

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

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.

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

回答by TechZen

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?

如果我有一个程序根据用户在搜索框中输入的内容提供记录,我是否应该在每次搜索框内容更改时创建一个新控制器?

Yes, because you change the predicate on a the fetch which requires generating a new controller.

是的,因为您更改了需要生成新控制器的 fetch 的谓词。

I thought the whole point of using NSFetchedResultsController is so we don't have to do that?

我认为使用 NSFetchedResultsController 的全部意义在于我们不必这样做?

The point of an FRC is to automatically handle the interaction between the context and the tableview. It has dedicated properties to return sections and rows as well as delegate methods to modify the table in response to changes in the data model. Since an FRC is defined by the its fetch, when you make a change to the fetch, you need to generate a new FRC

FRC 的重点是自动处理上下文和 tableview 之间的交互。它有专门的属性来返回部分和行以及委托方法来修改表以响应数据模型的变化。由于 FRC 是由它的 fetch 定义的,当你对 fetch 进行更改时,你需要生成一个新的 FRC

Don't make the mistake of thinking of an FRC has a big, expensive object. It's not. There is not a problem with creating and disposing of a large number of FRC as needed.

不要错误地认为 FRC 有一个大而昂贵的对象。它不是。根据需要创建和处理大量 FRC 没有问题。