littleDrummerBoy littleDrummerBoy - 2 months ago 11x
Objective-C Question

NSArrayController setFetchPredicate

I have a simple

application which stores information, names, dates, etc... and all is working. There is a search method, which uses the
on an
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
count is the entire database.

I placed a second
in the beginning of the method. If I run the search a second time, the second
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


First Run:

A-Count = 1200

B-Count = 1200

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
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


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