Device Sync returns an Empty List from a Collection - MongoDB Sync (Android's Kotlin SDK)

I have a ‘Reward’ collection that I’m trying to fetch the data from. I want to get all documents from that collection in my android app, where I’m using a Device Sync. The problem is, I already have two documents there, but when I try to fetch the data, I get an empty list? Even though I don’t see any error in the logs. Strange. Plus I’ve enabled the rules so that everyone can read/write to it. And I’ve added ‘type’ field as a queriable field to the Device Sync as well. Btw I’m able to fetch the data from my other collection “Student” successfully.

Development mode is enabled, this is the schema:

{
  "title": "Reward",
  "type": "object",
  "required": [
    "_id",
    "coins",
    "description",
    "price",
    "subtitle",
    "title",
    "type"
  ],
  "properties": {
    "_id": {
      "bsonType": "objectId"
    },
    "coins": {
      "bsonType": "long"
    },
    "description": {
      "bsonType": "string"
    },
    "price": {
      "bsonType": "double"
    },
    "subtitle": {
      "bsonType": "string"
    },
    "title": {
      "bsonType": "string"
    },
    "type": {
      "bsonType": "string"
    }
  }
}

Model class (Realm object):

open class Reward: RealmObject {
    @PrimaryKey
    var _id: ObjectId = ObjectId()
    var type: String = "premium"
    var title: String = ""
    var subtitle: String = ""
    var description: String = ""
    var price: Double = 0.0
    var coins: Int = 0
}

MongoDB data source:

object MongoDB : MongoRepository {
    private val app = App.create(APP_ID)
    private val user = app.currentUser
    private lateinit var realm: Realm

    init {
        configureTheRealm()
    }

    override fun configureTheRealm(): RequestState<Boolean> {
        return if (user != null) {
            try {
                val config = SyncConfiguration.Builder(
                    user,
                    setOf(Student::class, Reward::class)
                )
                    .waitForInitialRemoteData(15.seconds)
                    .initialSubscriptions { sub ->
                        add(
                            query = sub.query<Student>(query = "ownerId == $0", user.id),
                            name = "Student subscription"
                        )
                        add(
                            query = sub.query<Reward>(query = "type == $0", "premium"),
                            name = "Reward subscription"
                        )
                    }
                    .log(LogLevel.ALL)
                    .build()
                realm = Realm.open(config)
                RequestState.Success(data = true)
            } catch (e: Exception) {
                RequestState.Error(message = e.message.toString())
            }
        } else {
            RequestState.Error(message = MONGO_USER_NULL_MESSAGE)
        }
    }

    @OptIn(ExperimentalFlexibleSyncApi::class)
    override suspend fun getRewards(): Flow<RequestState<List<Reward>>> {
        return if (user != null) {
            return try {
                realm.query<Reward>(query = "type == $0", "premium")
                    .find()
                    .subscribe(mode = WaitForSync.ALWAYS, timeout = 15.seconds)
                    .asFlow()
                    .map { RequestState.Success(data = it.list) }
            } catch (e: Exception) {
                flow { emit(RequestState.Error(e.message.toString())) }
            }
        } else {
            flow { emit(RequestState.Error(MONGO_USER_NULL_MESSAGE)) }
        }
    }

}
1 Like

To fix this issue, I’ve had to create a function for adding a document in that collection, and afterwards I was able to fetch the data. Just if someone else have the same issue…

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.