9

When calling

FirebaseFirestore.getInstance().collection("myCollection").document("doc1").update("field1",myObject);

I get the error:

IllegalArgumentException: Invalid data. Unsupported type: com.myProg.objects.MyObject (found in field field1)

Even though I can add myObject to firestore when it is part of myDoc using Set method without a problem.

MyObject class (The simplest example):

public class MyObject{
   public int i;
}

Edit: my DB Structure before attempting:

myCollection ->

doc1:

field0 - "3"

field1 - null

also tried it without field1

Community
  • 1
  • 1
CaptainNemo
  • 1,532
  • 2
  • 22
  • 45

2 Answers2

14

So the only way, to update as of now is by using a map. In your case it should look like

Map<String, Object> updateMap = new HashMap();
updateMap.put("field1.i", myObject.i);

FirebaseFirestore.getInstance().collection("myCollection")
.document("doc1").update(updateMap);

I think Firestore should really update the APIs to facilitate updating of nested objects as a whole.

Dhara Bhavsar
  • 345
  • 4
  • 11
Debanjan
  • 2,817
  • 2
  • 24
  • 43
2

In my case i wanted to update the complex object in the firestore.

public class UserInitialModel { private List<ServiceItemModel> servicesOffered; }

public class ServiceItemModel{
private String serviceName;
private String price;
}

it was giving me error when i try to update java.lang.IllegalArgumentException: Invalid data. Unsupported type:

I Solved it by by using map

List<Map<String,Object>> list=new ArrayList<>();
        for (ServiceItemModel model:
        mUserInitialPresenter.getUserInitialModel().getServicesOffered()) {
            Map<String,Object> servicesOffered=new HashMap<>();
            servicesOffered.put("serviceName",model.getServiceName());
            servicesOffered.put("price",model.getPrice());
            list.add(servicesOffered);
        }
        dataMap.put("servicesOffered",list);
Adarsh Binjola
  • 216
  • 2
  • 10