我有一个变量,它包含Core Data的记录集合。有时它可能是成千上万的记录。如果我将此变量提供给集合视图,它是否会自动处理大量数据?
我希望该集合是从Core Data延迟加载到变量中的,并且集合视图一次加载10-20只是为了处理视口。这样做太好了,或者处理这个问题的最佳方式是什么?
答案 0 :(得分:2)
我使用带有集合视图的获取结果控制器的AshFurrow's实现。 然后,当您延迟加载获取的结果控制器时,只需设置批量大小。
- (NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Your added implementation
[fetchRequest setFetchBatchSize:20];
// More code...
aFetchedResultsController.delegate = self;
_fetchedResultsController = aFetchedResultsController;
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _fetchedResultsController;
}
以下是AshFurrow的NSFetchedResultsControllerDelegate实现,我将其保留原样。然后在集合视图中使用fetchedResultsController委托/ dataSource方法,与在表视图中的方式非常相似。
// GitHub : AshFurrow FetchedResultsController + CollectionView
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
// Begin updates
_objectChanges = [NSMutableDictionary dictionary];
_sectionChanges = [NSMutableDictionary dictionary];
}
- (void)controller:(NSFetchedResultsController *)controller
didChangeSection:(id<NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{
if (type == NSFetchedResultsChangeInsert || type == NSFetchedResultsChangeDelete) {
NSMutableIndexSet *changeSet = _sectionChanges[@(type)];
if (changeSet != nil) {
[changeSet addIndex:sectionIndex];
} else {
_sectionChanges[@(type)] = [[NSMutableIndexSet alloc] initWithIndex:sectionIndex];
}
}
}
- (void)controller:(NSFetchedResultsController *)controller
didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath
forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath
{
NSMutableArray *changeSet = _objectChanges[@(type)];
if (changeSet == nil) {
changeSet = [[NSMutableArray alloc] init];
_objectChanges[@(type)] = changeSet;
}
switch(type) {
case NSFetchedResultsChangeInsert:
[changeSet addObject:newIndexPath];
break;
case NSFetchedResultsChangeDelete:
[changeSet addObject:indexPath];
break;
case NSFetchedResultsChangeUpdate:
[changeSet addObject:indexPath];
break;
case NSFetchedResultsChangeMove:
[changeSet addObject:@[indexPath, newIndexPath]];
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
// End updates
NSMutableArray *moves = _objectChanges[@(NSFetchedResultsChangeMove)];
if (moves.count > 0) {
NSMutableArray *updatedMoves = [[NSMutableArray alloc] initWithCapacity:moves.count];
NSMutableIndexSet *insertSections = _sectionChanges[@(NSFetchedResultsChangeInsert)];
NSMutableIndexSet *deleteSections = _sectionChanges[@(NSFetchedResultsChangeDelete)];
for (NSArray *move in moves) {
NSIndexPath *fromIP = move[0];
NSIndexPath *toIP = move[1];
if ([deleteSections containsIndex:fromIP.section]) {
if (![insertSections containsIndex:toIP.section]) {
NSMutableArray *changeSet = _objectChanges[@(NSFetchedResultsChangeInsert)];
if (changeSet == nil) {
changeSet = [[NSMutableArray alloc] initWithObjects:toIP, nil];
_objectChanges[@(NSFetchedResultsChangeInsert)] = changeSet;
} else {
[changeSet addObject:toIP];
}
}
} else if ([insertSections containsIndex:toIP.section]) {
NSMutableArray *changeSet = _objectChanges[@(NSFetchedResultsChangeDelete)];
if (changeSet == nil) {
changeSet = [[NSMutableArray alloc] initWithObjects:fromIP, nil];
_objectChanges[@(NSFetchedResultsChangeDelete)] = changeSet;
} else {
[changeSet addObject:fromIP];
}
} else {
[updatedMoves addObject:move];
}
}
if (updatedMoves.count > 0) {
_objectChanges[@(NSFetchedResultsChangeMove)] = updatedMoves;
} else {
[_objectChanges removeObjectForKey:@(NSFetchedResultsChangeMove)];
}
}
NSMutableArray *deletes = _objectChanges[@(NSFetchedResultsChangeDelete)];
if (deletes.count > 0) {
NSMutableIndexSet *deletedSections = _sectionChanges[@(NSFetchedResultsChangeDelete)];
[deletes filterUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSIndexPath *evaluatedObject, NSDictionary *bindings) {
return ![deletedSections containsIndex:evaluatedObject.section];
}]];
}
NSMutableArray *inserts = _objectChanges[@(NSFetchedResultsChangeInsert)];
if (inserts.count > 0) {
NSMutableIndexSet *insertedSections = _sectionChanges[@(NSFetchedResultsChangeInsert)];
[inserts filterUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSIndexPath *evaluatedObject, NSDictionary *bindings) {
return ![insertedSections containsIndex:evaluatedObject.section];
}]];
}
UICollectionView *collectionView = self.collectionView;
[collectionView performBatchUpdates:^{
NSIndexSet *deletedSections = _sectionChanges[@(NSFetchedResultsChangeDelete)];
if (deletedSections.count > 0) {
[collectionView deleteSections:deletedSections];
}
NSIndexSet *insertedSections = _sectionChanges[@(NSFetchedResultsChangeInsert)];
if (insertedSections.count > 0) {
[collectionView insertSections:insertedSections];
}
NSArray *deletedItems = _objectChanges[@(NSFetchedResultsChangeDelete)];
if (deletedItems.count > 0) {
[collectionView deleteItemsAtIndexPaths:deletedItems];
}
NSArray *insertedItems = _objectChanges[@(NSFetchedResultsChangeInsert)];
if (insertedItems.count > 0) {
[collectionView insertItemsAtIndexPaths:insertedItems];
}
NSArray *reloadItems = _objectChanges[@(NSFetchedResultsChangeUpdate)];
if (reloadItems.count > 0) {
[collectionView reloadItemsAtIndexPaths:reloadItems];
}
NSArray *moveItems = _objectChanges[@(NSFetchedResultsChangeMove)];
for (NSArray *paths in moveItems) {
[collectionView moveItemAtIndexPath:paths[0] toIndexPath:paths[1]];
}
} completion:nil];
_objectChanges = nil;
_sectionChanges = nil;
}
答案 1 :(得分:1)
如果在您处于集合视图中时记录将发生变化,并且您想要跟踪它,请使用获取的结果控制器。它们是为表视图设计的,但很简单,可以适应集合视图。
否则,请在获取请求中设置批量大小。这将确保一次只从存储中提取一定数量的记录,并且如果不访问它们将被转回故障。来自文档:
如果设置非零批处理大小,则执行提取时返回的对象集合将分为批处理。执行提取时,将评估整个请求并记录所有匹配对象的标识,但一次只能从持久性存储中提取batchSize对象的数据。从执行请求返回的数组将是一个代理对象,它根据需要透明地对批处理进行故障处理。 (在数据库术语中,这是一个内存中的游标。)