Closed DigitalSolomon closed 2 years ago
Not sure I fully understand the question.
The callback receive
expects doesn't return anything, it's a Void
closure.
If you're talking about async/await, you can have your callback perform asynchronous code by wrapping it in a Task
, like shown here.
Thanks for the quick response :) To clarify what I mean, let's take the start()
method in CLIReceiver.swift
for example.
func start() {
transceiver.receive(FooCommand, using: response(handleFoo))
transceiver.receive(BarCommand, using: response(handleBar))
transceiver.resume()
}
Right now the handleFoo
method which I pass to response
is a synchronous method which returns .message("sample-message")
. I want to be able to make an asynchronous call upon receiving the command to populate the String
response. Do you know how to achieve this? I'll check out the link you provided.
Also, if a project doesn't use async/await
yet, are there other elegant options to elegantly return .message
asynchronously? Thanks
Hmmmm, I'm a bit lost because the code you're sharing is not in the context of this repo. I assume that's one of your projects that's using the library, right?
There should be some way to plug an async method in there, or you can write your own little wrapper. That's out of the scope of Github issues for the library though, so I'm closing this issue.
John's concurrency discover page has lots of guides that could help you get started: https://www.swiftbysundell.com/discover/concurrency/
If that doesn't help, you may also ask your question in Stackoverflow.
I don't know of any other similar wrappers for MultipeerConnectivity. I have async/await support planned, but I won't have time to implement that any time soon.
Ah, it is. Sorry, I didn't make the connection based on your initial comment 😅
No worries. I modified the code so that the handlers don't return a CLIResponse
immediately, but instead return Void
and receive an additional callback parameter which calls self?.transceiver.broadcast(CLIResponse.message(result))
after asynchronous activity has occurred. It's all working, just was curious if you had anything more elegant. Good work on your project overall! MultipeerKit is a nice wrapper around MultipeerConnectivity!
Glad to know you found a solution :)
Is there a way we can return a
CLIResponse
asynchronously, or do the message handling methods passed totransceiver.receive
have to be synchronous calls which return aString
?