1
\$\begingroup\$

Originally i have a function like this:

 private fun rxSaveEvents(
        events: List<LocationEvent>,
        locationUpdatesAboutToStop: Boolean): Boolean {

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

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

        }
        if (locationUpdatesAboutToStop) {
            stopLocationUpdates()

            Completable.fromCallable { cleanAllReadingsData() }.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?

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Ludisposed Nov 16 '17 at 12:29
  • \$\begingroup\$ @Ludisposed I've deleted the previous questions, which was too general. Now I've tried to be more specific about my problem. \$\endgroup\$ – Konrad Nov 16 '17 at 12:31
  • \$\begingroup\$ 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. \$\endgroup\$ – Ludisposed Nov 16 '17 at 12:32
  • \$\begingroup\$ This is written code, which i want to be improved, what's wrong with this? Even title suggest "change" \$\endgroup\$ – Konrad Nov 16 '17 at 12:33
  • \$\begingroup\$ Sure, we can review your code. But CR is not the place that will change it to your specific needs. \$\endgroup\$ – Ludisposed Nov 16 '17 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.