-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Request for community comments: Better Kotlin support #4701
Comments
🎊 Kotlin!!! 🎊 When you say problems you are having with Realm using Kotlin, I do not have many problems. realm-java works really well with Kotlin at the moment. I have a few apps that are 100% Kotlin and Realm code bases. Works great. My vote for priority is your list item #2. Here is an example of a Realm model I create in a Kotlin app using Kotlin nullability data type in them for optional fields:
So not needing annotations and instead using the data types of my Kotlin properties would be awesome. |
Was there a way to support a non-null initializing value (like "empty RealmResults") for a |
It would be relatively easy if we throw on creating queries, basically just creating an empty stub object, but if queries should be allowed it gets enormously more tricky. |
I think we can do something for |
@zaki50 Got anything specific in mind? |
@cmelchior Sorry, no. |
Maybe async queries could use Kotlin coroutines. |
|
why not something like /**
* Group conditions on a [RealmQuery].
*/
fun <E : RealmModel> RealmQuery<E>.group(body: (RealmQuery<E>) -> Unit) { // <-- changed here
beginGroup()
body()
endGroup()
} I'm not sure about Kotlin generic syntax, but you get the idea. |
@Zhuinden You're right! But it would be even better if the higher order function ( /**
* Group conditions on a [RealmQuery].
*/
fun <E : RealmModel> RealmQuery<E>.group(body: RealmQuery<E>.() -> Unit) {
beginGroup()
body()
endGroup()
} |
Another Kotlin fun fact:
Even better would be supporting the KClass equivalents directly:
|
@cmelchior I wonder if there would be something like |
Adding a method similar to
We can write a transaction like:
|
Adding static methods in
|
Supporting type-safe query.
|
Type-safe queries are a nice idea, but right now it would require a dependency to |
Maybe it would be worth it to pull out Kotlin support in a separate dependency. Then developers could decide on their own if they need type safety. |
use
|
I was thinking so, but it seems to be working without kotlin-reflect dependency. |
Right, it's not using reflection. I thought this SO answer was a great explanation, helped me grok it a bit better. https://stackoverflow.com/questions/45949584/what-does-the-reified-keyword-in-kotlin-really-do I wonder if Type Safe builders could be used in some creative way for complex queries
Not super elegant, but it is type safe. :-) |
In Kotlin /**
* Map Realm's [RealmQuery.in] methods to `oneOf` to bypass using Kotlin's `in` keyword
* as a method name.
*/
fun <E : RealmModel> RealmQuery<E>.oneOf(fieldName: String,
values: Array<String>): RealmQuery<E> = `in`(fieldName, values) |
@heinrichreimer I wrote the same code in #4684 ! https://github.com/realm/realm-java/pull/4684/files#diff-ffacb4808566a8a610dec8f6cfacc93fR72 |
@heinrichreimer the future says that the function we mentioned should have |
With an awesome I/O17 being over, one of the big announcements were official support for Kotlin.
Realm is already fully interoperable with Kotlin and we have a Kotlin example here and here that demonstrates how it works.
But we want to make that support even better and In order to do that, we are looking for feedback on what you would most like to see changed so Realm works even better with Kotlin
We have a few ideas ourselves:
(DONE): Add extension functions for all classes with methods that currently accept
Class<? extends RealmModel>
soKClass
can be used directly, e.g.realm.where(Person::class)
instead ofrealm.where(Person::class.java)
.(DONE) Detect nullability automatically in model classes. Right now you are required to use the Realm
@Required
annotation, but we can add support for JetBrains@NotNull
as well which is how the Kotlin bytecode indicate not-null values. Examplevar name : String
instead of of@Required var name : String
(DONE) Annotate our public API with JSR305 annotations in a similar way to Okio/OKHttp. This means that Realm will expose correctly the nullability of return values and input paramaters. For example:
val result : RealmResults<Person> = realm.where(Person::class).findAll()
instead ofval results : RealmResults<Person>? = realm.where(Person::class).findAll()
. Also being tracked here: JSR305 annotations for nullability #4643But we are very interested what problems you are running into when using Realm from Kotlin.
The text was updated successfully, but these errors were encountered: