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

Improve scopes functionality. #466

Open
Monabr opened this issue Feb 17, 2025 · 2 comments
Open

Improve scopes functionality. #466

Monabr opened this issue Feb 17, 2025 · 2 comments
Labels
question Further information is requested

Comments

@Monabr
Copy link

Monabr commented Feb 17, 2025

Hi. In my Crashlytics I see this errors:

          Fatal Exception: java.lang.IllegalStateException: There are multiple DataStores active for the same file: /data/user/0/....preferences_pb. You should either maintain your DataStore as a singleton or confirm that there is no two DataStore's active on the same file (by confirming that the scope is cancelled).
       at androidx.datastore.core.okio.OkioStorage.createConnection(OkioStorage.java:65)
       at androidx.datastore.core.DataStoreImpl$storageConnectionDelegate$1.invoke(DataStoreImpl.java:189)
       at androidx.datastore.core.DataStoreImpl$storageConnectionDelegate$1.invoke(DataStoreImpl.java:188)
       at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:83)
       at androidx.datastore.core.DataStoreImpl.getStorageConnection$datastore_core_release(DataStoreImpl.java:191)
       at androidx.datastore.core.DataStoreImpl$coordinator$2.invoke(DataStoreImpl.kt:192)
       at androidx.datastore.core.DataStoreImpl$coordinator$2.invoke(DataStoreImpl.kt:192)
       at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:83)

But I have this code

@Scope
@Target(CLASS, FUNCTION, PROPERTY_GETTER)
annotation class ApplicationSingleton

@ApplicationSingleton
interface SharedComponent : DBModule, NetworkModule, ViewModelsFactoriesModule


@ApplicationSingleton
@Component
abstract class AndroidAppComponent(
    private val applicationContext: Context
) : SharedComponent {
   @ApplicationSingleton
    @Provides
    fun provideMyStore(context: Context): MyStore{
        return MyStore.createDataStore { context.filesDir.resolve("datastore").resolve(MyStore.PREFERENCES_NAME_KMP).absolutePath }
    }
}

And in generated code I see

override val myStore: MyStore
    get() = _scoped.get("com.example.store.MyStore") {
      provideMyStore(
        context = context
      )
    }

But still get error for some users. What could it be? How it can be improved?

@evant
Copy link
Owner

evant commented Feb 18, 2025

Are you creating multiple instances of AndroidAppComponent?

@evant evant added the question Further information is requested label Feb 18, 2025
@Monabr
Copy link
Author

Monabr commented Feb 18, 2025

@evant I create AndroidAppComponent in 3 places:

  1. Application class and then use it in Activity class like that
interface ApplicationComponentProvider {
    val component: AndroidAppComponent
}

val Context.applicationComponent get() = (applicationContext as ApplicationComponentProvider).component
  1. In BroadcastReceiver - BootCompletedReceiver
  2. In BroadcastReceiver - AppUpdatedReceiver

But instances is single I believe.

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

No branches or pull requests

2 participants