gpt4 book ai didi

ios - NSPredicate predicateWithFormat 对象随时间变化

转载 作者:行者123 更新时间:2023-11-28 23:01:42 25 4
gpt4 key购买 nike

我有这段工作代码,但现在我需要能够根据 predicateWithFormat 中使用的对象更改 NSPredicate,如下所示:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Item" inManagedObjectContext:_context];

[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"self.atrObj == %@", _currentUser.atrObj.objectID]];

[fetchRequest setEntity:entity];

NSSortDescriptor *sortContent = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortContent]];
[fetchRequest setFetchBatchSize:10];
NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:_context sectionNameKeyPath:nil
cacheName:nil];

self.fetchedResultsController = theFetchedResultsController;
self.fetchedResultsController.delegate = self;

当我从具有此 tableviewcontroller 属性的应用程序委托(delegate)更改 _currentUser 对象时,我需要 fetchedResultsControlelr 使用新的 _currentUser 对象。

[self.tableViewcontroller setCurrentUser:user]; 

谢谢!

最佳答案

您可以随时更改谓词,但一定要通过 performFetch 重新获取。同样来自 Apple 文档:

Important: Important If you are using a cache, you must call deleteCacheWithName: before changing any of the fetch request, its predicate, or its sort descriptors. You must not reuse the same fetched results controller for multiple queries unless you set the cacheName to nil.

此外,来自 NSFetchedResultsController 的 Apple 文档:

Modifying the Fetch Request You cannot simply change the fetch request to modify the results. If you want to change the fetch request, you must:

  • If you are using a cache, delete it (using deleteCacheWithName:). Typically you should not use a cache if you are changing the fetch request.

  • Change the fetch request.

  • Invoke performFetch:.

关于ios - NSPredicate predicateWithFormat 对象随时间变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9900286/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com