proper technique for CoreData SQLite fetch with sort descriptors
proper technique for CoreData SQLite fetch with sort descriptors
- Subject: proper technique for CoreData SQLite fetch with sort descriptors
- From: Jim Thomason <email@hidden>
- Date: Fri, 25 Sep 2009 10:01:32 -0500
First off, I'll say that I'm trying to maintain Tiger compatibility in
my app, so if anyone tries to duplicate this on Leopard or Snow
Leopard, they may not have any luck.
I have an NSArrayController bound to a set of objects in an SQLite
CoreData store. The NSArrayController by default has several
NSSortDescriptors associated with it to properly order the objects
(since there are several properties to sub-sort on).
One of those, however, causes it to blow up badly - specifically, I
have it set on keyPath "creator.classRank", where classRank is a
method sitting up in my object class that isn't appropriate to stick
into my data store. I get an error about an unresolved keyPath for
creator.classRank. Some digging around yielded this post:
http://lists.apple.com/archives/Cocoa-dev/2005/Aug/msg00797.html
Which is basically the same issue that I'm having - in my case, it's
the keyPath that doesn't exist in the SQLite store that's causing the
fetch to fail. To address it, I overrode fetchWithRequest:merge:error:
in my NSArrayController subclass, as such:
- (BOOL)fetchWithRequest:(NSFetchRequest *)fetchRequest
merge:(BOOL)merge error:(NSError **)error {
//Make a copy of the fetchRequest's sortDescriptors
NSMutableArray* safeSortDescriptors = [NSMutableArray
arrayWithArray:[fetchRequest sortDescriptors]];
//pull out the classRankSortDescriptor (this is a static class
variable which is used initially to populate the controller's
sortDescriptors during awakeFromNib
[safeSortDescriptors removeObject:classRankSortDescriptor];
//reset the sort descriptors to the sanitized version, without the
offending descriptor
[fetchRequest setSortDescriptors:safeSortDescriptors];
//carry on our merry way
return [super fetchWithRequest:fetchRequest merge:merge error:error];
}
This works fine, and I'm not seeing any issues. Proper behavior on
Tiger, Leopard, and Snow Leopard. The fetch executes just fine, and
then the original sort descriptors (with creator.classRank) in my
NSArrayController subclass sort my data appropriately.
I have two questions -
1) Is this the proper technique to use to address this issue? Or is
there a different method I should subclass or technique I should use
instead? This feels kinda hackish, so I want to be sure I'm doing it
the right way.
2) I didn't seem to have any problems just leaving the new sort
descriptor in place on either Leopard or Snow Leopard, this is purely
a Tiger issue. Can anyone confirm if this behavior has changed and
this hack is no longer necessary on Leopard or higher? I wasn't sure
if the behavior had changed to allow this, or if I'd just gotten lucky
somehow due to an unrelated changed in the OS.
Thanks,
-Jim.......
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden