Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example of creating interop between Realm Kotlin and a Java project. #42

Merged
merged 17 commits into from
Feb 16, 2024

Conversation

cmelchior
Copy link
Contributor

We are getting a few questions on how to do this. This sample project demonstrates one way of doing it by exposing a RealmRepository from the Kotlin module that has a Java-friendly interface.

It should probably not be merged as is and needs to be discussed.

@cla-bot cla-bot bot added the cla: yes label Aug 9, 2023
Copy link
Contributor

@rorbech rorbech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Should also be added to the root's README list of samples.

var favoriteChild: Child? = null

// Expose method making it easier to use from Java
fun setChildren(children: ArrayList<Child>) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If named set then we should probably clear the underlying list before adding children.

@cmelchior
Copy link
Contributor Author

I restructured the sample a bit and added all the needed docs. This PR should now be ready for review

Copy link
Contributor

@rorbech rorbech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 😎

Copy link
Contributor

@clementetb clementetb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Collaborator

@nhachicha nhachicha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

}

lateinit var realm: Realm
val realmDispatchers = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() - 1).asCoroutineDispatcher()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use a fixed single thread? this is only used from realm.query<Person>

fun closeRealm() {
checkRealm()
realmScope.cancel("Closing Realm")
realmDispatchers.close()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't we also shutdown the realmDispatchers executor?

val config = RealmConfiguration.Builder(schema = setOf(Person::class, Child::class))
.name("javainterop-example.realm")
.build()
Realm.deleteRealm(config) // Cleanup any old test data
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: This might be surprising if the user re-run the same app unless documented ...

}

fun writeData(person: Person): Person {
return realm.writeBlocking {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing checkRealm() same for the below functions

callback.update(event)
}
}
return CancellationToken(job)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also return Java's Closeable instead of CancellationToken

@nhachicha nhachicha merged commit 131de27 into main Feb 16, 2024
1 check passed
@nhachicha nhachicha deleted the cm/java-interop-sample branch February 16, 2024 00:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants