Skip to content

Instantly share code, notes, and snippets.

@tmtrademarked
Created December 12, 2016 02:06
Show Gist options
  • Save tmtrademarked/6be88ada94f5293324f9da0b16178432 to your computer and use it in GitHub Desktop.
Save tmtrademarked/6be88ada94f5293324f9da0b16178432 to your computer and use it in GitHub Desktop.
RealmGate example
import android.content.Context;
import com.blueapron.service.dagger.scopes.AppContext;
import com.blueapron.service.models.NetworkModel;
import java.util.List;
import io.realm.Realm;
import io.realm.RealmConfiguration;
import io.realm.RealmObject;
/**
* The RealmGate serves as a holder around Realm instances. This makes it easier to inject.
*/
public class RealmGate {
public RealmGate(@AppContext Context context) {
Realm.init(context);
RealmConfiguration config = new RealmConfiguration.Builder()
// IMPORTANT - this is only OK while in development.
.deleteRealmIfMigrationNeeded()
.build();
Realm.setDefaultConfiguration(config);
}
public Portal open() {
return new Portal(Realm.getDefaultInstance());
}
public void upsert(RealmObject local) {
try (Portal realm = open()) {
realm.beginTransaction();
realm.insertOrUpdate(local);
realm.commitTransaction();
}
}
public void upsert(List<? extends RealmObject> local) {
try (Portal realm = open()) {
realm.beginTransaction();
realm.insertOrUpdate(local);
realm.commitTransaction();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment