Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parceler fails when used with with Lifecycle-viewmodel 2.2.0 #382

Closed
LNdame opened this issue Mar 11, 2020 · 7 comments
Closed

Parceler fails when used with with Lifecycle-viewmodel 2.2.0 #382

LNdame opened this issue Mar 11, 2020 · 7 comments

Comments

@LNdame
Copy link

LNdame commented Mar 11, 2020

Description

Bumping from androidx.lifecycle:lifecycle-viewmodel:2.0.0 to androidx.lifecycle:lifecycle-viewmodel:2.2.0 casuse the following issue
Parceler: Unable to find read/write generator for type java.util.Map<java.lang.String, java.lang.Object> for androidx.lifecycle.ViewModel.mBagOfTags

mBagsofTags is a private field that is in the lifecycle.ViewModel
private final Map<String, Object> mBagOfTags = new HashMap<>();

Resolution attempt

Creating a custom converter for the field was unfruitful as the field is private



    @Override
    public void mapKeyToParcel(String key, Parcel parcel) {
        parcel.writeParcelable(Parcels.wrap(key),0);
    }

    @Override
    public void mapValueToParcel(Object value, Parcel parcel) {
        parcel.writeParcelable(Parcels.wrap(value),0);
    }

    @Override
    public String mapKeyFromParcel(Parcel parcel) {
        return Parcels.unwrap(parcel.readParcelable(java.lang.String.class.getClassLoader()));
    }

    @Override
    public Object mapValueFromParcel(Parcel parcel) {
        return Parcels.unwrap(parcel.readParcelable(java.lang.Object.class.getClassLoader()));
    }
}
@johncarl81
Copy link
Owner

What object are you annotating with @Parcel?

@sup-fmarais
Copy link

Different person, same project :) I have done some digging and I am pretty sure this is our culprit.

@Parcel
public class DataManagerCachedDataImpl extends ViewModel
        implements DataManagerCachedData {

Do you have some insight into a workaround we could use?

@johncarl81
Copy link
Owner

Hi @maraisf! Parceler will attempt to serialize everything, including data members of the inherited classes. I'd recommend avoiding extending things like the ViewModel (which probably has a `Map<String, Object> field) if possible.

@sup-fmarais
Copy link

Thanks for the quick response and guidance! 👍

@johncarl81
Copy link
Owner

Sure @maraisf, does this solve your issue?

@sup-fmarais
Copy link

Removing the extends ViewModel does infact resolve the error above. Thanks.

@johncarl81
Copy link
Owner

Cool. You could also use the analyze parameter include or exclude ViewModel from the generated Parcelable, but, generally, I'd recommend keeping the type hierarchy limited.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants