IOS multicast delegate (GCDMulticastDelegate), iosdelegate

Source: Internet
Author: User
Tags call back notification center

IOS multicast delegate (GCDMulticastDelegate), iosdelegate

In IOS, there are several methods to implement callback:

Among the above four types, delegate and block are commonly used in my own projects.

In reality, callback requirements are also divided into two types

One-to-one callback can be implemented using delegate and block in IOS. The one-to-many callback is basically the notification center.

 

If you have a requirement, we will use image download as an example. This section first ignores SDWebimage and other third-party class libraries that have been encapsulated. The general process of image download is as follows:

The difficulty here is the callback. If the same image needs to be displayed in multiple places on a page, it is inevitable that there are multiple requests to download images that agree to the url at the same time, after the download is complete, the image must be displayed in multiple places at the same time. If such a requirement is met, it seems difficult to solve it with the existing solution. Some students think of the notification center, but the notification center is actually a broadcast service. As long as you register to receive the notification, all registrants will receive the notification, but in fact, I only need to give a notification after the image of the url I want to download is downloaded, instead of all download events. At this time, we need multicast delegation.

What is multicast delegation? I will explain it directly using a definition from other blogs. In short, multicast delegation is the ability to create call lists or linked lists for methods. When the multicast delegate is called, all methods in the list are automatically executed.

In IOS, I take the most commonly used delagate as an example. A normal delegate can only be one-to-one callback, but cannot implement one-to-many callbacks. The multicast delegate is an extension and extension of delegate. It adds a process of registration and cancellation, and any object that requires callback must be registered first.

In IOS, how does one implement multicast delegation? Foreigners have already written this article and it is quite easy to use. I first came into contact with IOS multicast delegation when I was studying XMPPframework, and multicast delegation was one of the core aspects of the XMPPframework architecture. The specific class name is GCDMulticastDelegate. It can be seen from the name that this is a multi-threaded multicast delegate. Why is multithreading supported? I understand that multiple callbacks may not be in the same thread. For example, when I register a callback, it is in the background thread, but when you call back, it is in the UI thread, then there may be problems. Therefore, you must ensure the thread on which you registered during registration, so the callback must still be performed on that thread.

Next I will explain how to use the write multicast delegate in IOS.

Let me give you an example. For example, there is a UserInfo class with a userName attribute. There are three lable and one button on the page, when you click the button, assign a value to the userName attribute of userInfo. At this time, the three lables simultaneously display the value of the userName attribute of userInfo.

For the above process, we need to register each lable with the userInfo instance, that is, to register with the multicast delegate. Call the multicast delegate method when assigning values to the userName of userInfo. The setText method is called here. In this way, the above requirements can be met.

The code indicates:

// Inherit from the userInfo class @ interface UserInfo: MulticastDelegateBaseObject @ property (nonatomic, strong) NSString * userName; @ end @ implementation UserInfo-(void) of the multicast delegate base class) setUserName :( NSString *) userName {_ userName = userName; [multicastDelegate setText: userName]; // call multicast delegate} @ end
-(Void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // initialize a userinfo instance userInfo = [[UserInfo alloc] init]; // Add a lable UILabel * lable = [[UILabel alloc] initWithFrame: CGRectMake (0, 20,100, 30)]; lable. backgroundColor = [UIColor blueColor]; lable. textColor = [UIColor blackColor]; [userInfo addDelegate: lable delegateQueue: dispatch_get_main_queue ()]; // register [self. view addSubview: lable]; lable = [[UILabel alloc] initWithFrame: CGRectMake (0, 60,100, 30)]; lable. backgroundColor = [UIColor blueColor]; lable. textColor = [UIColor blackColor]; [userInfo addDelegate: lable delegateQueue: dispatch_get_main_queue ()]; [self. view addSubview: lable]; lable = [[UILabel alloc] initWithFrame: CGRectMake (0,100,100, 30)]; lable. backgroundColor = [UIColor blueColor]; lable. textColor = [UIColor blackColor]; [userInfo addDelegate: lable delegateQueue: dispatch_get_main_queue ()]; [self. view addSubview: lable]; // Add a button UIButton * btn = [[UIButton alloc] initWithFrame: CGRectMake (200, 20,100, 50)]; [btn setBackgroundColor: [UIColor blueColor]; [btn setTitle: @ "button1" forState: UIControlStateNormal]; [btn addTarget: self action: @ selector (btnCLicked :) forControlEvents: UIControlEventTouchUpInside]; [self. view addSubview: btn];}-(void) btnCLicked :( UIButton *) btn {userInfo. userName = @ "123456"; // assign a value to userInfo}

 

Click to download source code

 


How to Set delegation for iOS

Which delegate do you want to set? Control-triggered event delegation or Protocol delegation?
 
How to use protocol and delegate (protocol and delegate) in ios

Generally, different object calls are most commonly used. protocol processing is required for callback processing ~
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.