Re: Where to implement alert ?
Re: Where to implement alert ?
- Subject: Re: Where to implement alert ?
- From: Quincey Morris <email@hidden>
- Date: Tue, 24 Jan 2012 16:16:25 -0800
On Jan 24, 2012, at 13:03 , Luc Van Bogaert wrote:
> Assume I have a document object with an array object as a property, and an associated window controller, with a removeObject: method which is called from the menu. If I wanted to alert the user before actually removing an object from the document's array, what is considered the best place to implement the NSAlert code: in the windowController's removeObject: method, or in the document's removeFromObjectsAtIndexes: method, thus do we generally add the NSAlert at the 'higher' or at the 'lower' level, if it matters at all?
Not in 'removeFromObjectsAtIndexes:' -- that's part of the KVC machinery for your data model array property, and you don't really want to pollute it with a possible UI interaction. Furthermore, in general you would want your property implementation to work on any thread, so a dependency on the main thread (for the UI interaction) is a hindrance.
Instead, you want to issue the alert at a place in your code path where user interaction is safe, which is to say in code belonging to the action method, 'removeObject:'. You have two choices here: put the action method in either the window controller or the document. In a way, it doesn't matter which, though you do have to make sure you implement the action method in the same class that validates the menu item. (The window controller may seem like the "obvious" place, but this particular action sounds more like a document-related operation rather than a window-related operation.)
Note that in the general case, where you might have multiple windows (and window controllers) on the same document, you'll need to synchronize multiple simultaneous deletions. That is, if the user starts a deletion on one window, bringing up the alert, then switches to a different window on the same document and starts another deletion, you'd probably want make sure your app didn't crash when the second deletion finally applies.
Of course, your current app may have nothing to do with multi-threaded access to the document or multiple window controllers per document, but it's worthwhile not shutting yourself out of these possible improvements in the future.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden