The method of implementing multi-agent IOS and the example code _ios

Source: Internet
Author: User

iOS implementation multi-agent

What is multi-agent

Use the Ring Letter SDK students should be more than a stranger to the agent, please see the following code:

 @method
 @brief Register a listener to the listening list
 @discussion Add the Listener object to the listening list and prepare to receive the corresponding event
 @param delegate the listening object that needs to be registered
 @param Queue notification thread
 @result/
-(void) AddDelegate: (id<emchatmanagerdelegate>) delegate when listening on an object Delegatequeue: (dispatch_queue_t) queue;

Usually we write more agents:

@property (Nonatomic,weak) id<emchatmanagerdelegate>delegate;

When the above attributes are written, the system will generate the Set method by default:

-(void) Setdelegate: (id<emchatmanagerdelegate>) delegate;

Through the comparison of two interfaces, it is easy to see: Single agent can only set one, and multiple agents may set more than one, it should be multiple agents can add multiple

What's the use of multiple agents

Some students may ask why they should use multiple proxies. With the notification can also achieve multiple objects at the same time monitoring AH. Yes, the way to listen to the notice can also achieve the purpose.

For example: the server through the socket came to a red dot message {"type": "Content": "A certain Message"},
Multiple pages now want to get the message to determine if they need to display the red dots.

Implementing with Notifications

Listening notification

[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (onreceivemsg:) name:@ "Knotificationname_ Onreceiveredpointmsg "Object:nil];

Implementing Notification methods

-(void) Onreceiveredpointmsg: (nsnotification *) Noti {
  nsdictionary *info = noti.userinfo;
  if ([info[@ "type"] integervalue] = =) {
    < #code #>
  }
}

Implement with Proxy

Registration Agent

[[Redpointmanager Sharedinstance] adddelegate:<# (id<redpointmanagerdelegate>) #>]

Implementing Proxy methods

-(void) Redpointmanagerdidreceive: (Redpointmodel *) Redpointmodel {
  if (Redpointmodel.type = =) {
    < #code # >
  }
}

Obviously, the use of proxy implementation is more intuitive and clear.

How to implement multiple proxies

The above mentioned Setdelegate: (id<emchatmanagerdelegate>) delegate method is not feasible, when the second set of the first time the agent is not held. This can only be done through AddDelegate: (id<emchatmanagerdelegate>) delegate.

Is it a bit out of place? Add a proxy object to an array (or a dictionary), which causes the object reference to count +1, causing the proxy object to not be freed. Yes, it is not feasible to add proxies directly to the array. But to hold multiple proxy objects, consider the release problem. Look at the usual write agent properties @property (nonatomic,weak) id<emchatmanagerdelegate>delegate; Suddenly thought of the use of weak modified not on the line.

Therefore, you can achieve the holding of multiple proxy objects by bridging.

This is good to do, the array holds the bridge object, the bridge object again has its own delegate.

Class Weakobjectbridge:nsobject {
  weak var weakobject:anyobject?
  Override Init () {
    super.init ()
  }
  init (object:anyobject?) {
    super.init ()
    Weakobject = Object
  }
}

Operations Agent

Func operatdelegate (CB: @escaping (_ Delegate:anyobject?)-> ()) {for
    Weakobjectbridge in Self.delegatebridges {
      DispatchQueue.main.async {
        cb (weakobjectbridge.weakobject)
      }
    }
  }

Specific call

Func action () {
    operatdelegate {(delegate) in the
      if let MyDelegate = delegate as? somemanagerdelegate {
        mydelegate.callback ()
        mydelegate.callback? ( msg: ["msg": "Hello world!"])}}
  

Demo Demo

Demo download

Click here to download demo.

Thank you for reading, I hope to help you, thank you for your support for this site!

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.