Hi I want to pre-populate the database in my KMM p...
# moko
a
Hi I want to pre-populate the database in my KMM project. I’m using Moko-resources for a single source across both platforms. I have this implementation for android side.
Copy code
actual fun Scope.createDriver(): SqlDriver {

    val context = androidContext()
    val fileName = "balagh_ul_quran.db"
    val database: File = context.getDatabasePath(fileName)

    if (!database.exists()) {
        val inputStream = MR.assets.balagh_ul_quran.getInputStream(context)
        val outputStream = FileOutputStream(database.absolutePath)

        inputStream.use { input ->
            outputStream.use {
                input.copyTo(it)
            }
        }
    }

    return AndroidSqliteDriver(NoteDatabase.Schema, context, fileName)
}
May I know the what are the steps to for iOS side..? thanks!
a
thank you for the response. Will it be done something like this?
Copy code
val path = "test.db"
val fileManager = NSFileManager.defaultManager

if (!fileManager.fileExistsAtPath(path)) {
    val bundlePath = MR.assets.balagh_ul_quran.url.path.toString()
    fileManager.copyItemAtPath(bundlePath, toPath = path, error = null)

}

return NativeSqliteDriver(NoteDatabase.Schema, path)
I tried this as well but not worked.:
Copy code
val path = "test.db"
    val a = NSData.dataWithContentsOfURL(MR.assets.balagh_ul_quran.url)
    a?.writeToFile(path, true)
    return NativeSqliteDriver(NoteDatabase.Schema, path)
