Kotlin-Realm-Extensions icon indicating copy to clipboard operation
Kotlin-Realm-Extensions copied to clipboard

How to search in field of type List?

Open ahmedkamalio opened this issue 5 years ago • 0 comments

Consider the following model

@RealmClass
open class StylistModel(
    @PrimaryKey
    var id: String,
    var categoriesIds: List<String>
) : RealmModel

I need to query all stylists those have specific category id.

// example
StylistModel().queryAsFlowable { includes("categoriesIds", id) }

One more thing 😊 , in most databases I can do something like this stylists.where("id > someValue") to skip all objects with id < someValue, notice that the id field is String how can I achieve the same result using these extensions?

Edit For now, I'm working around it by querying all objects and filter them manually like the following

    fun getStylists(
        countryName: String,
        countryCode: String,
        categoryId: String,
        latestId: String
    ): Flowable<List<StylistEntity>> {
        return StylistModel().queryAllAsFlowable()
            .map { list ->
                list.asSequence().map { stylist ->
                    stylist.toEntity()
                }.sortedWith(Comparator { a, b ->
                    when {
                        a.id > b.id -> 1
                        a.id < b.id -> -1
                        else        -> 0
                    }
                }).filter { stylist ->
                    stylist.countryCode == countryCode
                }.filter { stylist ->
                    stylist.countryName == countryName
                }.filter { stylist ->
                    stylist.categoriesIds.contains(categoryId)
                }.filter { stylist ->
                    stylist.id > latestId
                }.toList()
            }.defaultIfEmpty(emptyList())
    }

ahmedkamalio avatar Jun 06 '19 11:06 ahmedkamalio