Correct CoreData Relationships - Batch Crash Using NSFetchRequest

Background - Error Package:
NSFetchRequest allows batch unauthorized use, for example, using a query of 1000 results, it will lead to all errors, then it will distort X objects at the same time (i.e. Index 0-20, then 21-40, etc.)

This behavior is great for use in NSFetchResultsController for UITableViewDataSource and allows you to quickly scroll through the user interface because it does not protect objects one by one.

Now my problem is:
I use ordered relationships for lists of objects, for example, messages.

Since Post can appear in many lists of my model, I cannot save its index in each list on the Post object and use it as a parameter to organize the results.

So far, I have not found a way for NSFetchRequest to get the selection in that order, so I cannot use its batch reject. Therefore, I turn to relations with the index, and in the end I find myself unable one after another, which leads to uneven scrolling.

Is there any way that NSFetchResultsController retrieves according to order relationships? Or is there a batch immune API that is not private?

+7
source share
2 answers

I currently have a solution for this, but not a clean one:
I want the package to be unordered in groups of 20 in an ordered relationship.

So, every time I access the index, its index divides by 20 (index% 20 == 0), I use the new NSFetchRequest for the next 20 and do not fix them by calling the common field name.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row % 20 == 0) { NSArray *objectsToUnfault = [[someObject.orderedRelationship array] subarrayWithRange:NSMakeRange(indexPath.row, MIN(20, [someObject.orderedRelationship count] - indexPath.row))]; // It important to hold on this array, otherwise objects fault back self.lastPrefetch = [self preFetchEntityOfClass:[Post class] faultObjects:objectsToUnfault error:&error]; } //... continue and create cell } - (NSArray *)preFetchEntityOfClass:(Class)entityClass faultObjects:(NSArray*)objects error:(NSError **)error { NSEntityDescription *entityDescription = [NSEntityDescription entityForName:NSStringFromClass(entityClass) inManagedObjectContext:self.managedObjectContext]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF in %@", objects]; NSFetchRequest *request = [[NSFetchRequest alloc] init]; [request setEntity:entityDescription]; [request setPredicate:predicate]; [request setFetchBatchSize:MIN([objects count], 20)]; NSArray *results = [self.managedObjectContext executeFetchRequest:request error:error]; // I assume all my objects has this field "uid" NSArray *resultsUid = [results valueForKey:@"uid"]; //Batch unfaulting (results is a BatchFaultArray, private class type) if ([resultsUid count] != [results count]) { NSLog(@"Error: wrong count of uids"); //We need to use resultsUid, to avoid compiler opt } return results; } 
+3
source

I hope I understood your question correctly,

I found the answer, please see the answer and comments adonoho

relationship NSFetchedResultsController and NSOrderedSet

+1
source

All Articles