In Java, How do I cast a Realm Object into the class I wish to write to the Database?

1.5k Views Asked by At

I am currently working on a project where my Database has a very large number of tables (Approx 60 total). I am working to create the Database Helper class that will function as the writer/ reader to/ from the database. And example of my write method would be this:

public static void executeInsertInto(final Context context, final UserData passedObject){
    //Generate a realm object from the context
    Realm realm = Realm.getInstance(context);
    realm.executeTransaction(new Realm.Transaction() {
        @Override
        public void execute(Realm realm) {
            //Begin a transaction, create a new item from the passed object, and commit.
            realm.beginTransaction();
            UserData itemToWrite = realm.copyToRealm(passedObject);
            realm.commitTransaction();
        }
    });
    //Close the realm object to prevent leaks
    if (realm != null) {
        realm.close();
    }
}

As you can see from the code, I manually created an object of type UserData; The problem is, I either have to repeat this a total of 60 times (for the many types of objects / tables) or come up with a better way. This leads me to my question, is there a way to create an object using the type of passed object to determine the class? Something like the code below did not want to work as I am doing something fundamentally wrong with my Java code:

    public static void executeInsertInto(final Context context, final RealmObject passedObject){
            //Generate a realm object from the context
            Realm realm = Realm.getInstance(context);
            realm.executeTransaction(new Realm.Transaction() {
                @Override
                public void execute(Realm realm) {
                    //Begin a transaction, create a new item from the passed object, and commit.
                    realm.beginTransaction();
//The below line is flawed in that you cannot create an object that way, 
//But hopefully it illustrates what I am trying to accomplish
                    passedObject.getClass() itemToWrite = (passedObject.getClass()) realm.copyToRealm(passedObject);
                    realm.commitTransaction();
                }
            });
            //Close the realm object to prevent leaks
            if (realm != null) {
                realm.close();
            }
        }

Anyone have any Ideas? I would greatly appreciate it!

EDIT: Just to clarify further, Take Realm out of the picture for one second, assuming I have two classes (Class Car and Class Bike), both of which extend to Class Transportation, if I am passed class transportation as an argument, how can I determine which type is being passed and then once I do, how do I create a new object with that information?

-Sil

1

There are 1 best solutions below

1
On BEST ANSWER

All Realm classes extends RealmObject so you can use generics to accomplish it:

public static <T extends RealmObject> void executeInsertInto(final Context context, final T passedObject){
    Realm realm = Realm.getInstance(context);
    realm.executeTransaction(new Realm.Transaction() {
        @Override
        public void execute(Realm realm) {
            T itemToWrite = realm.copyToRealm(passedObject);
        }
    });
    realm.close();
}