0

I am trying to make some kind of comments using Firebase for Android. Just have one question, are there any way to add new items to array?

For example, if I have such kind of object enter image description here

If I am trying to push, it will convert it to map enter image description here

And I don't want to overwrite this object each time, because I will have multiuser support and it will fail at some point.

And I am trying to do it as a List to do not create DataTransferObjects for my models, and to support auto parsing using firebase.

Thanks! If there will be no ideas will go with creating Maps, actually.

My ObjectModel:

public class Company implements Parcelable {
    private String id;
    private String name;
    private String description;
    private List<Comment> comments;
}

Code for pushing item:

final DatabaseReference ref = companiesRef.child(companyId).child(NODE_COMMENTS).push();
        return Single.create(e -> ref.setValue(comment)
                .addOnCompleteListener(task -> {
                    e.onSuccess(task.isSuccessful());
                }));
Nick Titov
  • 588
  • 5
  • 19
  • Add some lines of your code – phatnhse Jul 16 '17 at 17:26
  • See the comments of this q: https://stackoverflow.com/questions/42872303/firebase-saves-data-as-list-and-sometimes-as-map-object – Thomas Jul 16 '17 at 17:27
  • I think Thomas specifically refers to this comment: https://stackoverflow.com/questions/42872303/firebase-saves-data-as-list-and-sometimes-as-map-object#comment72855846_42872303 – Frank van Puffelen Jul 16 '17 at 20:54

2 Answers2

1

This is how it works, look at the example below I have stored "0" as my first commentID and "1" as my second commentID. As you can see, I stored the lastCommentID as the commentID for the last comment in the lists.

{
  "comments": {
    "0": {
      "text": "mycomment",
      "time": "142351516"
    },
    "1": {
      "text": "secondcomment",
      "time": "153426564"
    }
  }

 "lastCommentId": "1"
}

So whenever you want to add new comment to the firebase you have to retrieve the lastCommentID first as a string and convert it to integer(E.g. 1) then add 1 to the value(E.g. 2) so that when you save the next comment it won't override the previous version.

Note that you have replace lastCommentID each time you add comment to the database.

{
  "comments": {
    "0": {
      "text": "mycomment",
      "time": "142351516"
    },
    "1": {
      "text": "secondcomment",
      "time": "153426564"
    },
    "2": {
      "text": "thirdcomment",
      "time": "153426564"
    }
  }

 "lastCommentId": "2"
}
UmarZaii
  • 1,355
  • 1
  • 17
  • 26
  • Thanks, the thing is I want to add new object(comment) to already existing list at Firebase. – Nick Titov Jul 16 '17 at 17:34
  • Sorry my bad, misunderstanding the situation. I see that you already found your answer. Why ask a question that you already know the answer? – UmarZaii Jul 16 '17 at 17:39
  • Umar, sorry, this code is for pushing object, but as I show on second screenshot it will convert list to map, and I am trying to insert new item into list, so that is not what I am looking for. Just added it to show what I tried already. So I still looking for answer – Nick Titov Jul 16 '17 at 17:44
  • So you want the next comment to be saved that have an id of a list? Like 0,1,2,3,4 and so on as for the id? But you don't want using the push function? Is that what you are trying to explain? – UmarZaii Jul 16 '17 at 17:58
  • Yes, kind of. Push function generate string id for new item, so this array became map. But I read docs and seems like firebase do not support arrays as I planned to use it. – Nick Titov Jul 16 '17 at 18:19
  • I know how to do it, I have done it before but i will not highly recommend using this option. You have to store the last id of the array in you firebase. Wait i will show you... – UmarZaii Jul 16 '17 at 18:23
0

You do not need to have a Model Class to write in Firebase, you can parse a JSON to Java Objects dinamically with Jackson and push it into Firebase.

You can parse a JSON as Java Objects with Jackson

. For example you can obtain: Object, List, Hashmap, Integer, String, etc. without POJO classes.

To see more please visit this answer