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

registerDataLoadingObserver not working as intended #284

Open
mlilienberg opened this issue Jan 28, 2025 · 2 comments
Open

registerDataLoadingObserver not working as intended #284

mlilienberg opened this issue Jan 28, 2025 · 2 comments
Labels
bug Something isn't working

Comments

@mlilienberg
Copy link

mlilienberg commented Jan 28, 2025

Describe the bug
We are trying to add some observability to OTA updates. We want to trace success and error cases or even just add logging. But we did not find any way to register the observer without causing other side effects.

This is our code, that does not cause work because Crowdin SDK behaves unexpeced:

Crowdin.registerDataLoadingObserver(
   object : LoadingStateListener {
      override fun onDataChanged() {
         Log.d("onDataChanged")
      }

      override fun onFailure(throwable: Throwable) {
         Log.d("onFailure")
      }
   }
)
Crowdin.init(application, builder.build())

Adding the callback via registerDataLoadingObserver has no effect because Crowdin.init has not been called yet. When trying to add it after calling Crowdin.init it does not work either because the network calls already happened.
We also tried delaying the update by using withInitSyncDisabled() but manually calling Crowdin.forceUpdate does not do the checks to updateInterval which is done when using the default config without using withInitSyncDisabled option.

Expected behavior
I can call registerDataLoadingObserver even before Crowdin.init and the observer is registered.

@mlilienberg mlilienberg added the bug Something isn't working label Jan 28, 2025
@MykhailoNester
Copy link
Collaborator

Hi @mlilienberg, here is the proposed solution:

We can add LoadingStateListener as a third argument to the init function. This will allow you to subscribe to events within the application scope. With this approach, the observer will be registered during initialization, ensuring you receive updates, including those from initial API calls.

Crowdin.init(
    context = applicationContext,
    config = CrowdinConfig.Builder()
        .withDistributionHash(distributionHash)
        .build(),
    loadingStateListener = object : LoadingStateListener {
        override fun onDataChanged() {
            Log.d("TAG", "Crowdin: onDataChanged")
        }

        override fun onFailure(throwable: Throwable) {
            Log.d("TAG", "Crowdin: onFailure")
        }
    }
)

If you need to remove the observer later, you can store the listener as property and unregister it using Crowdin.unregisterDataLoadingObserver(listener).

Additionally, where you register the observer using the registerDataLoadingObserver method (e.g., in MainActivity or another component) may affect when you receive updates. If registered early, you might receive both the initial callback and all subsequent updates. Otherwise, you will only receive subsequent events.

Would this solution work for your use case?

@mlilienberg
Copy link
Author

Hi @MykhailoNester,

thanks for looking into my issue. I actually had the same solution in mind. It will work for us!
Waiting now for the next library update :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants