0

On android I am using requery db and trying to upload my DB changes to server. To achieve the same, I am running the following logic

Scheduler sub2 = Schedulers.newThread();
Scheduler ob2 = Schedulers.newThread();
data.select(Broadcaster.class)
            .where(Broadcaster.IS_DIRTY.eq(true))
            .get()
            .observableResult()
            .subscribeOn(sub2)
            .observeOn(ob2)
            .flatMap(broadcasters->broadcasters.observable())
            .flatMap(broadcasters->Backend.getInstance()
                   .uploadBroadcaster(broadcasters)
                    .onExceptionResumeNext(Observable.empty()))
            .flatMapSingle(broadcaster -> markUploaded(broadcaster))
            .doOnError(t->Log.e(TAG,"Error uploading ",t))
            .subscribe();

But for every change the uploadBroadcaster is called multiple times (4-10 times) with the same data. What am I doing wrong here.

Jagan Veeraraghavan
  • 363
  • 2
  • 4
  • 14
  • `flatMap()` will be called for each `onNext()`, so it's depends on upstream Observable emissions, it's probably emit more than single item, thus you're calling `uploadBroadcaster` multiple times. what does the `.observableResult()` and the `broadcasters.observable()` do? – yosriz Jun 22 '17 at 05:28
  • Updated the question. I use requery db on android. .observableResult emits the changes to db as a reactiveResult and .observable converts the reactiveResult to an observable – Jagan Veeraraghavan Jun 22 '17 at 05:42

1 Answers1

0

Mistake was mine. This block of code was called multiple times. so the subscription happened multiple times.

Jagan Veeraraghavan
  • 363
  • 2
  • 4
  • 14