How do you block a return until a take() timer expires?

30 views
Skip to first unread message

DS0322

unread,
Jul 25, 2018, 11:43:38 PM7/25/18
to RxJava
I know there has to be a simple way to do what I am trying to do. In short, I want to call a function, and have that function listen for values for a specified period of time, then return the items it found during that time. Here is the code I have so far:


fun returnAllDevicesStartingWith(devicePrefix: String): List<String> {

Logger.d(TAG, "About to scan for devices. Will return those starting with $devicePrefix")

val mutableList = mutableListOf<String>()

val result = scanForDevices()
.take(4, TimeUnit.SECONDS, timeoutScheduler)

.subscribe{scanResult ->
val name = scanResult.bleDevice.name

Logger.d(TAG, "Potential device match named $name")

if(name != null) {
if(name.startsWith(prefix = devicePrefix)) {
Logger.d(TAG, "Match found $name")
mutableList.plus(name)
}
}
}



return mutableList
}

So I call the above function something like:

val deviceList = returnAllDevicesStartingWith("ABC")


The scanForDevices function returns an Observable<ScanResult> as it finds each device. That function works fine.

How to I keep from returning the mutableList until the take(4, TimeUnit.SECONDS, timeoutScheduler) expires?

Dávid Karnok

unread,
Jul 26, 2018, 3:52:31 AM7/26/18
to darr...@gmail.com, RxJava
Why not return Single<List<T>> and keep composing onto that, thus there is no need for blocking?
--
Best regards,
David Karnok

DS0322

unread,
Jul 26, 2018, 11:15:25 AM7/26/18
to RxJava
Appreciate the idea. The calling routine needs to have the complete list of devices before it can do its work, so a one time transmit seems to be in order unless I'm missing your point.
Reply all
Reply to author
Forward
0 new messages