Hi!
I have a Socket singleton class that use socket-io to configure the socket connection, it contains a lot of method wrapped around the "on" handler, but i have a "little" problem. Every method is really simple, like this:
func onMessage(message: @escaping ((MessageModel?) -> Void)) -> UUID?{
let uuid = self.socket?.on("message", callback: { (data, ack) in
if !data.isEmpty{
let messageModel = MessageModel(JSON: data.first)
message(messageModel)
}
})
return uuid
}
but when i call this method two or more times, i register a lot of "message" on handlers, then for every event that i receive, in reality i have X calls on every handler, where X is the number of times that the onMessage() closure was used in the code.
My idea is to de-register the handler using the UUID when a view controller is not on focus, but i have some cases where is needed that a view controller on focus need to be updated with the new data coming from the on handler.
Then, do you have any solution to call for example the onMessage() handler, and get only one response on every onMessage() handler used in every VC?
I hope I explained myself.
Thanks!
-Paolo
I don't quite understand what you're trying to do. But, that feels like a pretty bad UI that you have to add dynamic event handlers like that. Better would be to use acks for sent events.
I try to explain in a simple way:
i have this method
self.socket?.on("message", callback: { (data, ack) in
//do something with data
})
and 4 view controllers allocated where this method is used (we can imagine that is implemented in every viewDidLoad).
When the server emit a "message", on the client side i receive, on every handler on "message", 4 responses. 4 responses X 4 handlers = 16 identical responses. The question is: there is a simple way to register every on handler only one time? The result that i want is to have only one response for every handler (a total of 4 event, one for every handler "message").
Can you not call one of the off methods before the on then?
It's the method I currently use, but I thought there was some cleaner method to fix the "problem" :)
Nope, that's the best way to make sure a specific handler only exists once.