如何使用 NSFetchedResultsController 和 UISearchDisplayController

发布于 2024-08-20 03:19:41 字数 3693 浏览 4 评论 0原文

我一直在使用 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;
} 

I've been creating an iPhone App using Core Data.

First of all, does it make sense to use an NSFetchedResultsController and a UISearchDisplayController together to fetch the result? Would you recommend something else?

I've been trying quite long combining an NSFetchedResultController and a UISearchDisplayController. I've been thinking of setting an NSPredicate in the (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption method to the NSFetchedResultController of the UIViewController. But this isn't working that well.

So, have you any idea how to implement a solution for my problem? Thank you already for posting answers or links to good tutorials.

EDIT

Here is my code. The UISearchDisplayDelegate methods call the method (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope that should set the predicate in the NSFetchedResultController. I've also added the code of the 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;
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最好是你 2024-08-27 03:19:41

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

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

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

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

 [NSFetchedResultsController deleteCacheWithName:@"UserSearch"];

就像魔术一样,它会很好地工作。

您还需要监视搜索显示控制器何时结束其搜索,并擦除您的谓词。

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

Glancing at your code, its right there. I ran into issues using this method for changing the search. The fix: Clear your cache!

Say you initiated your fetched result controller as follows. Note the cacheName property.

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

As you change your predicate, simply add:

 [NSFetchedResultsController deleteCacheWithName:@"UserSearch"];

Like magic, it will work nicely.

You will also want to monitor when the search display controller will end its search, and wipe your predicate.

Although this thread looks dead, I found it on google, so perhaps someone out there will get use of this. :)

最终幸福 2024-08-27 03:19:41

根据文档,您要么想要使用谓词将结果过滤到数组中,要么重新初始化 fetchedResultsController。

重要提示:您不得修改
获取请求后
初始化控制器。为了
例如,您不得更改
谓词或排序顺序。

-- NSFetchedResultsController

(我会对空闲的答案发表评论,但我没有业力。)

According to the docs, you'll either want to filter the results into an array using a predicate or reinitialize the fetchedResultsController.

Important: You must not modify the
fetch request after you have
initialized the controller. For
example, you must not change the
predicate or the sort orderings.

-- NSFetchedResultsController

(I'd comment on idle's answer, but I'm karma-less.)

恋竹姑娘 2024-08-27 03:19:41

听起来您只对获取和显示对象感兴趣。在这种情况下,您不使用搜索显示控制器。搜索显示控制器与搜索栏一起使用,以帮助允许用户输入文本搜索您的内容。

通常,您使用获取结果控制器来帮助实现表视图数据源和委托方法。此外,您还可以为获取结果控制器创建并使用获取请求。这都是样板文件(当您创建新项目并选择“使用核心数据进行存储”选项时,请参阅 Apple 提供的模板代码)。

您可以在获取请求中创建并指定谓词。这将允许您过滤表格显示的对象。例如:

// only fetch objects with myAttribute set to someValue
NSPredicate *pred = [NSPredicate predicateWithFormat:@"myAttribute == %@",someValue];
[fetchRequest setPredicate:pred];

It sounds like you are just interested in fetching and displaying objects. In this case, you do NOT use a search display controller. The search display controller is used with a search bar to help with allowing user-entered text searches of your content.

Typically you use a fetched results controller to aid in your implementation of the table view datasource and delegate methods. Also, you create and use a fetch request for your fetched results controller. This is all boiler-plate (see the template code provided by Apple when you create a new project with the "Use Core Data for storage" option selected).

It is in the fetch request that you can create and specify a predicate. This will allow you to filter the objects that your table displays. For example:

// only fetch objects with myAttribute set to someValue
NSPredicate *pred = [NSPredicate predicateWithFormat:@"myAttribute == %@",someValue];
[fetchRequest setPredicate:pred];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文