Created
September 3, 2016 14:36
-
-
Save Zhuinden/f1cef540c31642408c982c82b491dc40 to your computer and use it in GitHub Desktop.
RealmManager from book example
This file contains 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 android.util.Log; | |
import io.realm.Realm; | |
import io.realm.RealmConfiguration; | |
/** | |
* Created by Zhuinden on 2016.08.16.. | |
*/ | |
public class RealmManager { | |
static Realm realm; | |
static RealmConfiguration realmConfiguration; | |
public static void initializeRealmConfig(Context appContext) { | |
if(realmConfiguration == null) { | |
setRealmConfiguration(new RealmConfiguration.Builder(appContext) | |
.deleteRealmIfMigrationNeeded() | |
.build()); | |
} | |
} | |
public static void setRealmConfiguration(RealmConfiguration realmConfiguration) { | |
RealmManager.realmConfiguration = realmConfiguration; | |
Realm.setDefaultConfiguration(realmConfiguration); | |
} | |
private static int activityCount = 0; | |
public static Realm getRealm() { | |
return realm; | |
} | |
public static void incrementCount() { | |
if(activityCount == 0) { | |
if(realm != null) { | |
if(!realm.isClosed()) { | |
realm.close(); | |
} | |
} | |
realm = Realm.getDefaultInstance(); | |
} | |
activityCount++; | |
} | |
public static void decrementCount() { | |
activityCount--; | |
if(activityCount <= 0) { | |
activityCount = 0; | |
realm.close(); | |
Realm.compactRealm(realmConfiguration); | |
realm = null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment