32262

Core Data Optimize Fetch Request

Question:

This Fetch Request takes almost 10 seconds to execute on iPhone. The goal is to fetch 1 random thumbnail from each category. setting fetchlimit = 1 most of the times return the same thumbnail so I have to fetch all photos from each category.

Any ideas ?

[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]; }];

many thanks! Bill.

Answer1:

You don't need to pull all your objects into an array. Just change

NSArray *photosManagedObjectsArray = [managedObjectContext executeFetchRequest:request error:&error];` NSUInteger randomIndex = arc4random() % [photosManagedObjectsArray count];

to this

NSUInteger count = [managedObjectContext countForFetchRequest:request error:&error]; NSUInteger randomIndex = arc4random() % count;

Now use fetchOffset to grab just the single object you need.

[request setFetchOffset:randomIndex]; [request setFetchLimit:1];

Recommend

  • Unexpected behavior in FIRST_VALUE() with IGNORE NULLS (Vertica)
  • How to schedule Google Data Fusion pipeline?
  • TypeORM doesn't support the database setting on the entity decorator
  • Xamarin service - run according to a time schedule
  • custom navigation for different user using core UI Angular template but I am getting some weird erro
  • Android - Client-side sorting with FirebaseRecyclerAdapter
  • Ajax and returning image created by PHP GD
  • Unable to use ng-include in angular 5
  • Prevent user account creation with sign in by email in firestore
  • Clojure; select all nth element from list of lists with unequal size, for n = 1, 2,
  • Antialiasing on OpenGL ES 2.0
  • How to implement 'if' in Gherkin
  • Lazy Evaluation - Space Leak
  • Encounter error “IB API required” when IB API is installed
  • Limit # of records returned based on a form control
  • Bootstrap 3 Validation
  • How to create mirrored image effect with CSS single element
  • How to convert days into months using datetime in Python3?
  • Optimization of optim() in R ( L-BFGS-B needs finite values of 'fn')
  • how to add dashed border on highcharts “area” graph for every point
  • Autocomplete source from project settings
  • Copy and paste data from multiple workbooks to a worksheet in another Workbook
  • how to specify different css for ie
  • Year over Year Stats from a Crossfilter Dataset
  • C++ STL stack pop operation giving segmentation fault
  • Calculate time from document
  • 'url' requires a non-empty first argument. The syntax changed in Django 1.5, see the docs
  • Amazon Elastick BeanStalk error: Failed to create the AWS Elastic Beanstalk application version
  • Print Longitude and Latitude in textbox Android
  • How to turn off notice reporting in xampp?
  • iphone image to video issue in video speed
  • Functions by reference or by variable, which to use when?
  • Access to a Matlab gui from the web