Skip to content

Commit

Permalink
Update java compatibility example to latest Realm Java
Browse files Browse the repository at this point in the history
  • Loading branch information
rorbech committed Apr 3, 2024
1 parent ef064ef commit d67f484
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 9 deletions.
4 changes: 2 additions & 2 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ gradlePlugin {
}

java {
sourceCompatibility = Versions.sourceCompatibilityVersion
targetCompatibility = Versions.targetCompatibilityVersion
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}

repositories {
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ object Versions {
const val targetSdk = 33
const val compileSdkVersion = 33
const val buildToolsVersion = "33.0.0"
const val buildTools = "7.3.1" // https://maven.google.com/web/index.html?q=gradle#com.android.tools.build:gradle
const val buildTools = "7.4.2" // https://maven.google.com/web/index.html?q=gradle#com.android.tools.build:gradle
const val ndkVersion = "23.2.8568313"
const val r8 = "8.0.34" // See https://developer.android.com/build/kotlin-support
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.realm.kotlin.demo.javacompatibility">
<uses-permission android:name="android.permission.INTERNET"/>

<application
android:allowBackup="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ class MainApplication : Application() {

override fun onCreate() {
super.onCreate()
java = JavaRepository(this)
kotlin = KotlinRepository()
(1..100).forEach {
java = JavaRepository(this)
kotlin = KotlinRepository()
kotlin.close()
java.close()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ package io.realm.kotlin.demo.javacompatibility.data

interface Repository {
val count: Int

fun close()
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,67 @@ import io.realm.RealmModel
import io.realm.RealmObject
import io.realm.annotations.PrimaryKey
import io.realm.annotations.RealmClass
import io.realm.annotations.RealmField
import io.realm.kotlin.createObject
import io.realm.kotlin.demo.javacompatibility.TAG
import io.realm.kotlin.demo.javacompatibility.data.Repository
import io.realm.kotlin.log.LogLevel
import io.realm.kotlin.log.RealmLog
import io.realm.mongodb.App
import io.realm.mongodb.AppConfiguration
import io.realm.mongodb.Credentials
import io.realm.mongodb.sync.Subscription
import io.realm.mongodb.sync.SyncConfiguration
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import org.bson.types.ObjectId

// Realm Kotlin will try to process this class if using io.realm.RealmObject so use
// io.realm.RealmModel/@RealmClass approach instead
@RealmClass
open class JavaEntity : RealmModel {
@PrimaryKey
var _id: ObjectId = ObjectId()
var name: String = "JAVA"
}

open class JavaEntityWithBaseObject : RealmObject() {
@PrimaryKey
@RealmField("_id")
var id: String = ""
}

var app = App("devicesync-iieas")

class JavaRepository(appContext: Context) : Repository {

var realm: Realm

init {
Realm.init(appContext)
val config = RealmConfiguration.Builder()
io.realm.log.RealmLog.setLevel(io.realm.log.LogLevel.ALL)

val user = runBlocking(Dispatchers.IO) {
app.login(Credentials.emailPassword("xxxx", "xxxx"))
}
val config = SyncConfiguration.Builder(user)
.initialSubscriptions { realm, subscriptions ->
subscriptions.addOrUpdate(Subscription.create("JavaEntity", realm.where(JavaEntity::class.java)))
}
.name("java.realm")
.allowWritesOnUiThread(true)
.build()
Realm.deleteRealm(config)
realm = Realm.getInstance(config)
realm.executeTransaction {
realm.createObject(JavaEntity::class.java)
realm.createObject(JavaEntity::class.java, ObjectId())
val entities = realm.where(JavaEntity::class.java).findAll()
Log.d(TAG, "JAVA: ${entities.size}")
}
}

override val count: Int = realm.where(JavaEntity::class.java).findAll().count()

override fun close() {
realm.close()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import io.realm.kotlin.RealmConfiguration
import io.realm.kotlin.demo.javacompatibility.TAG
import io.realm.kotlin.demo.javacompatibility.data.Repository
import io.realm.kotlin.ext.query
import io.realm.kotlin.log.LogLevel
import io.realm.kotlin.log.RealmLog
import io.realm.kotlin.types.RealmObject
import io.realm.kotlin.types.annotations.PrimaryKey

Expand All @@ -36,7 +38,9 @@ class KotlinRepository: Repository {
val realm: Realm

init {
RealmLog.level = LogLevel.ALL
val config = RealmConfiguration.Builder(setOf(KotlinEntity::class))

.name("kotlin.realm")
.build()
Realm.deleteRealm(config)
Expand All @@ -47,4 +51,7 @@ class KotlinRepository: Repository {
}

override val count = realm.query<KotlinEntity>().find().size
override fun close() {
realm.close()
}
}
2 changes: 1 addition & 1 deletion examples/realm-java-compatibility/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath ("io.realm:realm-gradle-plugin:10.11.0")
classpath ("io.realm:realm-gradle-plugin:10.18.0")
classpath ("io.realm.kotlin:gradle-plugin:${Realm.version}")
}
}
Expand Down

0 comments on commit d67f484

Please sign in to comment.