a
what exactly not worked? try to add error handling. in your code error from copyItemAtPath not catched at all, so you not see have you errors or not
a
I caught the copyItemAtPath, it’s returning false using the above code. I receive this in the Logs:
Copy code
2023-05-30 23:58:45.320203+0500 iOSApp[21924:532700] open on test.db: Read-only file system
myLog: false
So, then tried this implementation now:
Copy code
actual fun Scope.createDriver(): SqlDriver {

    val databaseName = "test.db"
    val fileManager = NSFileManager.defaultManager

    val documentsDirectory = fileManager.URLsForDirectory(NSDocumentDirectory, inDomains = NSUserDomainMask).first() as NSURL
    val destinationURL = documentsDirectory.URLByAppendingPathComponent(databaseName)

    val bundlePath = MR.assets.balagh_ul_quran.url

    if (!fileManager.fileExistsAtPath(destinationURL?.path.toString())) {
        val response = try {
            if (destinationURL != null) {
                fileManager.copyItemAtURL(bundlePath, toURL = destinationURL, error = null)
            } else {

            }
        } catch (e: Exception) {
            showLog("catch:${e.message}")
        }

        println("myLog: $response")
    }

    return NativeSqliteDriver(NoteDatabase.Schema, destinationURL?.path.toString())
}
Now the app is crashing with the following logs:
Copy code
Uncaught Kotlin exception: org.koin.core.error.InstanceCreationException: Could not create instance for '[Singleton:'app.cash.sqldelight.db.SqlDriver']'
    at 0   iOSApp                              0x10401cc95        kfun:kotlin.Exception#<init>(kotlin.String?;kotlin.Throwable?){} + 133 
    at 1   iOSApp                              0x104982af5        kfun:org.koin.core.error.InstanceCreationException#<init>(kotlin.String;kotlin.Exception){} + 133 
    at 2   iOSApp                              0x10498472b        kfun:org.koin.core.instance.InstanceFactory#create(org.koin.core.instance.InstanceContext){}1:0 + 2715 
    at 3   iOSApp                              0x104986f52        kfun:org.koin.core.instance.SingleInstanceFactory#create(org.koin.core.instance.InstanceContext){}1:0 + 226 
    at 4   iOSApp                              0x1049872a8        kfun:org.koin.core.instance.SingleInstanceFactory.get$lambda$0#internal + 232 
    at 5   iOSApp                              0x104987334        kfun:org.koin.core.instance.SingleInstanceFactory.$get$lambda$0$FUNCTION_REFERENCE$0.invoke#internal + 84 
    at 6   iOSApp                              0x10498742d        kfun:org.koin.core.instance.SingleInstanceFactory.$get$lambda$0$FUNCTION_REFERENCE$0.$<bridge-UNN>invoke(){}#internal + 77 
    at 7   iOSApp                              0x1049a470f        kfun:org.koin.mp.KoinPlatformTools#synchronized(org.koin.mp.Lockable;kotlin.Function0<0:0>){0§<kotlin.Any?>}0:0 + 703 
    at 8   iOSApp                              0x10498714a        kfun:org.koin.core.instance.SingleInstanceFactory#get(org.koin.core.instance.InstanceContext){}1:0 + 362 
    at 9   iOSApp                              0x104990b81        kfun:org.koin.core.registry.InstanceRegistry#resolveInstance(org.koin.core.qualifier.Qualifier?;kotlin.reflect.KClass<*>;org.koin.core.qualifier.Qualifier;org.koin.core.instance.InstanceContext){0§<kotlin.Any?>}0:0? + 529 
    at 10  iOSApp                              0x10499a18c        kfun:org.koin.core.scope.Scope.resolveValue#internal + 1612 
    at 11  iOSApp                              0x10499996f        kfun:org.koin.core.scope.Scope.resolveInstance#internal + 2255 
    at 12  iOSApp                              0x104999037        kfun:org.koin.core.scope.Scope#get(kotlin.reflect.KClass<*>;org.koin.core.qualifier.Qualifier?;kotlin.Function0<org.koin.core.parameter.ParametersHolder>?){0§<kotlin.Any?>}0:0 + 2487 
    at 13  iOSApp                              0x104a43f53        kfun:com.ahmadhassan.mynote.test.HomeVModel#<init>(){} + 995 
    at 14  iOSApp                              0x104a40824        kfun:com.ahmadhassan.mynote.test#Home(androidx.compose.runtime.Composer?;kotlin.Int){} + 996 
    at 15  iOSApp                              0x104a4fefc        kfun:com.ahmadhassan.mynote.ComposableSingletons$Main_iosKt.<init>$lambda$0#internal + 684 
    at 16  iOSApp                              0x104a50051        kfun:com.ahmadhassan.mynote.ComposableSingletons$Main_iosKt.$<init>$lambda$0$FUNCTION_REFERENCE$0.invoke#internal + 97 
    at 17  iOSApp                              0x104a5010d        kfun:com.ahmadhassan.mynote.ComposableSingletons$Main_iosKt.$<init>$lambda$0$FUNCTION_REFERENCE$0.$<bridge-UNNNB>invoke(androidx.compose.runtime.Composer;kotlin.Int){}#internal + 173 
    at 18  iOSApp                              0x104357222        kfun:androidx.compose.runtime.internal.ComposableLambdaImpl#invoke(androidx.compose.runtime.Composer;kotlin.Int){}kotlin.Any? + 1106 
    at 19  iOSApp                              0x10436d371        kfun:androidx.compose.runtime.internal.ComposableLambdaImpl#$<bridge-NNNNB>invoke(androidx.compose.runtime.Composer;kotlin.Int){}kotlin.Any?(androidx.compose.runtime.Composer;kotlin.Any?){}kotlin.Any? + 177 
    at 20  iOSApp                              0x104978a07        kfun:moe.tlaster.precompose.PreComposeApplication$lambda$1$lambda$0#internal + 823 
    at 21  iOSApp                              0x10497a757        kfun:moe.tlaster.precompose.$PreComposeApplication$lambda$1$lambda$0$FUNCTION_REFERENCE$9.invoke#internal + 119 
    at 22  iOSApp                              0x10497a88d        kfun:moe.tlaster.precompose.$PreComposeApplication$lambda$1$lambda$0$FUNCTION_REFERENCE$9.$<bridge-UNNNB>invoke(androidx.compose.runtime.Composer;kotlin.Int){}#internal + 173 
    at 23  iOSApp                              0x104357222        kfun:androidx.compose.runtime.internal.ComposableLambdaImpl#invoke(androidx.compose.runtime.Composer;kotlin.Int){}kotlin.Any? + 1106 
    at 24  iOSApp                              0x10436d371        kfun:androidx.compose.runtime.internal.ComposableLambdaImpl#$<bridge-NNNNB>invoke(androidx.compose.runtime.Composer;kotlin.Int){}kotlin.Any?(androidx.compose.runtime.Composer;kotlin.Any?){}kotlin.Any? + 177
Don’t know, what is the issue. Can you suggest something? Thanks!
a
error throwed by koin. to see original error wrap
createDriver
into try catch and print catched exception
a
Catched this exception: myLog-Scope: File /Users/ahmadhassan/Library/Developer/CoreSimulator/Devices/3D5EDB35-1133-4E3F-A19D-2A193F102480/data/Containers/Data/Application/4B0C994E-1724-42AE-B077-7DE5117E9860/Documents/test.db contains a path separator Don’t know, where is the issue..
a
a
I already checked this link, seems irrelevant for my driver in Kotlin as Swift is being used here and I’m an Android developer. Do you have any idea? Thanks!
108 Views