NSNotification or Delegate

Discussion in 'iOS Programming' started by erdinc27, Dec 1, 2016.

  1. erdinc27 macrumors regular

    Joined:
    Jul 20, 2011
    #1
    I develop an shopping application. This is first time i develop such an application. In a point i am a little confused so maybe someone here to help me. I have an UITableView that i display the products and there will be a basket that user put the product that he wants to buy. I want to create an object(e.g CartManager and it will be singleton) that object will hold the products added and the count etc. My question is that lets say the user added two or three different products in basket. At that point should i use Delegate or NSNotification to inform the ViewControllers that display products ? I hope i could describe my question well.
     
  2. dantastic macrumors 6502a

    dantastic

    Joined:
    Jan 21, 2011
    #2
    Think of Delegates as an intimate one-to-one relationship. You are communicating a vital piece of information back to a single point and the information must not get lost. As an example you might have a stepper in the tableViewCell so the user taps the stepper and orders 3 Gin Tonics, the buy button in the tableViewCell calls a Delegate method in the ViewController to order 3 Gin Tonics. Think of this as information sent reliably that mustn't get lost.

    A Notification is a broadcast, you stand at the top of the bar and shout your order at all the bartenders. Probably not what you want as you could end up with a couple more GTs than you were hoping for.

    Think of Notifications as passing a small piece of info and it doesn't matter if it gets lost (It's not going to get lost but think of it that way). So in your case, the shopping cart singleton should be sending Notifications that there are now 3 GTs in the shopping basket. Then if you have coach marks in your UI they can listen to the notifications and update their coach marks. As you can see here, not the end of the world if a Notification would get lost.

    You do have another options as well. Key Value Observing. Your coach marks could subscribe to an Integer property in your shopping cart singleton and they would get notified when this is updated. I wouldn't suggest it in this case but I'm just putting it out there as another option.
     
  3. erdinc27 thread starter macrumors regular

    Joined:
    Jul 20, 2011
    #3
    Thank you for your explanation. I was almost sure that i should use Delegate for that example. Because there will not be one to many broadcasting. There will be only one CartManager and a Cart that updates the manager and there will be an UIViewController that will show the products and the counts if the user has same in cart. After your explanation now i think i will use Delegate.
    One more question. Which one is more suitable for the MVC ? Delegate or NSNotification ?
     
  4. dantastic macrumors 6502a

    dantastic

    Joined:
    Jan 21, 2011
    #4
    A Delegate is more conforming to the pattern in a strict sense. But never be too strict about patterns, you end up doing really stupid stuff just to satisfy some notion of how it should be. If it makes sense to use a notification, hey, use a notification.
     
  5. cMacSW macrumors regular

    Joined:
    Mar 20, 2006
    #5
    Agree with dantastic, I would also add, that I have found delegates easier to debug.
     

Share This Page