NSSortDescriptor on transient attribute for NSFetchedResultsController
Asked Answered
H

1

9

Ok, I initially wanted to make NSSortDescriptor of a request for NSFetchedResultsController to sort based on the property in my NSManagedObject subclass, but It obviously won't do it, because NSFetchedResultsController is limited to predicates and sort descriptors that work on the fetched entity and its relations, so I decided to create a transient attribute in my data model, synthesis the property for this attribute to ivar in my NSManagedObject subclass, and sort based on it.

When running it, i got while executing fetch 'NSInvalidArgumentException', reason: 'keypath isActive not found in entity <NSSQLEntity SMSourceEntity id=2>'

I know this is KVO issue, so I have added + (NSSet*)keyPathsForValuesAffectingIsActive, but still have the same issue.

What did I do wrong, or I'm still missing something to make it find my keypath? Thanks.

code:

@implementation SMSourceEntity

@dynamic friendlyName;
@dynamic interfaceAddress;
@dynamic uniqueID;
@dynamic network;
@synthesize isActive = _isActive;

+ (NSSet*)keyPathsForValuesAffectingIsActive
{
    return [NSSet setWithObject:@"isActive"];
}

@end

my sortDescriptor:

request.sortDescriptors = [NSArray arrayWithObjects:[NSSortDescriptor sortDescriptorWithKey:@"isActive" ascending:NO] , nil];
Halcomb answered 25/7, 2013 at 9:2 Comment(0)
T
18

It isn't a KVO issue, it's an issue with what you're trying to do because the FRC requires that the sort can be applied to the underlying SQLite store. In other words, you can only filter and sort on non-transient attributes. You will need to make the attribute non-transient so that it's value is saved into the store and available to SQLite.

For the FRC, only the section name key path attribute can be transient.

Trakas answered 25/7, 2013 at 9:4 Comment(2)
Thanks, so If I would ever want transient properties work with my FRC, I would have to subclass it, get fetched results, then sort the array of fetched object based on my property in didChangeContent:? Does it make sense?Halcomb
I think it would be very hard to do, because you would have to write a lot of code or throw away most of the benefits of using an FRC (like paginated on-demand loading). You should probably consider other options on a case-by-case basis. Like using multiple FRCs for different sections or something like that.Trakas

© 2022 - 2024 — McMap. All rights reserved.