如何在重新进入查看时转储 Core Data 搜索结果 FRC?

Posted

技术标签:

【中文标题】如何在重新进入查看时转储 Core Data 搜索结果 FRC?【英文标题】:How do you dump Core Data search results FRC on reentry to view? 【发布时间】:2011-06-14 00:48:36 【问题描述】:

我的核心数据搜索有问题。加载后,我的整个表都是可见的。好的。我按下我的搜索按钮,输入一个术语,然后按下搜索。显示正确的行。好的。

问题是当我从表格视图返回到我的***视图(多级表格视图)并向下钻取到表格时。我之前过滤的搜索结果仍然显示。如何让 FRC 转储之前的搜索谓词并在输入时显示所有记录?

这是我的代码的相关部分:

    - (void)viewDidLoad 
            [super viewDidLoad];

           if (managedObjectContext == nil)  managedObjectContext = [(RecipesAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; 

           //search
           UISearchDisplayController *searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:sBar contentsController:self];  

           [self performSelector:@selector(setSearchDisplayController:) withObject:searchDisplayController];

           [searchDisplayController setDelegate:self];  
           [searchDisplayController setSearchResultsDataSource:self];  
           [searchDisplayController setSearchResultsDelegate:self];
           [searchDisplayController release];  

           NSError *error;
           if (![[self fetchedResultsController] performFetch:&error]) 
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            exit(-1);
           

           self.filteredListContent = [NSMutableArray arrayWithCapacity:[[[self fetchedResultsController] fetchedObjects] count]];  


- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar 

    if (self.sBar.text !=nil)
    
        NSPredicate *predicate =[NSPredicate predicateWithFormat:@"name contains[cd] %@", self.sBar.text];

        [fetchedResultsController.fetchRequest setPredicate:predicate];
    

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

    [self.myTable reloadData];

    [sBar resignFirstResponder];  



- (NSFetchedResultsController *)fetchedResultsController 

    // Set up the fetched results controller if needed.
    if (fetchedResultsController == nil) 

        // Create the fetch request for the entity.
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

        // Edit the entity name as appropriate.
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"MyEntity" inManagedObjectContext:managedObjectContext];
        [fetchRequest setEntity:entity];

        //set batch size
        [fetchRequest setFetchBatchSize:20];

        // Edit the sort key as appropriate.
        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
        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:@"name" cacheName:nil];
        aFetchedResultsController.delegate = self;
        self.fetchedResultsController = aFetchedResultsController;

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

    return fetchedResultsController;


- (void)viewDidUnload 
    self.filteredListContent = nil;
    self.fetchedResultsController = nil;



- (void)dealloc 
    self.filteredListContent = nil;
    self.fetchedResultsController = nil;

    [fetchedResultsController release];
    [managedObjectContext release];

    [filteredListContent release];

    [myTable release];

    [super dealloc];

谢谢

更新:知道了,谢谢。 我只是调用这个方法:

- (void)loadClean 

    if(fetchedResultsController)
        [fetchedResultsController release];
    

    fetchedResultsController = nil;

    NSError *error;
    if (![[self fetchedResultsController] performFetch:&error]) 
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        exit(-1);
    

    [self.myTable reloadData];

【问题讨论】:

【参考方案1】:

您需要为 FRC 提供新的提取请求并删除其缓存。如果您使用相同的 fetch 请求,您将获得相同的返回。

【讨论】:

即使我的缓存设置为零? 当你说“新人”时,我不确定我是否关注你?

以上是关于如何在重新进入查看时转储 Core Data 搜索结果 FRC?的主要内容,如果未能解决你的问题,请参考以下文章

Linux:段错误(核心已转储) Segmentation fault (core dumped)(在Linux上如何得到一个段错误的核心转储)(笔记)(未完成,暂停)

Core Data 在重新分配时删除对象

关于内核转储(core dump)的设置方法

如何在 Linux 上转储 .NET Core 应用程序

应用重新启动时数据不会在 Core Data 中持久化

Swift:当 didSelectRowAtIndexPath 时如何更新 Core Data 中的数据?