这个 Fetch Request 在 iPhone 上执行大约需要 10 秒。目标是从每个类别中获取 1 个随机缩略图。设置 fetchlimit = 1 大多数时候返回相同的缩略图,所以我必须从每个类别中获取所有照片。
有任何想法吗 ?
[categoriesArray enumerateObjectsUsingBlock:^(id object, NSUInteger index, BOOL *stop) {
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Photos"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"place.subcategory.category == %@", object];
[request setPredicate:predicate];
NSError *error;
NSArray *photosManagedObjectsArray = [managedObjectContext executeFetchRequest:request error:&error];
NSUInteger randomIndex = arc4random() % [photosManagedObjectsArray count];
NSManagedObject *photoObject = [photosManagedObjectsArray objectAtIndex:randomIndex];
UIImage *photoImage = [UIImage imageWithData:[photoObject valueForKey:@"thumbnail"]];
UIImage *resizedImage = [photoImage imageCroppedToFitSize:CGSizeMake(IMAGE_WIDTH, IMAGE_HEIGHT)];
[imagesArray addObject:resizedImage];
[objectsArray addObject:photoObject];
}];
非常感谢!账单。