Search code examples
javaandroidgoogle-maps-android-api-2realmparceler

Is it possible to persist 3rd Party Parcelable Objects in Realm?


Does Realm provide support to persist 3rd Party Parcelable Objects (like MarkerOptions class from Maps API)?

So, I'm building a route planning app for Android and I need to persist a list of LatLng, MarkerOptions and Polyline objects from the Maps API - all of which implement Parcelable. I thought I'd try Realm out to persist the list of objects.

I read about Parceler library support in Realm and was trying to persist a Parcelable class which contains LatLng object in Realm.

import io.realm.RealmObject;
import io.realm.SavedLocationRealmProxy;

@Parcel
public class SavedLocation extends RealmObject{

private String locationName;
private LatLng location;
private String areaName;

public SavedLocation() {
}

public SavedLocation(String locationName, LatLng location) {
    this.locationName = locationName;
    this.location = location;
}

public SavedLocation(String locationName, LatLng location, String areaName) {
    this.locationName = locationName;
    this.location = location;
    this.areaName = areaName;
}

...

Compilation does not complete with this error

Error:(7, 8) error: Type com.google.android.gms.maps.model.LatLng of field location is not supported

I also tried adding this annotation as directed by Realm documention

@Parcel(implementations = { SavedLocationRealmProxy.class },
    value = Parcel.Serialization.BEAN,
    analyze = { SavedLocation.class })
public class SavedLocation extends RealmObject{
...

However, SavedLocationRealmProxy does not get created due to enclosing LatLng class.

Is the support for Parceler just provided to make RealmObjects parcelable or are Parcelable Objects persistable in Realm?

Thanks..


Solution

  • The support from Parceler allows RealmObjects to be parceled in Intents/etc (as CommonsWare pointed out in the comments). Note, when RealmObjects are parceled, Realm objects become disconnected from the Realm.

    Why are you getting this error?

    LatLng does not extend RealmObject. Therefore it cannot be saved to Realm. In order to save your LatLng objects you will need to create a LatLng object of your own (like MyLatLng or something) and map them over.

    If you're looking for a good example of Geo Points with Realm, you might want to check out Thorben Primke's realm-mapview example here: https://github.com/thorbenprimke/realm-mapview

    In a related noted, Realms geo point support can be tracked here: https://github.com/realm/realm-java/issues/1772