Ahmad Hassan
07/28/2023, 8:29 PMAhmad Hassan
07/28/2023, 8:57 PMactual fun Scope.createDriver(): SqlDriver {
val context = androidContext()
val fileName = "dbFileName"
val database: File = context.getDatabasePath(fileName)
if (!database.exists()) {
val inputStream = context.assets.open(fileName)
val outputStream = FileOutputStream(database.absolutePath)
inputStream.use { input ->
outputStream.use {
input.copyTo(it)
}
}
}
return AndroidSqliteDriver(NoteDatabase.Schema, context, fileName)
}
I tried this code on iOS but it’s not working.
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)
then tried this implementation:
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())
}
But nothing worked.
Can anyone provide any sample app or docs something?
thanks!hfhbd
07/28/2023, 9:12 PMColton Idle
07/29/2023, 1:45 PMContext
. should i just cave and make it an android gradle module? Should I just use the multiplatform docs instead?Colton Idle
07/29/2023, 2:03 PMjw
07/29/2023, 2:03 PMColton Idle
07/29/2023, 2:04 PMjw
07/29/2023, 2:05 PMColton Idle
07/29/2023, 2:05 PMColton Idle
07/29/2023, 2:05 PMColton Idle
07/29/2023, 2:06 PMalec
07/29/2023, 3:24 PMalec
07/29/2023, 3:24 PMColton Idle
07/29/2023, 3:25 PMColton Idle
07/29/2023, 3:30 PMColton Idle
07/29/2023, 3:31 PMDatabase.Schema.create(driver)
which was just because my android code didn't have that. wonder why i need that on the jvm but not android?
4. in android land i know that its typically good to only keep one database object around as a singleton that DI'd everywhere. in the case of SQLDelight, I'm not sure what I should keep as singletons. There's the Driver
, Database
and *Queries
. any tips on that?Shabinder Singh
07/30/2023, 6:00 PMitnoles
07/30/2023, 6:26 PMkrzysztof
07/31/2023, 11:51 AMDerek Ellis
07/31/2023, 12:12 PMgenerateAsync
enabled in your gradle config it will generate code for R2DBC. If you want to use JDBC you can just remove generateAsync
.krzysztof
07/31/2023, 12:21 PMDerek Ellis
07/31/2023, 12:56 PMeygraber
07/31/2023, 4:45 PMalec
07/31/2023, 4:46 PMBenoit Quenaudon
07/31/2023, 4:54 PMeygraber
07/31/2023, 7:19 PMLandry Norris
07/31/2023, 9:35 PMkevin.cianfarini
07/31/2023, 9:52 PMTim Kye
08/01/2023, 7:40 PMtrevjones
08/01/2023, 7:51 PM@JsonClass(generateAdapter = true, generator = "hand-written")
sealed class ClientMessage {
val type: String
get() = this.javaClass.name
companion object Unknown : ClientMessage()
}
@JsonClass(generateAdapter = true)
data class Launch(
val request: JobRequest
) : ClientMessage()
@Keep
class ClientMessageJsonAdapter(private val moshi: Moshi) : JsonAdapter<ClientMessage>() {
@Suppress("UNCHECKED_CAST")
private val actual by lazy {
PolymorphicJsonAdapterFactory.of(
ClientMessage::class.java,
"type",
)
.withSubtype(Launch::class.java, Launch::class.java.name)
.withDefaultValue(ClientMessage.Unknown)
.create(ClientMessage::class.java, emptySet(), moshi) as JsonAdapter<ClientMessage>
}
override fun fromJson(reader: JsonReader): ClientMessage? {
return actual.fromJson(reader)
}
override fun toJson(writer: JsonWriter, value: ClientMessage?) {
actual.toJson(writer, value)
}
}