Trying To Set a Nested Object - Getting "IllegalArgumentException: Invalid data. Unsupported type"

时间:2018-05-06 17:16:46

标签: android firebase google-cloud-firestore

I have a null object which has a { Email: "test1@gmail.com", selectedanswer: null } property. This property is initialised to be Race . This is how this object's randomArticlesPair looks like in the database:

enter image description here

I'd like to null the document property later and put a set instead, and this is how I do it:

randomArticlesPair

This is the custom object object that I'm trying to insert:

CollectionReference usersRef= FirebaseFirestore.getInstance().collection(FirebaseConstants.USERS);
DocumentReference senderRef=usersRef.document(raceRoom.getSenderEmail()).collection(FirebaseConstants.RACES_FEED).document(raceRoomID);

Map<String,Object> randomArticlesPair=new HashMap<>();
randomArticlesPair.put(FirebaseConstants.RANDOM_ARTICLES_PAIR,raceRoom.getRandomArticlesPair());

senderRef.set(randomArticlesPair,SetOptions.merge());

Its properties are two RandomArticlePair objects:

public class RandomArticlesPair {

    private RandomArticle randomArticleA;
    private  RandomArticle randomArticleB;

    public RandomArticlesPair(){}

    public RandomArticlesPair(RandomArticle randomArticleA, RandomArticle randomArticleB) {
        this.randomArticleA = randomArticleA;
        this.randomArticleB = randomArticleB;
    }

    public RandomArticle getRandomArticleA() {
        return randomArticleA;
    }

    public RandomArticle getRandomArticleB() {
        return randomArticleB;
    }
}

My problem is that after trying to add the new data, I'm getting this error:

RandomArticle

Inserting a string works just fine:

@IgnoreExtraProperties
public class RandomArticle {
    private String title, firstParagraph, id, url;
    @Exclude private List<Linkshere>backTemplates_List;
    @Exclude private List<Backlink>backlinks_List;

    public RandomArticle(){}

    public RandomArticle(String title, String firstParagraph, String id, String fullUrl){
        this.title=title;
        this.firstParagraph=firstParagraph;
        this.id=id;
        this.url=fullUrl;
    }

    public RandomArticle(String title, String fullUrl){
        this.title=title;
        this.url=fullUrl;
    }

    public RandomArticle(String title, String firstParagraph, String id, String fullUrl, List<Linkshere>backTemplates_List){
        this.title=title;
        this.firstParagraph=firstParagraph;
        this.id=id;
        this.url=fullUrl;
        this.backTemplates_List=backTemplates_List;

    }

    public String getTitle(){
        return this.title;
    }

    public String getFirstParagraph(){
        return this.firstParagraph;
    }

    public String getId(){
        return this.id;
    }

    public String getURL(){
        return this.url;
    }

    @Exclude public List<Linkshere> getBacktemplatesList(){
        return this.backTemplates_List;
    }

    @Exclude public void setBacklinks_List(List<Backlink> backlinks_List) {
        this.backlinks_List = backlinks_List;
    }

    @Exclude public List<Backlink> getBacklinksList(){
        return this.backlinks_List;
    }

}

enter image description here


How can I fix this?

1 个答案:

答案 0 :(得分:1)

通常,期望您使用POJO或原始类型的映射,但不要混用它们。在这种特殊情况下,由于您已经拥有Race对象,我认为您可以直接编写它,然后稍后再次更新。像这样:

Race race = createRaceObjectWithNullRandomArticlesPair();
senderRef.set(race);
...
race.setRandomArticlesPair(...);
senderRef.set(race, SetOptions.merge());

这对您的情况有用吗?

允许POJO映射的困难在于,不清楚它们应该如何被提取,我们试图避免读/写之间的不对称,因为我们认为它会导致更长时间的混乱术语。

例如,您可以将Map.class传递给DocumentSnapshot.toObject()方法,但由于java的类型擦除,很难指定您想要一个字符串映射到RandomArticlesPair对象。 (虽然这实际上并不是不可能的。你可以使用像这里描述的那样的方法:http://gafter.blogspot.ca/2006/12/super-type-tokens.html但是这有点偏离主题,因为Firestore目前不支持这样的东西。)