0

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..

vepzfe
  • 4,217
  • 5
  • 26
  • 46
  • Do a workaround: Create your own Parcel class that contains doubles representing the Latitude / Longitude. Since LatLng is not supported you can always get around that with your own class, albeit seems counter intuitive, just call it like MarkerOptionsEntity. Now you are working with a DTO / Entity mapping type of model which is not too far fetched – Lucas Crawford Jan 19 '16 at 19:23
  • Just because a class implements `Parcelable` does not mean that it can be persisted, via Realm or any other means. The only purpose of `Parcelable` is to be able to put instances in a `Parcel` for IPC. – CommonsWare Jan 19 '16 at 19:28
  • You can only persist `RealmObject`s in `Realm`. You'd need your own `RealmLatLng`, `RealmMarkerOptions` and `RealmPolyline` classes that each `extends RealmObject`, and then you'd need to map between the two types of classes. – EpicPandaForce Jan 19 '16 at 21:29

1 Answers1

0

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

Donn Felker
  • 9,553
  • 7
  • 48
  • 66
  • Thank you Donn. That clears my doubts. I already created a new LatLng class of my own which extends Realm object following yours and Lucas's answers. In my getter methods I was trying to return google's LatLng objects itself as I thought this would not need me to change all the code which already uses LatLng, however Realm does not allow any methods apart from getter setter which return the class objects due to their Proxy classes. I'm just trying to persist the data, but it seems to be getting more and more tricky to save 3rd Party objects using Realm. – vepzfe Jan 20 '16 at 09:50
  • @abhiank Recent changes in Realm are giving you more flexibility with RealmObjects and custom methods and logic. Please see this news release from Realm about custom logic in RealmObjects: https://realm.io/news/android-installation-change/ Enjoy! – Donn Felker Jan 20 '16 at 18:05
  • thats good!.. fortunately I was able to make it work .. some shitty code here and there :P .. but saving and retrieving is working now :) – vepzfe Jan 21 '16 at 18:48