Reputation: 3614
I am trying to persist a custom object using the following code:
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
DatabaseReference curWorkoutExercisesRef = databaseReference.child("workouts")
.child(mCurrentWorkout.getId())
.child("workoutExercises");
WorkoutExercise we = new WorkoutExercise(exercise);
curWorkoutExercisesRef.push().setValue(we);
Here's my object:
public class WorkoutExercise {
private String id;
private Exercise exercise;
public WorkoutExercise() {}
// getters, setters, other methods
// ...
}
public class Exercise {
private String id;
private String title;
private List<BodyPart> bodyParts;
public Exercise() {}
// getters, setters, other methods
// ...
}
public class BodyPart {
private String id;
private String name;
public BodyPart() {}
// getters, setters, other methods
// ...
}
And every time I am getting this error - com.google.firebase.database.DatabaseException: Serializing Arrays is not supported, please use Lists instead
. My objects don't contain any arrays so this error looks quite confusing. I found a way to fix this error - everything works fine if I add the @Exclude
annotation to the bodyParts
list from my Exercise
class but it is obviously not what I am trying to achieve.
So seems that Firebase is unable to persist objects containing inner lists? Are there any straightforward workarounds or best practices here? Thanks!
P.S. I am using firebase-database:9.2.1
Upvotes: 24
Views: 23051
Reputation: 59
When you use hashmap to set values use this to get url of image you're uploading.
task.getResult().getDownloadUrl().toString()
Upvotes: 0
Reputation: 31
//this will recreate your Object list by the constructor
private void handleData(){
orderList = Db.getCarts();
orders = new ArrayList<>();
Double totalPrice = 0.0;
for (Order order : orderList){
// totalPrice = totalPrice + ( price * quantity )
totalPrice += (Double.valueOf(order.getPrice())* Integer.valueOf(order.getQuantity()));
orders.add(new Order(order.getProductId(),order.getProductName(),order.getQuantity(),order.getPrice(),order.getDiscount(),order.getImage()));
}
}
private void alertDialog() {
AlertDialog.Builder alert = new AlertDialog.Builder(getContext());
alert.setTitle("One more step!");
alert.setMessage("Enter your address");
final EditText edtaddress = new EditText(getContext());
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT
);
edtaddress.setLayoutParams(layoutParams);
alert.setView(edtaddress);
alert.setIcon(R.drawable.ic_shopping_cart_black_24dp);
alert.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Request request = new Request(
Common.currentUser.getPhone(),
Common.currentUser.getName(),
edtaddress.getText().toString(),
textTotal.getText().toString(),
orders
);
// Submit to firebase
// We will using system.currentMillis to key
requestReference.child(String.valueOf(System.currentTimeMillis()))
.setValue(request);
//Delete cart
Db.deleteCart();
Toast.makeText(getContext(), "Thank you , Order Place", Toast.LENGTH_SHORT).show();
getActivity().finish();
}
});
alert.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alert.show();
}
Upvotes: 1
Reputation: 3928
In case this helps others that are receiving the same error, I was getting the same error and the solution ended up being the following for me:
Changing:
databaseReference.child("somechild").setValue(etName.getText());
To:
databaseReference.child("somechild").setValue(etName.getText().toString());
As @fraggjkee points out, Firebase attempts to serialize getters, and this was the similar issue here with firebase attempting to serialize the result of the .getText()
.
Hope this helps others!
Upvotes: 21
Reputation: 1088
Here's how this was resolved for me. The issue occurred when I was trying to update an int field in database. I was getting text from edit text field and forgot to convert that into an int. This caused the failure. So try checking the data type you're sending to the database if this issue occurs.
Upvotes: 0
Reputation: 51
Add @Exclude to the functions that FB tries to interpret, like those that return a Drawable.
@Exclude
public Drawable getUnitColorImage(){
return TextDrawable.builder().buildRound("", prefColor);
}
Upvotes: 4
Reputation: 12900
Add @IgnoreExtraProperties to your class
@IgnoreExtraProperties
public class YourPojo {
public String name;
// Default constructor for Firebase
public YourPojo(){}
// Normal constructor
public YourPojo(String name) {
this.name = name;
}
}
Upvotes: 2
Reputation: 1609
I had a similar problem, I was trying to use groovy instead of Java, I guess that groovy is generating other get methods for me. I converted my domain class from Pogo (src/main/groovy) to Pojo (src/main/java) and now all is working well
Upvotes: 1
Reputation: 3614
I've managed to find the reason causing this crash. I installed the app on another device running Android 5.x and Firebase threw a much less confusing exception there:
com.google.firebase.database.DatabaseException: No properties to serialize found on class android.graphics.Paint$Align
It appeared that Firebase (unlike Gson) tries to serialize all possible getters even though they don't have direct connection with global variables (class members / fields), in my particular case one of my objects contained a method returning Drawable
- getDrawable()
. Obviously, Firebase doesn't know how to convert drawable into json.
Interesting moment (probably a bug in Firebase SDK): on my older device running Android 4.4.1 I still get my original exception in the same project and configuration:
Caused by: com.google.firebase.database.DatabaseException: Serializing Arrays is not supported, please use Lists instead
Upvotes: 16