Invalid Query. You cannot use 'array_contains' filters with 'not_in' filters

Viewed 185

I have a collection named channels.

Users are added to userIds array and the record is also saved to field named serverId.

I just need a list of newly created or updated rows to update to my local room db.

I update snapshotTime field every time any changes occurs.

Previously I used the following code, where lastTime is the max(snapshotTime) of local room db

FirestoreUtil.chatChannelCollectionRef
    .whereArrayContains("userIds", currentUserId)
    //.whereGreaterThan("snapshotTime", lastTime)
    .orderBy("snapshotTime", Query.Direction.ASCENDING)
    .startAfter(lastTime)
    .addSnapshotListener (activity) { snapshots, e ->
        if (e != null) {
            Log.e("exception occurred", "")
            return@addSnapshotListener
        }

        for (dc in snapshots!!.documentChanges) {
            when (dc.type) {
                DocumentChange.Type.ADDED -> {
                    val chatChannel = dc.document.toObject(ChatChannel::class.java)
                    chatChannel.serverId = dc.document.id
                    repository.insertOrUpdateChannel(channelDao, chatChannel)
                    Log.e(TAG, "ADDED chatChannel: ${dc.document.data} - ${dc.document.id}")
                }
                DocumentChange.Type.MODIFIED -> {
                    val chatChannel = dc.document.toObject(ChatChannel::class.java)
                    chatChannel.serverId = dc.document.id
                    repository.insertOrUpdateChannel(channelDao, chatChannel)
                    Log.e(TAG, "MODIFIED chatChannel: ${dc.document.data} - ${dc.document.id}")
                }
                DocumentChange.Type.REMOVED -> {
                    val chatChannel = dc.document.toObject(ChatChannel::class.java)
                    chatChannel.serverId = dc.document.id
                    //MessageDatabase.getDatabase(AjoowebApplication.applicationContext()).runInTransaction {
                    repository.deleteChannel(channelDao, chatChannel)
                    Log.e(TAG, "REMOVED chatChannel: ${dc.document.data} - ${dc.document.id}")
                }
                else -> {
                    Log.e(TAG, "MISSING: ${dc.document.data} - ${dc.document.id}")
                }
            }
        }
    }

which returns the updated fields but not the new records.

So I tried this query to get the new rows only:

here channels is a list with all channelsId in local db

FirestoreUtil.firestoreInstance
    .collection(FBCollections.CHANNEL_COL)
    .whereNotIn("serverId", channels)
    .whereArrayContains("userIds", currentUserId)
    .get()
    .addOnSuccessListener (activity) { snapshots ->
        for (dc in snapshots!!.documents) {
            val chatChannel = dc.toObject(ChatChannel::class.java)!!
            chatChannel.serverId = dc.id
            repository.insertOrUpdateChannel(channelDao, chatChannel)
            Log.e(TAG, "LOADED chatChannel: ${dc.data} - ${dc.id}")
        }
    }

Now the error encountered is:

Invalid Query. You cannot use 'array_contains' filters with 'not_in' filters

How can I get the newly created records as well ?

0 Answers
Related