Created
December 12, 2016 02:06
-
-
Save tmtrademarked/6be88ada94f5293324f9da0b16178432 to your computer and use it in GitHub Desktop.
RealmGate example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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