gpt4 book ai didi

iphone - 如何使用 NSFetchedResultsController 和 UISearchDisplayController

转载 作者:行者123 更新时间:2023-12-03 18:24:21 25 4
gpt4 key购买 nike

我一直在使用 Core Data 创建 iPhone 应用程序。

首先,一起使用 NSFetchedResultsController 和 UISearchDisplayController 来获取结果是否有意义?您还能推荐其他东西吗?

我一直在尝试将 NSFetchedResultController 和 UISearchDisplayController 结合起来。我一直在考虑在 UIViewController 的 NSFetchedResultController 的 (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption 方法中设置 NSPredicate。但这效果不太好。

那么,您知道如何为我的问题实现解决方案吗?感谢您发布答案或优秀教程的链接。

编辑

这是我的代码。 UISearchDisplayDelegate 方法调用方法 (void)filterContentForSearchText:(NSString*)searchText range:(NSString*)scope ,该方法应在 NSFetchedResultController 中设置谓词。我还添加了 NSFetchedResultController 的代码。

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];

// Return YES to cause the search result table view to be reloaded.
return YES;
}


- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
[self filterContentForSearchText:[self.searchDisplayController.searchBar text] scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];

// Return YES to cause the search result table view to be reloaded.
return YES;
}


- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSString *query = self.searchDisplayController.searchBar.text;
if (query && query.length) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Name contains[cd] %@", query];
[fetchedResultsController.fetchRequest setPredicate:predicate];
}

NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error]) {
// Handle error
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
exit(-1); // Fail
}

}


- (NSFetchedResultsController *)fetchedResultsController {

if (fetchedResultsController != nil) {
return fetchedResultsController;
}

/*
Set up the fetched results controller.
*/
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];

// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];

// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

[fetchRequest setSortDescriptors:sortDescriptors];

// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;

[aFetchedResultsController release];
[fetchRequest release];
[sortDescriptor release];
[sortDescriptors release];

return fetchedResultsController;
}

最佳答案

看看你的代码,它就在那里。我在使用这种方法更改搜索时遇到了问题。解决方法:清除缓存!

假设您按如下方式启动了获取结果 Controller 。请注意cacheName 属性。

    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:[self fetchRequest] managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"UserSearch"];

当您更改谓词时,只需添加:

 [NSFetchedResultsController deleteCacheWithName:@"UserSearch"];

就像魔术一样,它会很好地发挥作用。

您还需要监视搜索显示 Controller 何时结束其搜索,并删除您的谓词。

虽然这个线程看起来已经死了,但我在谷歌上找到了它,所以也许有人会利用这个。 :)

关于iphone - 如何使用 NSFetchedResultsController 和 UISearchDisplayController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2193738/

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