gpt4 book ai didi

ios - NSFetchedResultsController 始终不返回任何行

转载 作者:可可西里 更新时间:2023-11-01 03:37:37 24 4
gpt4 key购买 nike

我正在尝试让 NSFetchedResultsController 与我的 tableview 一起工作,但尽管我尽了最大努力使其正确设置,但它始终不返回任何行。我已经通过 Finder 打开我的数据存储,并通过 SQLite 编辑器验证实际上有很多记录,但它总是返回零。我错过了什么?

Controller 的自定义 getter:

- (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]);
}
}

获取结果委托(delegate):

- (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];
}

我正在为部分和行号实现这两种方法,它为部分返回 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。可能是您在应用程序的其他部分按名称重用了相同的缓存?

关于ios - NSFetchedResultsController 始终不返回任何行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8373790/

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