Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TrueTimeRx initial in Application.onCreate(), where I should add clear() for this disposable? #108

Open
thuypt opened this issue Jan 11, 2019 · 2 comments

Comments

@thuypt
Copy link

thuypt commented Jan 11, 2019

Regarding example, we got a Disposable at time TrueTimeRx initial in Application.onCreate(), it should be added in a CompositeDisposable like this:

private CompositeDisposable disposables;
disposables.add(trueTimeRxDisposable)

Problem is we don't have something like onDestroy in Application layer, so where and when I should call disposables.clear()?

@MarkVillacampa
Copy link

You don't need to dispose of it. If you call TrueTimeRx in your Application, it means its lifetime is tried to the lifetime of the application, so you never want to stop observing it. When either a successful time or an error comes through the subscription, the subscription will be closed, so you wont need to stop observing it early.

  For cleanliness, I've added the disposables.clear() call in the onTerminate method which would be the Application equivalent of onDestroy.

@CyxouD
Copy link

CyxouD commented Feb 21, 2024

For cleanliness, I've added the disposables.clear() call in the onTerminate method which would be the Application equivalent of onDestroy.

MarkVillacampa As far as I know, It is not equivalent, because onTerminate isn't always called.

thuypt This is how I handle it with retries:

override fun onCreate() {
        super.onCreate()

        with(ProcessLifecycleOwner.get()) {
            lifecycleScope.launch {
                lifecycle.repeatOnLifecycle(Lifecycle.State.CREATED) {
                    handleTrueTime()
                }
            }
        }
    }


    private fun CoroutineScope.handleTrueTime() = launch {
        var disposable: Disposable? = null
        try {
            while (isActive) {
                disposable?.dispose()
                disposable = TrueTimeRx.build()
                    .initializeNtp(NTP_APPLE)
                    .subscribeOn(Schedulers.io())
                    .subscribe(
                        {
                            // Note that it not always invokes
                            Log.d(
                                TAG,
                                "TrueTime was initialized"
                            )
                        }
                    ) { throwable: Throwable ->
                        // Note that it not always invokes
                        Sentry.captureException(throwable)
                        Log.d(
                            TAG,
                            "TrueTime wasn't initialized: ${throwable}"
                        )
                    }
                delay(DELAY_BETWEEN_CLOCK_RE_SYNC)
            }
        } finally {
            disposable?.dispose()
        }

    }


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants