Re: [Pyobjc-dev] Key Value Observing of lists?
Brought to you by:
ronaldoussoren
|
From: Orestis M. <or...@or...> - 2008-06-01 21:25:44
|
>> I have a workaround where I surround changes to the list with:
>>
>> self.willChangeValueForKey_("selections")
>> self.selections.append("new selection")
>> self.didChangeValueForKey_("selections")
>>
>> This works fine for now, but gives me the following messages in the
>> Console:
>>
>> *** Ignoring *** addObserver:forKeyPath:options:context: for
>> 'partName' (of <NSTableBinder: 0x3fad90>{object: <NSTableView:
>> 0x3eabc0>, bindings: content=arrangedObjects} with 0 in 0x0).
>>
>>
> I don't think I've seen this before, but I usually use
> NSMutableArray instances when dealing with KVO.
>
> Could you try to reproduce this problem in small script? Ideally
> this would be a unittest testcase, but a GUI program would be fine
> too.
OK, I've done some further investigation. This probably was a result
of my confusion about how pyobjc works with KVO.
This was only appearing when the items in the list where actual Python
objects.
Sample Code:
class Hello(NSObject):
label = objc.ivar()
def init(self):
self = NSObject.init(self)
self.label = "hello"
return self
class World(object):
def __init__(self):
self.label = "world"
class KVO_ListsAppDelegate(NSObject):
list = objc.ivar()
def init(self):
self = NSObject.init(self)
self.list = [Hello.alloc().init(), World()]
return self
def applicationDidFinishLaunching_(self, sender):
NSLog("Application did finish launching.")
self.list[0].label = "goodbye"
self.list[1].label = "people"
NSLog("adding value")
self.willChangeValueForKey_("list")
self.list.append(Hello.alloc().init())
NSLog("added value")
self.didChangeValueForKey_("list")
NSLog("done")
The log is:
[Session started at 2008-06-01 22:23:18 +0100.]
2008-06-01 22:23:19.786 KVO_Lists[1407:10b] *** Ignoring ***
addObserver:forKeyPath:options:context: for 'label' (of
<NSTableBinder: 0x1f2f9e0>{object: <NSTableView: 0x1f178e0>, bindings:
content=arrangedObjects} with 0 in 0x0).
2008-06-01 22:23:20.029 KVO_Lists[1407:10b] Application did finish
launching.
2008-06-01 22:23:20.032 KVO_Lists[1407:10b] adding value
2008-06-01 22:23:20.033 KVO_Lists[1407:10b] added value
2008-06-01 22:23:20.035 KVO_Lists[1407:10b] *** Ignoring ***
removeObserver:forKeyPath: for 'label' (of <NSTableBinder:
0x1f2f9e0>{object: <NSTableView: 0x1f178e0>, bindings:
content=arrangedObjects}).
2008-06-01 22:23:20.036 KVO_Lists[1407:10b] done
2008-06-01 22:23:20.049 KVO_Lists[1407:10b] *** Ignoring ***
addObserver:forKeyPath:options:context: for 'label' (of
<NSTableBinder: 0x1f2f9e0>{object: <NSTableView: 0x1f178e0>, bindings:
content=arrangedObjects} with 0 in 0x0).
Notice how there are three objects in there, but only one complaint.
So I guess Python objects don't play well with KVO at the moment, and
I should use NSObjects where possible, right?
Regards,
Orestis
|