Reputation: 19
I did a lot of research and haven't got any answer for this one.
this is my code:
fileprivate var dispose: ScopedDisposable<AnyDisposable>?
func connectToServer() {
...
let user = ...
let channelName = getChannelName(user)
self.connector.connectTo(channelName)
}
func getChannelName(_ user: String) -> String {
var channelName: String = ""
self.dispose = ScopedDisposable(
self.service.requestChannelNameFromServerForUser(user)
.startWithValues({ results in
channelName = results[0].channelName
})
)
return channelName // this will return "" because callback hasn't returned yet
}
I'm trying to find a way to make the function "getChannelName" to wait until "channelName" is retrieved from the reactive request (from another server).
Thanks for your help.
Upvotes: 0
Views: 313
Reputation: 5941
The first()
function will essentially do this for you:
func getChannelName(_ user: String) -> String {
let result = self.service.requestChannelNameFromServerForUser(user).first()
if let results = result?.value {
return results[0].channelName
}
else {
// The producer returned no values or an error. Return a default value or throw error or something.
return ""
}
}
But the more idiomatic way to do this is to make connectToServer
itself an asynchronous SignalProducer
which connects the getChannelName
and connectTo
producers into one one larger producer.
Upvotes: 1