Rightpoint / android-template

A `cookiecutter` template for Android projects
28 stars 4 forks source link

Potential Issue w/ Realm Cache Implementation #10

Closed octohub closed 5 years ago

octohub commented 6 years ago

@jonduran3000 @tetedoie Can one of you chime in on this critique of our Realm implementation?

octohub commented 6 years ago

I spoke to @tetedoie regarding this. He will be fixing it.

tetedoie commented 6 years ago

So the critique is regarding this dagger module

@Module
 public abstract class DatabaseModule {
     @Provides
     @Singleton
     static Realm providesDatabase(Application app) {
         Realm.init(app);
         RealmConfiguration configuration = new RealmConfiguration.Builder()
            .deleteRealmIfMigrationNeeded()
            .build();
        return Realm.getInstance(configuration);
    }
 }

This line Realm.init(app); initialize the Realm library and should be done only one time on the onCreate() of the application class. (Not sure if it’s safe to call it multiple time) This line .deleteRealmIfMigrationNeeded() should never be used in production, it’s an option to help during db schema development to avoid having to write migration code between schema change.

Realm instances, RealmObjects and RealmResults cannot be share between thread (at least yet) but it’s totally possible to get a Realm instance in the main thread to perform an async query using Realm async api. What is mentioned in the critic is also true, with this Realm instance Singleton created in the main thread, we’ll not be able to perform query from a background thread.

octohub commented 5 years ago

Handled: https://github.com/Raizlabs/android-template/pull/43