iOS Design Patterns - Part 6: Auto Re-Login | Ray Wenderlich

You'll use the multicast closure delegate pattern from the previous video to create an auto re-login authentication client in this video.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/3816-ios-design-patterns/lessons/6

There is no “Download materials” button.

We’re getting this sorted out now… it should be up soon!

Sorry about the mixup here. :confused:

I found Multicast Delegate example Part 5 and 6 a little overcomplicated.

I liked the way you handle weak references with NSMap (very good tip) but I’m struggling yet to put it in order in my mind.

After the Part 5 and 6 I found this solution
“easier to understand”.

NSHashTable can hold weak references to its members also.
We use NSMap because of NSMapTable can hold keys and values with weak references, in such a way that entries are removed when either the key or value is deallocated ?

Just in case that link dies later, here’s what this comment refers to:

class MulticastDelegate <T> {
  private let delegates: NSHashTable<AnyObject> = NSHashTable.weakObjects()
  
  func add(delegate: T) {
    delegates.add(delegate as AnyObject)
  }

  func remove(delegate: T) {
    for oneDelegate in delegates.allObjects.reversed() {
      if oneDelegate === delegate as AnyObject {
        delegates.remove(oneDelegate)
      }
    }
  }

  func invoke(invocation: (T) -> ()) {
    for delegate in delegates.allObjects.reversed() {
      invocation(delegate as! T)
    }
  }
}

func += <T: AnyObject> (left: MulticastDelegate<T>, right: T) {
  left.add(delegate: right)
}

func -= <T: AnyObject> (left: MulticastDelegate<T>, right: T) {
  left.remove(delegate: right)
}

Yes, I like this solution too!

The difference between this MulticastDelegate and MulticastClosureDelegate shown in this series is that MulticastDelegate requires that you define a delegate protocol. That’s what <T> in class MulticastDelegate <T> is all about.

MulticastClosureDelegate instead uses closures, so it doesn’t require you to define a delegate protocol.

To which you might wonder, “Which should I use?”

I personally use both! Sometimes, even in the same app. :smile:

“When show I use each?”

Another good question-- thanks for being such a good sport. :wink:

I usually use MulticastDelegate for long-lived multicast delegate relationships. Whereas, I use the MulticastClosureDelegate for short-lived delegate relations (think, “call once and forget about it”).

There’s no reason why you couldn’t use MulticastClosureDelegate for long-term delegates, however, if you wanted to do it. :]

In that case, it’d likely make more sense to change some of the default parameter values, so the closures won’t automatically get removed (as they do now) by default.

Make sense?

Also, here’s a similar implementation of MulticastDelegate, which includes comments explaining how it works (for disclosure: I contributed to this):

This is what I use in my own projects, and it has a CocoaPod too. :]

At 7:46 there is a loud click that messed up my ears for minutes. RIP headphone users.