# Code which buffers objects and schedules saving them from buffers, after applied some actions to it

Originally i have a function like this:

 private fun rxSaveEvents(
events: List<LocationEvent>,

synchronized(mEventsMonitor) {
if (events.isNotEmpty()) {
val locationEvents = LocationEvents()
segregateLocationEvents(events, locationEvents)

Completable.fromCallable { updateLocationEventsToDb(locationEvents) }.subscribeOn(Schedulers.io()).subscribe()

}

Completable.fromCallable { removeExpiredData() }.subscribeOn(Schedulers.io()).subscribe()
}

locationEvents.clear()

return true
}
}


This function is triggered after some amount of time. It can be triggered in 2 different ways - scheduled with boolean as true or false. Here are the schedulers, which schedules this method invokation:

 val scheduleNormalSave = Completable.timer(locationEventsSaveThrottlingSeconds, TimeUnit.SECONDS).doOnSubscribe(Consumer { normalSaveScheduled.set(true) }).doOnComplete(Action { normalSaveScheduled.set(false) })
val scheduleStopAndSave = Completable.timer(locationStopUpdatesSeconds, TimeUnit.SECONDS).doOnSubscribe(Consumer { stopAndSaveScheduled.set(true) }).doOnComplete(Action { stopAndSaveScheduled.set(false) })


The booleans:

  private var normalSaveScheduled: AtomicBoolean = AtomicBoolean(false)
private var stopAndSaveScheduled: AtomicBoolean = AtomicBoolean(false)


If some event is triggered it will configure scheduler and and the start the schedule invokation: E.g

   if (!normalSaveScheduled.get()) {
configureNormalSaveCompletable()
}


And the configure normalSaveCompletable method:

  fun configureNormalSaveCompletable() {
scheduleNormalSave.subscribe(Action { rxSaveEvents(locationEvents, false) })
}


The locationEvents is just an arrayList, which buffers some events asynchronously.

private val locationEvents: ArrayList<LocationEvent> = ArrayList<LocationEvent>()


So when some event occurs(for example location update) it insert the LocationEvent to this ArrayList.

So my question is how to make my function, this rxSaveEvents more reactive? Do you need to see its other functions in this method implementation?

• I see you have similer questions closed or put on hold. What makes this question different from the others? I've flagged this as "Asking advice about code not yet written." Make sure you read the whole faq and help center to see what is on-topic here. – Ludisposed Nov 16 '17 at 12:29
• @Ludisposed I've deleted the previous questions, which was too general. Now I've tried to be more specific about my problem. – Konrad Nov 16 '17 at 12:31
• The problem is still the same, you ask: "Is there a way to change my method?" That would be off topic, because you haven't written it yet. – Ludisposed Nov 16 '17 at 12:32
• This is written code, which i want to be improved, what's wrong with this? Even title suggest "change" – Konrad Nov 16 '17 at 12:33
• Sure, we can review your code. But CR is not the place that will change it to your specific needs. – Ludisposed Nov 16 '17 at 12:36