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!