Re: KVO on objects in NSArrayController
On Sat, 3 Sep 2011 09:23:43 -0700, Kyle Sluder said: I understand that... I think the alternative would be to register an observer on all 8000+ objects in the array. Yes, this is the expected pattern with KVO. Contrary to what you might think at first, it is *fast*. Unless you are using garbage collection, in which case it is an order of magnitude slower than with MRC (manual ref counting). ie horribly slow. -- Sean McBride, B. Eng s...@rogue-research.com Rogue Researchwww.rogue-research.com Mac Software Developer Montréal, Québec, Canada ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: KVO on objects in NSArrayController
On 2011 Sep 02, at 22:22, Trygve Inda wrote: Briefly, you can configure the checkbox cell (either in IB or programmatically) to have an action… I think this sounds like a much better option than trying to use KVO. Indeed it is, *if* you literally want the notification, as you said in your original post, whenever the user toggles a … checkbox. However, if, as is more commonly the case, you actually need a notification whenever the 'marked' attribute in the data model changes, and if your application provides any other means by which that attribute could be changed, such as Undo, scriptability, business logic, or some other view of that attribute, which may be added in the future, then the shortcut of wiring to the control's action, although convenient, is going to be be missing these changes. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: KVO on objects in NSArrayController
On 2011 Sep 02, at 22:22, Trygve Inda wrote: Briefly, you can configure the checkbox cell (either in IB or programmatically) to have an action… I think this sounds like a much better option than trying to use KVO. Indeed it is, *if* you literally want the notification, as you said in your original post, whenever the user toggles a … checkbox. However, if, as is more commonly the case, you actually need a notification whenever the 'marked' attribute in the data model changes, and if your application provides any other means by which that attribute could be changed, such as Undo, scriptability, business logic, or some other view of that attribute, which may be added in the future, then the shortcut of wiring to the control's action, although convenient, is going to be be missing these changes. I understand that... I think the alternative would be to register an observer on all 8000+ objects in the array. For now however, the checkbox is the only way to alter the marked state so this should work. Thanks! ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: KVO on objects in NSArrayController
On Sep 2, 2011, at 23:34 , Jerry Krinock wrote: Indeed it is, *if* you literally want the notification, as you said in your original post, whenever the user toggles a … checkbox. Sure. However, if, as is more commonly the case, you actually need a notification whenever the 'marked' attribute in the data model changes, and if your application provides any other means by which that attribute could be changed, such as Undo, scriptability, business logic, or some other view of that attribute, which may be added in the future, then the shortcut of wiring to the control's action, although convenient, is going to be be missing these changes. I think characterizing it as more commonly the case is going too far. If it were really true that the most common KVO case made it desirable to observe thousands of individual objects (as opposed to observing or watching something less numerous), then we'd see a lot more complaining about KVO on this list. :) I'd say the opposite is true -- the absolute need to observe properties of every individual object in an array (especially a large array) is rare enough that it's not a great hindrance to the usefulness of KVO generally. Still, in the scenario you raise, where there's a big array and it doesn't seem feasible to observe everything in the array, then I would look for a solution that produces KVO notifications for the array itself. That is, rather than generating a KVO notification for 'array [n].object.property changed', I'd arrange to generate a KVO notification for 'array object at index n was replaced'. (And in fact, I've written code like this. Unfortunately, it's a bit tricky to get it right.) OTOH, doing this may well fall under the heading of premature optimization. We don't have any a priori reason to assume that setting up tens of thousands of observations is infeasible, or a performance or resource bottleneck. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: KVO on objects in NSArrayController
I'm coming late to this conversation, but couldn't you just add a -[MyObject setMarked:] method that would then be called to change the 'marked' attribute/property? It can do anything else it wants after making the change. Why use notifications at all? Is it some other object that needs to be notified when MyObject changes? On 2011-09-03, at 1:22 AM, Trygve Inda wrote: On Sep 2, 2011, at 19:01 , Trygve Inda wrote: I have an array of objects (of class MyObject) managed by an NSArrayController and displayed in an NSTable (via bindings). One property of MyObject is a BOOL marked. I need to be notified whenever the user toggles a marked checkbox in my NSTable. What is the best way to do this? Do I need to add an observer to every object in the array (there are about 8,000 of them). How can I best achieve this so that I get passed the changed MyObject (immediately after it changes). Briefly, you can configure the checkbox cell (either in IB or programmatically) to have an action, and possibly an explicit target. In the action method, you can use NSTableView's 'clickedRow' method to find the row and hence the object whose marked property changed, and send a message representing to the change directly to whatever object has to be notified. I think this sounds like a much better option than trying to use KVO. Thanks! ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/dave.fernandes%40utoronto.ca This email sent to dave.fernan...@utoronto.ca ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: KVO on objects in NSArrayController
On 2011 Sep 03, at 07:34, Dave Fernandes wrote: I'm coming late to this conversation, but couldn't you just add a -[MyObject setMarked:] method that would then be called to change the 'marked' attribute/property? It can do anything else it wants after making the change. Why use notifications at all? Is it some other object that needs to be notified when MyObject changes? Yes. The reason I use the notification is, of course, the same reason you always using a notification, because the observer is not known to the observee, and/or it makes for nicer encapsulation. But if you didn't have these reasons, indeed a simple message would work. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: KVO on objects in NSArrayController
I agree, it is certainly a valid and useful design pattern that I use as well. But I just wasn't sure that it is needed in the OP's case. From the part that I read (and I missed the first part of the thread), there was no mention of which objects needed to be notified. On 2011-09-03, at 10:52 AM, Jerry Krinock wrote: On 2011 Sep 03, at 07:34, Dave Fernandes wrote: I'm coming late to this conversation, but couldn't you just add a -[MyObject setMarked:] method that would then be called to change the 'marked' attribute/property? It can do anything else it wants after making the change. Why use notifications at all? Is it some other object that needs to be notified when MyObject changes? Yes. The reason I use the notification is, of course, the same reason you always using a notification, because the observer is not known to the observee, and/or it makes for nicer encapsulation. But if you didn't have these reasons, indeed a simple message would work. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/dave.fernandes%40utoronto.ca This email sent to dave.fernan...@utoronto.ca ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: KVO on objects in NSArrayController
On Sat, Sep 3, 2011 at 12:12 AM, Trygve Inda cocoa...@xericdesign.com wrote: I understand that... I think the alternative would be to register an observer on all 8000+ objects in the array. Yes, this is the expected pattern with KVO. Contrary to what you might think at first, it is *fast*. To use OmniPlan 2 as an example, each cell in each row in the outline view registers itself as an observer of one property of the model object (actually a controller-layer proxy). The row itself registers as a KVO observer of a few other properties of that proxy. OmniPlan 2 documents usually have a few hundred if not thousands of rows. We have never seen any of the KVO implementation functions appear in our sample reports during performance testing. In fact, KVO is *much* faster than using NSNotificationCenter for the same purpose. But sending one NSNotification is much faster than sending 8,000 KVO notifications. So do what's right for the situation at hand. If you need to know about the marked property of 8,000 objects, register yourself as a KVO observer for each of those 8,000 objects. --Kyle Sluder For now however, the checkbox is the only way to alter the marked state so this should work. Thanks! ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/kyle.sluder%40gmail.com This email sent to kyle.slu...@gmail.com ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
KVO on objects in NSArrayController
I have an array of objects (of class MyObject) managed by an NSArrayController and displayed in an NSTable (via bindings). One property of MyObject is a BOOL marked. I need to be notified whenever the user toggles a marked checkbox in my NSTable. What is the best way to do this? Do I need to add an observer to every object in the array (there are about 8,000 of them). How can I best achieve this so that I get passed the changed MyObject (immediately after it changes). Thanks, Trygve ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: KVO on objects in NSArrayController
On 2011 Sep 02, at 19:01, Trygve Inda wrote: What is the best way to do this? Do I need to add an observer to every object in the array (there are about 8,000 of them). If you want to use KVO, yes. I recall some sample code, written by mmalc. I forgot if it was on stepwise.com (which means it's no longer available) or on developer.apple.com. I think the concept is explained in Apple's KVO Programming Guide or whatever. Now, the reason why my memory fails me is because I don't do it this way. When I find myself needing to do gymnastics like that with KVO, I use old-fashioned NSNotification/Center/Queue instead. There are still gymnastics, but maybe only half as much. Most importantly, when a observer is going away, you can remove all of its observations with one line of code, invoking -[NSNotificationCenter removeObserver:]. If I recall correctly, with KVO you need to do careful accounting and remove each key path of each observed object. Any mistakes and you'll get KVO observations sent to dead objects; at least an exception and often a crash. No matter how few bugs you produce in an average day, you're going to have relatively fewer of them with one line of code. There is still the matter of where to put that line of code, because -dealloc is often too late, but you have the same issue KVO, and if this business was easy, everybody would be in it :)) In your particular case, you'd have your observer register for a notification named, for example TrygveMyObjectWasMarked, and post this notification in an override of -[MyObject setIsMarked:]. How can I best achieve this so that I get passed the changed MyObject (immediately after it changes). Pass it as the notification object or in the userInfo. Your desire to have it happen immediately is another topic. To get some control over how fast is immediate, you can use NSNotificationQueue and adjust the posting style. If performance becomes an issue, careful coalescing of notifications can usually fix it. I'll be interested to see if anyone posts any other ideas, or disadvantages of my design pattern which I've overlooked. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: KVO on objects in NSArrayController
On Sep 2, 2011, at 19:01 , Trygve Inda wrote: I have an array of objects (of class MyObject) managed by an NSArrayController and displayed in an NSTable (via bindings). One property of MyObject is a BOOL marked. I need to be notified whenever the user toggles a marked checkbox in my NSTable. What is the best way to do this? Do I need to add an observer to every object in the array (there are about 8,000 of them). How can I best achieve this so that I get passed the changed MyObject (immediately after it changes). Briefly, you can configure the checkbox cell (either in IB or programmatically) to have an action, and possibly an explicit target. In the action method, you can use NSTableView's 'clickedRow' method to find the row and hence the object whose marked property changed, and send a message representing to the change directly to whatever object has to be notified. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: KVO on objects in NSArrayController
On Sep 2, 2011, at 19:01 , Trygve Inda wrote: I have an array of objects (of class MyObject) managed by an NSArrayController and displayed in an NSTable (via bindings). One property of MyObject is a BOOL marked. I need to be notified whenever the user toggles a marked checkbox in my NSTable. What is the best way to do this? Do I need to add an observer to every object in the array (there are about 8,000 of them). How can I best achieve this so that I get passed the changed MyObject (immediately after it changes). Briefly, you can configure the checkbox cell (either in IB or programmatically) to have an action, and possibly an explicit target. In the action method, you can use NSTableView's 'clickedRow' method to find the row and hence the object whose marked property changed, and send a message representing to the change directly to whatever object has to be notified. I think this sounds like a much better option than trying to use KVO. Thanks! ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com