androidlocationgoogle-cloud-firestoreandroid-roomgeopoints

GeoPoint does not allowed to be stored in Room


Edited: Tested as suggested by Nazariy Moshenskiy's answer

I tried storing GeoPoints object in android's abstraction data persistence layer Room. Seems like this operation is not allowed.

Project does not compile with following error:

error: Cannot figure out how to save this field into database. You can consider adding a type converter for it.

Are there any solutions to achieve this? (compatible TypeConverter)

PS: I do not want to change my model class' object type to long, long or any other type as I am using GeoPoints within my model class to store in Firestore and retrieve based on parameters we can provide to the query against the GeoPoints.


Solution

  • As the OPs solution did not work I decided to implement a TypeConverter to achieve this. Even though the question has lot of downvotes thinking this can't be achieved out of the box, I will share my solution for whoever actually tried and failed to achieve this.

    So I wrote a class called CustomTypeConverters and annotated two methods with @TypeConverter.

    1. stringToGeoPoint(String data)

    This method will take a JSON String as the input parameter. Then it will create a GeoPoint object using Gson library with the representation of GeoPoint.class and return it.

    1. geoPointToString(GeoPoint geoPoint)

    This method will take a GeoPoint object as the input parameter. Then it will create a JSON String object using Gson library and return it.

    CustomTypeConverters.java

    public class CustomTypeConverters {
    
        @TypeConverter
        public static GeoPoint stringToGeoPoint(String data) {
            return new Gson().fromJson(data, GeoPoint.class);
        }
    
        @TypeConverter
        public static String geoPointToString(GeoPoint geoPoint) {
            return new Gson().toJson(geoPoint);
        }
    
    }
    

    Then we need to point this converter using annotations within GeoPoint object we defined in our POJO. Assume I have a User class with a GeoPoint object,

    @TypeConverters(CustomTypeConverters.class)
    private GeoPoint geoPoint;
    

    I verified the answer by browsing, data/data/your-application-package/databases in the device I am running this code.