Where to implement alert ?

  • Hi,

    I have what I think is a very simple design question for which I would like to get input from some of the experts on this list.
    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?

    Thanks for any advice.

    --
    Luc Van Bogaert
  • 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.
previous month january 2012 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