ios - 核心数据 NSFetchedResultsController 和批量大小设置加载所有行

标签 ios objective-c uitableview core-data nsfetchedresultscontroller

我的核心数据数据库中有数千个点,我想将所有这些点都放在我的 tableView 中,但我不想一次将它们全部提取到内存中,但我想使用 NSFetchedResultsController 并用一些常量对它们进行批处理。我设置它就像我在许多教程中看到的那样,我设置了 fetchBatchSize 属性但是在显示之前加载 View Controller 需要几秒钟。

这个函数在viewDidLoad中被调用:

- (void)initPositionsFetch {
    // Initialize Fetch Request
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"OpenPositionCD"];

    // Add Sort Descriptors
    [fetchRequest setSortDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"timestamp" ascending:NO]]];

    // Add predicate
    [fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"moduleId == %@", _module.moduleId]];

    [fetchRequest setFetchBatchSize:100];

    [fetchRequest setIncludesPendingChanges:NO];

    // Initialize Fetched Results Controller
    self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:temporaryContext sectionNameKeyPath:nil cacheName:nil];

    // Configure Fetched Results Controller
    [self.fetchedResultsController setDelegate:self];

    // Perform Fetch
    NSError *error = nil;
    [self.fetchedResultsController performFetch:&error];

    if (error) {
        NSLog(@"Unable to perform fetch.");
        NSLog(@"%@, %@", error, error.localizedDescription);
    }
}

NSFetchedResultsControllerDelegate 方法:

#pragma mark - NSFetchedResultsControllerDelegate
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
    [self.tableView beginUpdates];
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    [self.tableView endUpdates];
}

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
    switch (type) {
        case NSFetchedResultsChangeInsert: {
            [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
        }
        case NSFetchedResultsChangeDelete: {
            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
        }
        case NSFetchedResultsChangeUpdate: {
            [self configureCell:(PanelPositionCell *)[self.tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
            break;
        }
        case NSFetchedResultsChangeMove: {
            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
        }
    }
}

UITableView 委托(delegate)方法:

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (_tracksVisible)
        return 1;
    else
        return [[self.fetchedResultsController sections] count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_tracksVisible)
        return [_tracks count];
    else {
        NSArray *sections = [self.fetchedResultsController sections];
        id<NSFetchedResultsSectionInfo> sectionInfo = [sections objectAtIndex:section];

        return [sectionInfo numberOfObjects];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier;
    if(_tracksVisible)
        CellIdentifier = TrackCellIdentifier;
    else
        CellIdentifier = PositionCellIdentifier;

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if(_tracksVisible) {
        [((PanelTrackCell *)cell) setValuesByTrackCD:[_tracks objectAtIndex:indexPath.row]];
    } else  {
        [self configureCell:(PanelPositionCell *)cell atIndexPath:indexPath];
    }

    return cell;
}

- (void)configureCell:(PanelPositionCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    // Fetch Record
    NSManagedObject *record = [self.fetchedResultsController objectAtIndexPath:indexPath];
    OpenPositionCD *position = (OpenPositionCD *)record;

    // Update Cell
    [cell setValuesByOpenPositionCD:position];
}

这有什么问题?为什么要加载所有行?核心数据模型可能有问题吗?我试图删除谓词但没有帮助。必须设置 SortDescriptor,但这只是我想到的唯一可能出现问题的地方。我希望结果按时间戳排序。时间戳是日期属性并设置为索引。当我必须先获取结果然后按时间对结果进行排序时,如何对结果进行批处理?我使用 tableView 通过开关 (_tracksVisible) 显示另一个数据应该不是问题,对吧?

我什至尝试使用这段代码在后台获取结果:

[[[self fetchedResultsController] managedObjectContext] performBlock:^{
        NSError *error = nil;
    [self.fetchedResultsController performFetch:&error];

    if (error) {
        NSLog(@"Unable to perform fetch.");
        NSLog(@"%@, %@", error, error.localizedDescription);
    }
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            [_tableView reloadData];
        }];
}];

发生的事情是 Controller 显示得更早,但随后 UI 卡住并且花费的时间几乎与以前相同。

最佳答案

尝试使用较低的 BatchSize 值,也许是 20

[fetchRequest setFetchBatchSize:20];

您也可以尝试在 initPositionFetch 中添加

[fetchRequest setFetchLimit:20];

关于ios - 核心数据 NSFetchedResultsController 和批量大小设置加载所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31704585/

相关文章:

ios - 更新 TableView 导致崩溃

ios - 点击通知时在UITableViewCell中显示相应的内容?

ios - 使用 SpriteKit 为 iPhone X 调整屏幕大小

ios - 从xcode中的同一类调用方法

ios - NSString 到 NSDate 给出了错误的日期

ios - 以编程方式访问 iCloud 音乐库下载和 iCloud 状态

ios - UIPopoverController 中的 UIImagePickerController 显示为空白 View

ios - 在 WebView 中播放视频意外结束后台视频录制?

objective-c - 等待异步函数完成

ios - UITableViewCell 行为奇怪 - 文本变为白色