skip to Main Content

I have this data class written:

package com.my.app

data class User(
    var uid: String,
    var nickname: String,
    var email: String,
    var description: String = "",
    var avatar: String = "default",
    var banReason: String = "",
    var bannedBy: String = "",
    var pin: String = "", // encrypted
    var isBanned: Boolean = false,
    var isVerified: Boolean = false,
    var isPremiumAccount: Boolean = false,
    var isAdministrator: Boolean = false,
    var isModerator: Boolean = false,
    var isPinEnabled: Boolean = false,
    var bannedTo: Long = 0,
    var createdAt: Long = 0
){
    constructor(): this(
        "",
        "",
        "",
        "",
        "default",
        "",
        "",
        "",
        false,
        false,
        false,
        false,
        false,
        false,
        0,
        java.sql.Timestamp(System.currentTimeMillis()).time
    )
}

and I used that data class to parse documents into that class with this code:

val user = userDocumentReference.toObject(User::class.java)

and everything worked until I added likedPosts collection to the user document – then val user is always null

Question – is there any way to put this collection into a data class with a code similar to, for example:

data class User(
    var uid: String,
    var nickname: String,
    [...]
    var likedPosts: MutableList<String> // List with liked posts uid's from likedPosts Collection

or maybe

data class User(
    var uid: String,
    var nickname: String,
    [...]
    var likedPosts: LikedPosts() // LikedPosts() is another data class

Or maybe is there a workaround? I can’t put everything in one document due to the document size limitation of 1 MB.

Or maybe I have to go back to manually assigning values from documents?

Firestore-root
   |
   --- users (collection)
         |
         |  (document)                  (collection)
         --- $userId ------------------- likedPosts
                |                             |
                --- username: "user"       $postUid --- postUid: "xxxxxxxxxxxxxxxa"
                |                             |
                --- avatar: "default"      $postUid --- postUid: "xxxxxxxxxxxxxxxb"
                |                             |
                --- isBanned: false        $postUid --- postUid: "xxxxxxxxxxxxxxxc"
                |
                --- isVerified: true

EDIT

The value was null because I made a mistake and my app was looking for a user document that does not exist – anyway, is it possible to access the likedPosts collection that is in the document, in one data class?

2

Answers


  1. You can add one data class to another, create list and put data class inside it, and this list will have items which type of that added data class:

    data class User(
        var uid: String,
        var nickname: String,
        [...]
        var likedPosts: MutableList<LikedPosts>
    
    Login or Signup to reply.
  2. You cannot get a User document together with all documents that exist inside the likedPosts sub-collection in a single go. This is not possible because a document is not tied to the documents that exist in a sub-collection. Bear in mind that the queries in Firestore are shallow, it can only return documents from the collection that the query is run against. There is no way to get documents from a top-level collection and a sub-collection in a single query. So if you need to read a User document and also get all the documents that exist in the likedPosts sub-collection, then you have to perform two separate requests. Once you have all documents that exist in the sub-collection, you can add them to a list inside the User object.

    If you however need to have such a functionality, then you should consider using the Realtime Database, where you can read a node along with everything beneath it.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search