Has anyone achieved datastore working on Compose D...
# compose-desktop
p
Has anyone achieved datastore working on Compose Desktop? In my case it works perfectly when executing the project under Android Studio, but if I generate the executable and execute it under Windows... it doesn't work. Doesn't store or recover variables. I see that a
app.preferences_pb.tmp
file is created on the application AppData folder, where it haves permissions and reading or writting of other non datastore files works... but in this case this file has 0 bytes size. Note it's a
.tmp
file, and when I execute it under
Android Studio
I receive a full working file without
.tmp
extension. What is happening? I'm adding how I use datastore in the thread.
This is my class:
Copy code
class DataStoreUseCase() {
    private val dataStore = createDataStore()

    suspend fun saveString(key: DataStoreKey, value: String) {
        dataStore.edit { preferences ->
            preferences[stringPreferencesKey(key.name)] = value
        }
    }

    suspend fun getString(key: DataStoreKey): String? {
        val preferences = dataStore.data.first()
        return preferences[stringPreferencesKey(key.name)]
    }

    private fun createDataStore(): DataStore<Preferences> =
        PreferenceDataStoreFactory.createWithPath(
            produceFile = { "app.preferences_pb".toPath() }
        )
}
c
You should use something like this to create an absolut path https://github.com/Syer10/Kotlin-Multiplatform-AppDirs
p
finally I found how to solve it, was related to some kind of bug with datastore
is necessary to add this:
Copy code
nativeDistributions{
            targetFormats(TargetFormat.Msi)
            modules("jdk.unsupported")
            modules("jdk.unsupported.desktop")            
}
those two last "modules" lines make it work, I hope they will solve this, sounds very bad to use these lines with the word "unsupported"
well, that post is from 2023, so I wonder why anyone here has posted about this, I doubt I'm the only one using datastore in compose desktop
I'm using datastore 1.1.2, which is the last version, and it still happens
c
As it says in the ticket, it’s a packaging issue and not datastore related.
That’s why it works for you when running from Gradle.
p
oh, I suposse it must be solved then by... jetbrains? gradle?
well, seems that it will took time
at least we can make it work, in the hope that these lines won't break anything
c
No, only you can solve it for your project. It’s not a general problem. Not everybody has the same setup and libraries and modules used.
p
I suppose this happens with everyone using datastore and compose desktop?
or you mean that one of my other libraries is causing it?
my project just have commonMain and desktopMain, it's only for desktop
a
@Pablo Out of curiosity... you pasted 5 lines and said:
those two lines make it work
Can you clarify which lines you added exactly? Did you mean these?
Copy code
modules("jdk.unsupported")
modules("jdk.unsupported.desktop")
p
yes, exactly these two
m
Under the hood jpackage is using the Java module system but it is not always possible to determine automatically which of the standard Java modules are needed for your application. E.g., when code is accessed only via reflection. Therefore you sometimes have to tell the build system which modules are needed. E.g., this is a line from my own build script:
Copy code
modules("java.instrument", "jdk.unsupported", "java.sql")
As you can see I also have to use “jdk.unsupported”. There is a Gradle task “`suggestModules`” to help you with this but it also does not always work. So, it often is a try and error game. See: https://www.jetbrains.com/help/kotlin-multiplatform-dev/compose-native-distribution.html#including-jdk-modules
☝🏼 1
110 Views