littleDrummerBoy littleDrummerBoy - 3 months ago 13
Objective-C Question

NSArrayController setFetchPredicate

I have a simple

CoreData
application which stores information, names, dates, etc... and all is working. There is a search method, which uses the
setFetchPredicate
on an
NSArrayController
and the results appear in a table - this is working.

Looking at adding some addition code at the end of this method, so just before the method ends, I added a simple line to get the count of the NSArrayController.

[searchArrayController setFetchPredicate: searchPredicate];

[searchArrayController rearrangeObjects];

NSlog(@"Count = %lu", (unassigned long)[[searchArrayController arrangedObjects] count]);


Results: Count = 1200

(1200 is the number of objects in the database.)

The table displays correctly, i.e. show the correct number of data objects, however the
NSLog
count is the entire database.

I placed a second
NSLog
in the beginning of the method. If I run the search a second time, the second
NSLog
shows the correct number of objects.

Beginning method:
NSlog(@"A-Count = %lu", (unassigned long)[[searchArrayController arrangedObjects] count]);
...

[searchArrayController setFetchPredicate: searchPredicate];

[searchArrayController rearrangeObjects];

NSlog(@"B-Count = %lu", (unassigned long)[[searchArrayController arrangedObjects] count]);
end of method


Results:

First Run:

A-Count = 1200

B-Count = 1200

NSTable
displays correct number of objects

Second Run:

A-Count = 4 (This is correct for the search parameters)

B-Count = 4

It appears the first time though the method the NSArrayController has not yet been filtered based on the predicate when the B-Count
NSLog
is called. But this is at the end of the method, the NSArrayController should be filtered at this point.

I've got to be missing something simple here - any help would be appreciated.

Thank you

Answer

rearrangeObjects uses filterPredicate. fetch uses fetchPredicate but fetch is deferred until the next iteration of the runloop.

Comments