开发者

Core Data fetches based on properties of 'ordered' relationships

开发者 https://www.devze.com 2023-01-26 22:32 出处:网络
My app has smart folder like functionality: a predicate is setup with a NSPredicateEditor and used to fill the folder with a fetch request.

My app has smart folder like functionality: a predicate is setup with a NSPredicateEditor and used to fill the folder with a fetch request.

The entity used in the search has a to-many relationship. The relationship is ordered, in the sense that an index is stored in the destination entity for s开发者_运维问答orting purposes.

My problem is that I would like to build in a rule based on the last values in the ordered relationship, but I can't figure out how to build a predicate to do this, because the relationship is not an array. Core data doesn't actually know about the order.

I have a readonly property on the class that returns the ordered items, but this doesn't seem to help with the fetch request because the property is not available in the core data store.

The only option I can think of is to de-normalize and store the last items in the relationship ordered in a separate property. Is that the only solution?


Well, assuming I have understood the problem correctly, I'd do it like this. Lets say you've got two entities, TopEntity has a (NSString *)name property and a to-many relationship to MyEntity which has a (NSString *)data property and (NSInteger)order property.

Lets say you want the TopEntity objects which match a given string, and whose MyEntity orders are satisfy a certain condition, then you can do it with two predicates and an NSFetchRequest like so....

NSManagedObjectContext *context = [self managedObjectContext];

// Create some top level entities
TopEntity *aTop = [TopEntity insertInManagedObjectContext:context];
aTop.name = @"This is Your Name";
TopEntity *bTop = [TopEntity insertInManagedObjectContext:context];
bTop.name = @"This aint a Name";    
TopEntity *cTop = [TopEntity insertInManagedObjectContext:context];
cTop.name = @"This is My Name";    

// Add some data
NSInteger i, len = 30;
for(i=0; i<len; i++) {
    // Create a new object
    MyEntity *entity = [MyEntity insertInManagedObjectContext:context];
    entity.orderValue = i;
    entity.data = [NSString stringWithFormat:@"This is some data: %d", i];
    if(i < 10) {
        [aTop addObjectsObject:entity];
        [entity addTopObject:aTop];
    } else if (i < 20) {
        [bTop addObjectsObject:entity];
        [entity addTopObject:bTop];            
    } else {
        [cTop addObjectsObject:entity];
        [entity addTopObject:cTop];                        
    }
}

// Save the context
NSError *error = nil;
[context save:&error];

// A predicate to match against the top objects
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH %@", @"This is"];
// A predicate to match against the to-many objects
NSPredicate *secondPredicate = [NSPredicate predicateWithFormat:@"ANY objects.order < %d", 5];
NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
[fetch setEntity:[NSEntityDescription entityForName:@"TopEntity" inManagedObjectContext:context]];
[fetch setPredicate:predicate];
NSArray *result = [[context executeFetchRequest:fetch error:&error] filteredArrayUsingPredicate:secondPredicate];


for(TopEntity *entity in result) {
    NSLog(@"entity name: %@", entity.name);         
}

So, essentially you can just wrap the results of your fetch request with another predicate and use the ANY keyword.

I've got no idea how efficient that is, but it works for this case. Running the above will output "This is Your Name" i.e. it matches the first TopEntity.


I don't think there's a way to limit to n results in a predicate, only at the fetch request level.

Aside from referencing the last n items in a relationship as you mentioned, you might try a boolean attribute "lastN" and flip them on/off when you curate the order of the list (say, during user-initiated sort or drag-and-drop reordering).

Alternatively, you could create a separate fetch request for each searched thing that sorts by your sort key, ordered descending, and is limited (via -setFetchLimit: ) to n results.

Tracking this as a relationship or an attribute is somewhat "messy" whereas the fetch limit is more expensive (because of multiple round trips). If your reordering is done by one-off user actions, it might be better performance-wise to use the relationship or attribute approach since the work is amortized rather than done all at once in a series of fetches. I haven't found a better way myself and will follow this one closely. :-)

0

精彩评论

暂无评论...
验证码 换一张
取 消