ios – NSFetchedResultsController始终不返回任何行

前端之家收集整理的这篇文章主要介绍了ios – NSFetchedResultsController始终不返回任何行前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图让NSFetchedResultsController使用我的tableview,但尽管我尽最大努力正确设置它,它总是返回没有行.我通过Finder打开了我的数据存储,并通过sqlite编辑器验证了实际上有很多记录,但它总是返回零.我错过了什么?

控制器的自定义吸气器:

- (NSFetchedResultsController *)fetchedResultsController {
    if (fetchedResultsController_ != nil) {
        return fetchedResultsController_;
    }

    RBGameItemController* itemController = [RBGameItemController sharedInstance];

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"GameItem" inManagedObjectContext:itemController.managedObjectContext];
    [fetchRequest setEntity:entity];

    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];

    [fetchRequest setFetchBatchSize:20];

    NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest  
                                                                                                  managedObjectContext:itemController.managedObjectContext 
                                                                                                    sectionNameKeyPath:nil 
                                                                                                             cacheName:@"Root"];
    self.fetchedResultsController = theFetchedResultsController;
    self.fetchedResultsController.delegate = self;

    [sort release];
    [fetchRequest release];
    [theFetchedResultsController release];

    return self.fetchedResultsController;
}

我在viewDidLoad中获取数据:

- (void)viewDidLoad {
    [super viewDidLoad];

    NSError *error = nil;
    if (![self.fetchedResultsController performFetch:&error]) {
        NSLog(@"Unresolved error %@,%@",error,[error userInfo]);
    }
}

获取结果代表:

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
    // The fetch controller is about to start sending change notifications,so prepare the table view for updates.
    [self.tableView beginUpdates];

    NSLog(@"controllerWillChangeContent");
}


- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
    NSLog(@"controller:didChangeObject:");
    UITableView *tableView = self.tableView;

    switch(type) {

        case NSFetchedResultsChangeInsert:
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeUpdate:
            [self configureCell:[self.tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
            break;

        case NSFetchedResultsChangeMove:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            // Reloading the section inserts a new row and ensures that titles are updated appropriately.
            [tableView reloadSections:[NSIndexSet indexSetWithIndex:newIndexPath.section] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}


- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
    NSLog(@"controller:didChangeSection:");
    switch(type) {
        case NSFetchedResultsChangeInsert:
            [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}


- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    NSLog(@"controllerDidChangeContent:");
    // The fetch controller has sent all current change notifications,so tell the table view to process all updates.
    [self.tableView endUpdates];
}

我正在实现section和row number的两种方法,它们为section返回1,为行返回0:

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    int sections = [[self.fetchedResultsController sections] count];
    NSLog(@"sections=%i",sections);
     return sections;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    id<NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
    int rows = [sectionInfo numberOfObjects];

    NSLog(@"rows=%i",rows);

    return rows;
}

解决方法

您是否尝试使用获取请求手动执行查询以确保它返回您所期望的内容?在你的自定义fetchedResultsController中尝试做:
NSArray *entities = [itemController.managedObjectContext executeFetchRequest:fetchRequest error:&error];
NSLog(@"%d",entities.count);

看看会有什么回来

另外,尝试在没有缓存的情况下设置fetchedRequestController.您可能是在应用程序的其他部分按名称重用相同的缓存?

原文链接:https://www.f2er.com/iOS/330784.html

猜你在找的iOS相关文章