Re: localized compare and SQLite persistent store
Re: localized compare and SQLite persistent store
- Subject: Re: localized compare and SQLite persistent store
- From: Melissa Turner <email@hidden>
- Date: Thu, 11 Aug 2005 13:20:37 -0700
On Aug 10, 2005, at 18:02, Clifford Crawford wrote:
Hello,
I'm getting a weird problem involving NSSortDescriptor (using the
localizedCaseInsensitiveCompare: method) and SQLite. My app has a
table view which automatically sorts every time a new entry is
added, which I set up by binding the array controller's
sortDescriptors to the following method in my app delegate:
- (NSArray *)wordSortDescriptors {
if (!wordSortDescriptors) {
wordSortDescriptors = [NSArray arrayWithObjects:
[[NSSortDescriptor alloc] initWithKey:@"word"
ascending:YES
selector:@selector
(localizedCaseInsensitiveCompare:)],
[[NSSortDescriptor alloc] initWithKey:@"gloss"
ascending:YES
selector:@selector
(localizedCaseInsensitiveCompare:)],
nil];
}
return wordSortDescriptors;
}
When I use an XML-based persistent store, this works fine; however,
when I switched to using SQLite, I get the following error on app
startup:
NSRunLoop ignoring exception 'unsupported NSSortDescriptor selector:
localizedCaseInsensitiveCompare:' that raised during posting of
delayed perform with target
3e5f70 and selector 'invokeWithTarget:'
and none of the data displays in the table view. If I comment out
the "selector:..." parts in the wordSortDescriptors method above,
however, then the app *does* work using SQLite, but now the entries
in the table view aren't sorted properly (all of the entries
contain Japanese text, so I need to use localized compare). Does
anyone know what I'm doing wrong here? Why would the type of
persistent store I'm using affect whether or not I can call the
localizedCaseInsensitiveCompare: method??
Short answer:
Because the SQL store doesn't know anything about
localizedCaseInsensitiveCompare:
Longer explanation:
When your array controller pulls its contents from the database, it
does so using an NSFetchRequest with the entity you are requesting,
any predicate you have set to specify which instances of that entity
you want, and any sort descriptors you have set to describe how the
data should be brought back.
In the XML, binary and in memory stores, the evaluation of the
predicate and sort descriptors happens in memory, where we have full
access to the obj-C runtime, Cocoa, and all the goodness therein,
including the comparison methods on NSString.
The SQL store on the other hand, compiles the predicate and sort
descriptors to SQL, and evaluates the result in the database itself.
This is done primarily for speed; databases are much faster at this,
it's what they're designed for. But it means that evaluation is
happening in a non-Cocoa world, and so sort descriptors (or
predicates) which rely on Cocoa simply can't work, and result in
exceptions like the one you're seeing.
You may need to subclass NSArrayController so you can have it not
pass the sort descriptors to the database and do the sorting after
your data has been fetched instead.
+Melissa
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden