CoreData ordered relationships - batch unfaulting using NSFetchRequest
Asked Answered
A

2

7

Background - Batch unfaulting:
NSFetchRequest allows batch unfault - for example, use a query of 1000 results, it would bring all as faults, then it would unfault X objects at a time (i.e. index 0-20, then 21-40, etc)

This behavior is great when used in NSFetchResultsController for a UITableViewDataSource, and it allows fast UI scrolling as it doesn't unfault objects one-by-one.

Now to my problem:
I'm using ordered relationships for lists of objects, let's say Posts.

Since a Post may appear on a lot of lists on my model, I can't store its index in every lists on Post entity and use it as a param for ordering results.

As for now, I haven't found a way for NSFetchRequest to fetch according to this order, so I can't use its batch unfaulting. So I'm addressing to the relationship with an index, and I end up unfaulting one-by-one, which causes bumpy scrolling.

Is there any way for NSFetchResultsController to fetch according to order relationships? Or, is there a batch unfaulting API which isn't private?

Anthropology answered 18/6, 2013 at 8:34 Comment(0)
A
3

Currently I have a solution for that, but not a clean one:
I want to batch unfault by groups of 20, in an ordered relationship.

So, every time i address an index that its index divides by 20 (index % 20 == 0), I use a new NSFetchRequest for the next 20 and unfault them by calling a 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's 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;
}
Anthropology answered 25/6, 2013 at 8:19 Comment(0)
S
1

I hope I understood your question correctly,

I found the answer, please look the answer and comments of adonoho

NSFetchedResultsController and NSOrderedSet relationships

Seizure answered 20/6, 2013 at 17:16 Comment(1)
Sorry, but as i said, on my case, the post may appear on unknown (large) number of lists, so i can't hold its index on every list.Anthropology

© 2022 - 2024 — McMap. All rights reserved.