1. About delegation

First contact the multicast delegate is in c #, first of all about entrust, simple said, is the class identification method, it gives a method of the specified name, the signature and return type can be said to be the class of ascension, it is very similar to a function pointer in c + +, c # delegate is type safe, and c + + function Pointers can be any point to an address as a function, Methods can be called from a defined delegate object, which in C# looks like this:

delegate int demoDelegate(int input)
Copy the code

This is a simple delegate implementation with method parameters and return values. It can be used as Function with the same method parameter type and return value type.

int demoFunc(int input) {
     return input * input
}
Copy the code

This is just a simple function, we need to initialize the delegate and assign the method to the delegate, then we can use it later:

myDelegate = new demoDelegate(demoFunc) int newNumber = myDelegate(10); // Just like calling the method directly.Copy the code

2. About multicast delegation

So what is a multicast delegate? With the single delegate foundation above, it is easy to understand how to understand multicast delegates.

Delegatedelegate {class MulticastDelegates {// Declare a delegate, Delegate has no return value public void DemoDelegates(String name); Public static void Hello(String name) {console. WriteLine("Hello {0}", name);
        }
 
        public static void GoodBye(String name)
        {
            Console.WriteLine("Goodbye, {0}", name);
        }
 
        public static void Main() { DemoDelegates delegates = Hello; Delegates += GoodBye; // If +=... String name ="Three and four.";
            Console.WriteLine("This is a method call:");
            delegates(name);
            Console.WriteLine("This is another method call."); // Returns a list of calls to the delegate. Delegate[] allDelegates = delegates.GetInvocationList(); // The allDelegates list stores the delegates type foreach (DemoDelegates dele)inallDelegates) { dele(name); }}}}Copy the code

The results

So this is the first method of calling hello, three, four, and goodbye, three, four, and goodbye, three, four, and goodbye, three, fourCopy the code

. It’s just an array of delegates… Well. Multicast delegates have a lot of benefits because they allow us to call methods of the same type at the same time. Ha, ha, ha

3. Multicast delegation in ios

This is all done in C#, so can we use multicast delegate in ios? The answer is no… But we can do it ourselves. The simplest is to declare an array that holds a delegate, traverse the array as needed, and execute one by one. Note:

NSPointerArray* _delegates; // Store the delegateCopy the code
_delegates = [[NSPointerArray alloc] initWithOptions:NSPointerFunctionsWeakMemory]; // An array of weakly referenced objectsCopy the code
- (void)addDelegate:(id<Delegate>)observer {// Delegate: Delegate (_delegates) {if(! [_delegates containsObject:observer]) { [_delegates addObject:observer]; }} - (void) Delegate:(id<Delegate>) Delegate {// synchronized(_delegates) {[_delegates removeObject:observer]; }}Copy the code

Through the above operations, we have our own multicast delegate. Of course, we can use Notification to realize the same service, which is more convenient. However, Notification has many congenital disadvantages:

  1. [NotificationCenter defaultCenter] postNotification]. This method is synchronous, that is, after the receiver has processed the message, the POST will continue. Therefore, do not perform time-consuming operations. And the thread in which it is sent, the thread in which it is received. 2. After calling addObserver, we will also call observer remove 3. When a notification is sent, you need to look for the observer to receive the notification.

A weak reference to an array of objects ensures that when the object in the array is released, it will also be NULL, helping to avoid a memory leak.

The above is a simple understanding of multicast delegation.