Ahmad Hassan
05/28/2023, 6:39 PMactual 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!alex009
05/29/2023, 7:14 AMurl
https://github.com/icerockdev/moko-resources/blob/d86f4f5e1ba1b8d6472b0925a8fc2650[…]rc/appleMain/kotlin/dev/icerock/moko/resources/AssetResource.kt that will be local url, and you can read it with dataWithContentsOfFile
for example - http://iphoneincubator.com/blog/data-management/how-to-read-a-file-from-your-application-bundleAhmad Hassan
05/30/2023, 4:02 AMval 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.:
val path = "test.db"
val a = NSData.dataWithContentsOfURL(MR.assets.balagh_ul_quran.url)
a?.writeToFile(path, true)
return NativeSqliteDriver(NoteDatabase.Schema, path)
alex009
05/30/2023, 7:37 AMAhmad Hassan
05/31/2023, 5:01 PM2023-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:
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:
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!alex009
06/04/2023, 2:14 PMcreateDriver
into try catch and print catched exceptionAhmad Hassan
06/07/2023, 12:13 PMalex009
06/07/2023, 2:11 PMAhmad Hassan
06/08/2023, 8:58 AM