Observing NSArrayController arrangedObjects when there is a fetch predicate

  • I have an NSArrayController (in entity mode - this is a CoreData
    app),  for entities of type 'Recording' which has a fetch predicate
    set in IB :

    status == 2

    I can successfully register to observe the arrayControllers
    arrangedObjects key and during app launch I can see that I have one
    object in the arrangedObjects array.

    During app operation I change the status of an already existing
    (existed at launch) Recording instance to have a status of 2.

    However my observer callback is not called. I had hoped that changing
    the status of an object so that it now satisfied the predicate on the
    array controller would cause the observers to be notified. Is there a
    different key on the Array Controller I should be observing ?
    Something extra I should do to cause the controller to automatically
    update and call my observer ?

    Thanks

    Andrew 8-)
  • On 2007 Oct, 12, at 11:34, Andrew Kimpton wrote:

    > I had hoped that changing the status of an object so that it now
    > satisfied the predicate on the array controller would cause the
    > observers to be notified. Is there a different key on the Array
    > Controller I should be observing ? Something extra I should do to
    > cause the controller to automatically update and call my observer ?

    That array controller may be observing an entity in your managed
    object context, but it's not observing all the properties of all the
    managed objects, and even if it was, it's probably not going to
    'rearrange' its objects (that is, run its sort and filter predicates)
    in response.  Try to give it a kick with something like this...

    - (void)awakeFromNib {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                  selector:@selector
    (modelChanged:)

    name:NSManagedObjectContextObjectsDidChangeNotification
                                                    object:nil] ;

    }

    - (void)modelChanged:(NSNotification*)notification {
        [[self arrayController] rearrangeObjects] ;

        // Maybe do other business logic stuff too
        // ...
    }

    I admit that the above solution is kind of a shotgun, but the more
    focused approaches I've tried in problems like this caused me to
    scratch alot of hair off my head, and often ended up throwing
    infinite loops in obscure corner cases.  If you're lucky, some Core
    Data expert out there might have a better answer.

    Have fun.  Oh, and don't forget to removeObserver:: when you dealloc
    that thing ;)
previous month october 2007 next month
MTWTFSS
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
Go to today