Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Darrin
    @darrinps
    So I am confused why it works for the test which emits values just like the real thing does, and not for the real thing. Again, if I don't do a take, and subscribe instead, then the scanAllDevices code does scan correctly. I just can't allow it to scan forever and I need one complete list. Not something that gets sent back an element at a time.
    Darrin
    @darrinps
    For example, doing this I do get the BLE devices scanned successfully. I just cannot use them coming back async. I need a complete list or as much of a list as can be found in a specified time period. Note that the scanner takes less than a second to get all of the values when it is working, but I gave it up to 15 just to take out that variable. Here is the code that does at least scan.
        private fun scanAllDevices(context: Context) {
    
        val rxBleClient = RxBleClient.create(context);
    
        rxBleClient.scanBleDevices(
                 ScanSettings.Builder ()
                        .setScanMode(ScanSettings.SCAN_MODE_LOW_POWER) // change if needed
                        .setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES) // change if needed
                        .build(),
                ScanFilter.Builder()
                        .build()
        )
                .observeOn(Schedulers.io())
                .subscribe{scanResult->
                    val rxBleDevice = rxBleClient.getBleDevice(scanResult.getBleDevice().getMacAddress());
                    Logger.d(TAG, "The device has name ${rxBleDevice.name}")
                }
    }
    Ignacio Baca Moreno-Torres
    @ibaca
    Not sure, but add various ".doOnNext" to log element to see which operator is not doing what you expect
    erge123
    @erge123
    @songyunlu
    Darrin
    @darrinps
    Thanks for the suggestion. I did try that, and didn't see any .doOnNext methods ever get hit anywhere. Anyway, I figured out how to do what I want, but I still am not certain why what I had did not work. What I came up with looks like this:
        button_scan3.setOnClickListener {
            Log.d(TAG, "\n\nFiring up Scanner3")
    
            val scanner = SuiteScanner.create(this.applicationContext)
            val set = mutableSetOf<String>()
    
            scanner.scanForAllBLEDevices3("ABC")
                    .doOnComplete { Log.d(TAG, "Completed...Scanner3")
                        for(name in set ) {
                            Log.d(TAG, "BLE Device: $name")
                        }
                    }
                    .subscribe { scanResult ->
                        val rxBleDevice = scanResult.getBleDevice()
                        Log.d(TAG, "The device has name ${rxBleDevice.name}")
                        val name = rxBleDevice.name
                        if (name != null) {
                            set.add(name)
                        }
                    }
    
        }                                                                                                                                                                                                                                                                                                                                                                                                                                           
    
        fun scanForAllBLEDevices3(prefix: String): Observable<ScanResult> {
    
        Log.w(TAG, "We are in scanForAllBLEDevices3.")
    
        return rxBleScanner()
                .take(2, TimeUnit.SECONDS, timeoutScheduler)
                .observeOn(Schedulers.io())
                .filter { device -> inspectScanResult(device.bleDevice.name, prefix) }
    }
    
    
    private fun inspectScanResult(foundDevice: String?, prefix: String): Boolean {
        Log.d(TAG, " Inspecting: $foundDevice")
    
        if(foundDevice == null) {
            return false
        }
    
        return (foundDevice.startsWith(prefix))
    }
    So I'll just do what needs to be done in the onComplete()
    Yannick Lecaillez
    @ylecaillez
    Hi !
    Is there an operator which behaves like take(1) but does not cancel upstream once the first item has been emitted ?
    I need that because i send multiple request concurrently (flatMap) but i only care about the first response coming back. Still i want the subsequent request to be performed. Any idea ?
    Sadly, Flowable.amb() is also cancelling remaining requests
    Yannick Lecaillez
    @ylecaillez
    Hmm, switchMap() might be what i'm looking for. Sorry for the noise.
    David Karnok
    @akarnokd
    Beware that switchMap cancels the active inner source if a new upstream item arrives and gets mapped in.
    Also not completing and not cancelling may leak resources.
    Yannick Lecaillez
    @ylecaillez
    Yes this does not work for my use case i ended with something like that:
    Using compose:
    .compose(f -> {
                               final AtomicBoolean isFirst = new AtomicBoolean();
                               return f.filter(r -> isFirst.compareAndSet(false, true));
                           })
    David Stocking
    @dmstocking
    I think you could create your own operator doOnFirst() easy if your in kotlin
    fun Observable<T>.doOnFirst(action: (T) -> Unit) {
        var first = true;
        return this.doOnNext { item ->
            if (first) {
                first = false
                action(item)
            }
        }
    }
    骨来(PeterLi)
    @pli2014
            Flowable
                    .create(emitter -> {
                        int i = 0;
                        while (i++ < 10) {
                            emitter.onNext(RandomUtils.nextInt() + ":" + i);
                            Thread.sleep(100);
                        }
                        emitter.onComplete();
                    }, BackpressureStrategy.BUFFER)
                    //.subscribeOn(Schedulers.computation())
                    .observeOn(Schedulers.io())
                    .buffer(1, TimeUnit.SECONDS, Schedulers.trampoline())
                    .subscribe(list -> {
                        System.out.println("BlockedBufferRxTest.main:" + list.toString());
                    });
    
             Thread.sleep(1000000);
    Schedulers.trampoline() blocked the current main thread that cannot execute subscribe method. There are some overwrite buffer methods which called to Schedulers.computation() as input arguments.
    Yannick Lecaillez
    @ylecaillez
    @dmstocking How is this different from what i've done ? I'm in Java btw.
    Isn't there a backpressure problem here ?
    Volkan Yazıcı
    @vy

    Using RxJava 1.1.9, I have a problem as follows:

    import rx.Observable;
    
    public enum ZipWithTest {;
    
        public static void main(String[] args) {
            Observable
                    .just(1, 2, 3)
                    .doOnNext(next -> System.out.format("next: %d%n", next))
                    .zipWith(
                            Observable.just(10, 100, 1000),
                            (next, multiplier) -> next * multiplier)
                    .doOnNext(multiplied -> System.out.format("multiplied: %d%n", multiplied))
                    .toBlocking()
                    .last();
        }
    
        /*
        next: 1
        next: 2
        next: 3
        multiplied: 10
        multiplied: 200
        multiplied: 3000
         */
    
    }

    I was expecting to get the following:

        next: 1
        multiplied: 10
        next: 2
        multiplied: 200
        next: 3
        multiplied: 3000

    What am I missing?

    骨来(PeterLi)
    @pli2014
    @vy doOnNext only go through every element in just(1, 2, 3) source.
    so that it invokes an action when it calls {@code onNext}
    Volkan Yazıcı
    @vy
    @pli2014 I got it. But why is it getting every element from just(1,2,3) and then zipping with the rest? Shouldn't they go in parallel? That is, one from source, one from multiplier, one from source, one from multiplier, and so on.
    骨来(PeterLi)
    @pli2014
    zipwith behavior is just that
    DoOnNext is called after ever onNext op from every element of source
    Ignacio Baca Moreno-Torres
    @ibaca
    @vy you should really upgrade to rxjava 2, but as rx1 observable has backpressure this might work as you said, but the problem is that zipWith has a buffer, so the 3 next call are juts filling the zip buffer, not sure if in rx1 exists the zipWith operator overload to indicate the buffer size, but you can do it in rx2, in that case you need to use Flowable instead of Observable to support backpressure
    Ignacio Baca Moreno-Torres
    @ibaca
    @ylecaillez you can use publish and merge the take(1) with a ignoreElements, so the source observable keep subscribe untils it completes (you can exgract the compose lamda to reuse the operator)
    Observable.just(1, 2, 3).doOnNext(n -> out.println("emmit " + n))
            .compose(o -> o.publish(p -> p.take(1)))
            .subscribe(n -> out.println("final " + n));
    // emmit 1 ⏎ final 1
    Observable.just(1, 2, 3).doOnNext(n -> out.println("emmit " + n))
            .compose(o -> o.publish(p -> p.take(1).mergeWith(p.ignoreElements())))
            .subscribe(n -> out.println("final " + n));
    // emmit 1 ⏎ final 1 ⏎ emmit 2 ⏎ emmit 3
    Mark Raynsford
    @io7m
    'ello. i'm wondering if there's a recommended way to achieve the following on 1.x: i have a hot observable (a publishsubject) but i'd like to buffer the last produced item so that new subscribers always see the most recently published event when they subscribe (and then see any new events when i publish them after that)
    i'm currently stuck on 1.x due to an incompatible third-party dependency in the project i'm working on, unfortunately
    sorry, that should read "buffer the most recently produced value"
    Mark Raynsford
    @io7m
    i assume that i want some combination of replay and defer, but i can't quite work it out
    Mark Raynsford
    @io7m
    ... turns out a BehaviorSubject does exactly this
    sorry for the noise!
    Moritz Bust
    @Busti
    How does ReactiveX compile to so many different languages? Is a transpiler used to achieve this, or is every code-base managed separately?
    Igor Bozin
    @igorbzin_gitlab
    I am trying to get a grasp of RxJava. How would i chain following situation in calls with RxJava: i retrieve a Single<User> from my database, loading a user by id for example, this should happen async. After loading the user object, i put some of his specific data in an arraylist. After that finished I want to listen on this array for changes, so that when an element gets added or removed I write to the database async to update the user. I have following code so far, also implemented the piece you have suggested
    David Karnok
    @akarnokd
    @Busti RxJava compiles to a JVM bytecode and any JVM language should be able to interact with it. The language adapters such as RxScala, RxKotlin, etc. are usually thin wrappers around RxJava so technically you still run Java bytecode beneath.
    @igorbzin_gitlab You mean something like this? https://stackoverflow.com/q/28816691/61158
    Moritz Bust
    @Busti
    @akarnokd Thank you for your answer. I was just wondering if non-related languages in the ReactiveX project somehow shared a common codebase since there are so many different languages supported. Something like haxe comes to mind, but I do realize that that would be a bad idea.
    Igor Bozin
    @igorbzin_gitlab

    @akarnokd yes, something exactly like this, thank you. I am still not sure how to chain those asynchronous calls though, since i operate on the database.

            Observable.just(uriObservableList)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<RxObservableList<Uri>>() {
                        @Override
                        public void onSubscribe(Disposable d) {
                            Log.d(TAG, "ONSUBSCRIBE ");
                        }
    
                        @Override
                        public void onNext(RxObservableList<Uri> uris) {
                            String s = makePathString(uris.getList());
                            AppExecutor.getInstance().diskIO().execute(() -> viewModel.updatePicturePaths(s)); // <-- THis async call should be performed on every onNext 
                            mAdapter.updatePictures(uris.getList());
                            Log.d(TAG, "ONNEXT");
                        }
    
                        @Override
                        public void onError(Throwable e) {
                            Log.d(TAG, "ONERROR  " + e);
                        }
    
                        @Override
                        public void onComplete() {
                            Log.d(TAG, "ONCOMPLETE  ");
                        }
                    });

    How do I construct my Subscription so that before every onNext call I can perform a write operation on the database? I have tried doOnNext which is supposed to be executed before onNext, but it is always called from the main thread

    David Stocking
    @dmstocking
    @ylecaillez woopsy I didn't notice your message. Yours appears to be really similar. The only difference appears to be that I take a action to run not just filter the first value. Yours kind of looks like it implements first().
    Viktor Vostrikov
    @wellbranding
    I have previously used MediatorLiveData to combine results from FCM, Room and Networking in my Repository. It worked well. However, now I want more complexity and some additional bonuses from RxJava. This is why I have decided to use RxJava this time. I can combine Room and Networking with Observable.concatArrayEager(my _observables) also I have used merge, but I want to get only latest changed value, not all observables, like it is done with MediatorLiveData However, I don't now how to do that after FCM pushes value, and how should I notify my main observable after new changes occur? No examples on this issue whatsoever. It is crucial part. I receive FCM in my BroadCastReceiver and then notified my repository's livedata, which notified my MediatorLiveData... How to do that with RxJava? Would really welcome any advice, because it is really important issue.
    David Karnok
    @akarnokd
    @igorbzin_gitlab Put the doOnNext between the subscribeOn and observeOn.
    James Fleming
    @Flemingjp
    Hello, I'm trying to get my head around trying to open a stream, buffering the items it emits until an async task has completed (this case a web request) and then process all the buffered items and the items following (if any).
    Ignacio Baca Moreno-Torres
    @ibaca
    You can use a combine latest for example, but the the buffer will grow until the request completes, alternatively you can make the request and we it completes flatMap it with the stream, so you don't need the buffer
    James Fleming
    @Flemingjp
    What would be the best way to start the async task once the stream has started? I'm cautious of interrupting the flow of the stream
    Igor Bozin
    @igorbzin_gitlab

    @akarnokd already tried it.

            viewModel.getPlace().map(this::getPicturePaths)
                    .subscribeOn(Schedulers.io())
                    .toObservable()
                    .concatMap(new Function<ArrayList<Uri>, Observable<RxObservableList<Uri>>>() {
                        @Override
                        public Observable<RxObservableList<Uri>> apply(ArrayList<Uri> uriArrayList) throws Exception {
                            mAdapter = new PictureRvAdapter(MarkerActivity.this, uriArrayList, MarkerActivity.this);
                            LinearLayoutManager layoutManager = new LinearLayoutManager(MarkerActivity.this, LinearLayoutManager.VERTICAL, false);
                            DividerItemDecoration decoration = new DividerItemDecoration(getApplicationContext(), VERTICAL);
                            mPicturesRV.addItemDecoration(decoration);
                            mPicturesRV.setLayoutManager(layoutManager);
                            mPicturesRV.setAdapter(mAdapter);
                            beginTransition(constraintSetEnd, 850);
                            Log.d(TAG, "Adapter set, transition started");
    
                            uriObservableList.setList(uriArrayList);
                            return uriObservableList.getObservable();
                            //return Observable.just(uriObservableList);
                        }
                    })
                    .doOnNext(new Consumer<RxObservableList<Uri>>() {
                        @Override
                        public void accept(RxObservableList<Uri> uris) throws Exception {
                            String pathString = makePathString(uris.getList());
                            viewModel.updatePicturePaths(pathString);
                            Log.d(TAG, "Updated pictures in database");
                        }
                    })
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<RxObservableList<Uri>>() {
                        @Override
                        public void onSubscribe(Disposable d) {
                            Log.d(TAG, "Onsubscribe called");
                        }
    
                        @Override
                        public void onNext(RxObservableList<Uri> uris) {
                            mAdapter.updatePictures(uris.getList());
                            Log.d(TAG, "Adapter pictures updated");
                        }
    
                        @Override
                        public void onError(Throwable e) {
                            Log.d(TAG, "OnError : " + e);
                        }
    
                        @Override
                        public void onComplete() {
                            Log.d(TAG, "OnComplete called");
                        }
                    });

    still gives me this error: MarkerActivity: OnError : java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time.

    Ignacio Baca Moreno-Torres
    @ibaca
    @Flemingjp
    Observable<String> stream = Observable.fromArray("a", "b", "c");
    Observable<String> request = Single.just("1").toObservable();
    stream.withLatestFrom(request, (streamItem, requestItem) -> streamItem + requestItem)
            .forEach(System.out::println);
    // output: a1b1c1
    Nicholas Bransby-Williams
    @nbransby
    What combination of operators do I need to achieve the following
    Nicholas Bransby-Williams
    @nbransby
    Combine two streams into one which only emits an item( or completes ) as soon as both streams have emitted the same number of items (at least 1)
    Been studying the operators but can’t see an obvious way to do it
    Nicholas Bransby-Williams
    @nbransby
    think ive got it: first.buffer(second)
    .map { it.size - 1 }
    .scan(0) { acc, current -> acc + current }
    .filter { it == 0 }