gpt4 book ai didi

ios - fetchedResultsController.fetchedObjects.count = 0 但它充满了对象

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

我使用非常标准的 fetchedResultsController 实现 tableView 中的输出。在 -viewDidLoad 结束时,我进行了第一次调用:

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

这是我的 fetchedResultsController:

 - (NSFetchedResultsController *) fetchedResultsController
{
if (_fetchedResultsController != nil)
{
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Preparation"
inManagedObjectContext:_context];
[fetchRequest setEntity:entity];

int i = 1;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ismer == %d", i];
fetchRequest.predicate = predicate;

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects: sortDescriptor, nil];

fetchRequest.sortDescriptors = sortDescriptors;

_fetchedResultsController = [[NSFetchedResultsController alloc]initWithFetchRequest:fetchRequest managedObjectContext:_context sectionNameKeyPath:nil cacheName:nil];
_fetchedResultsController.delegate = self;
NSLog(@"_fetchedResultsController.fetchedObjects.count - %d", _fetchedResultsController.fetchedObjects.count);

return _fetchedResultsController;
}

我的 tableView 方法:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[self.fetchedResultsController sections]count];
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

CDPreparation *drug = (CDPreparation *)[self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:@"%@", drug.name];
return cell;
}

那么,问题是:

_fetchedResultsController.fetchedObjects.count 的日志中等于 0,但视觉上 tableView 充满了对象。为什么我有两个不同的计数结果?

最佳答案

NSFetchedResultsController在您调用 performFetch: 之前不会实际执行获取请求, 因此结果计数为 0。

如果您在调用 performFetch: 后记录 fetchedObjects.count,您将看到一个与 tableView 行计数匹配的数字。

关于ios - fetchedResultsController.fetchedObjects.count = 0 但它充满了对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18954624/

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