estou usando UICollectionView com NSFetchResultsController para apresentar os diferentes conjuntos de fotos. Esta é a primeira vez que uso os dois UICollectionView E NSFetchResultsController.
Aqui está o meu código:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"CellIdentifier";
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
MyPhoto *myPhoto = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.imageView.image = [UIImage imageWithData:myPhoto.photoData];
return cell;
}
- (NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController != nil)
{
return _fetchedResultsController;
}
/*
Set up the fetched results controller.
*/
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSManagedObjectContext *moc = [[CoreDataManager sharedInstance] managedObjectContext];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"MyPhoto" inManagedObjectContext:moc];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:40];
// Sort using the timeStamp property.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"sectionName" ascending:YES];
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:YES];
[fetchRequest setSortDescriptors:@[sortDescriptor, sortDescriptor1]];
// Use the folderName property to group into sections.
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:moc sectionNameKeyPath:@"sectionName" cacheName:@"Root"];
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
}
Quando eu tenho mais seções e tento rolar a exibição, não está rolando suavemente. Estou perdendo alguma coisa?
Respostas:
1 para resposta № 1Não há diferença entre usar NSFetchedResultController com TableView ou CollectionView.
Vejo poucas coisas em seu código -
1) BatchSize:40
- quanto maior o tamanho do lote, mais tempo leva para buscar. A busca não será tão frequente, mas levará mais tempo. tente defini-lo para 20, por exemplo. A rolagem deve ser mais sufocante.
2) Sua entidade é foto.
- Certifique-se de não armazenar BLOB (big data) como valores. Isso tornará a busca lenta. Se você precisar armazenar a imagem no conjunto CoreData (Armazenar em arquivo de armazenamento externo), digite em seu modelo.
- Faça uma miniatura da imagem.Se você precisar mostrar uma imagem de tamanho pequeno, faça uma miniatura e salve-a diretamente na chave CoreData (não use "Armazenar em arquivo de armazenamento externo"). Isso tornará a busca muito rápida, pois você não usará nenhum arquivo externo e tamanho de preenchimento de foto ser pequeno.
- pré-busca de dados. Se você tiver algumas subentidades de foto e as estiver usando,
NSString *relationshipKeyPath = @"bObjects"; // Set this to the name of the relationship on photo
NSArray *keyPaths = [NSArray arrayWithObject:relationshipKeyPath];
[request setRelationshipKeyPathsForPrefetching:keyPaths];