Last active
September 4, 2016 16:56
-
-
Save Zhuinden/57e18a5cfc352f9d0dbabbd9030da471 to your computer and use it in GitHub Desktop.
Realm Singleton instance for UI thread from Book 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
public class BooksScopeListener extends Fragment { // could go to base class | |
public BooksScopeListener() { | |
setRetainInstance(true); | |
RealmManager.incrementCount(); | |
} | |
@Override | |
public void onDestroy() { | |
RealmManager.decrementCount(); | |
super.onDestroy(); | |
} | |
} | |
public class BooksActivity extends AppCompatActivity { | |
Realm realm; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
RealmManager.initializeRealmConfig(getApplicationContext()); // could go to base class | |
super.onCreate(savedInstanceState); | |
BooksScopeListener fragment = (BooksScopeListener) getSupportFragmentManager().findFragmentByTag("SCOPE_LISTENER"); // could go to base class | |
if(fragment == null) { | |
fragment = new BooksScopeListener(); | |
getSupportFragmentManager().beginTransaction().add(fragment, "SCOPE_LISTENER").commit(); | |
} | |
//get realm instance | |
realm = RealmManager.getRealm(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment