Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Realm will crash when device orientation changes #9

Open
markzyu opened this issue Feb 13, 2016 · 5 comments
Open

Realm will crash when device orientation changes #9

markzyu opened this issue Feb 13, 2016 · 5 comments

Comments

@markzyu
Copy link

markzyu commented Feb 13, 2016

This bug can be reproduced (but not related to changes brought) in commit 114a172, by changing the device orientation while browsing MainActivity.

Caused by: java.lang.IllegalStateException: This Realm instance has already been closed, making it unusable.
   at io.realm.BaseRealm.checkIfValid(BaseRealm.java:344)
   at io.realm.Realm.where(Realm.java:880)
   at com.takefive.ledger.MainActivity.onCreate(MainActivity.java:110)
   at android.app.Activity.performCreate(Activity.java:5990)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420) 
   at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3980) 
   at android.app.ActivityThread.access$1000(ActivityThread.java:154) 
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327) 
   at android.os.Handler.dispatchMessage(Handler.java:102) 
   at android.os.Looper.loop(Looper.java:135) 
   at android.app.ActivityThread.main(ActivityThread.java:5294) 
   at java.lang.reflect.Method.invoke(Native Method) 
   at java.lang.reflect.Method.invoke(Method.java:372) 
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:910) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:705) 
   at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:114)
``` 
@mar29th
Copy link
Member

mar29th commented Feb 15, 2016

Need to better handle view lifecycle events.

@markzyu
Copy link
Author

markzyu commented Feb 15, 2016

I am thinking about using Provider rather than directly injecting (in the
latter way the pointer will not change even if activity has been stopped
and restarted.)

On Sunday, February 14, 2016, lafickens [email protected] wrote:

Need to better handle view lifecycle events.


Reply to this email directly or view it on GitHub
#9 (comment)
.

@mar29th
Copy link
Member

mar29th commented Feb 15, 2016

I concur. Realm instances should always be retrieved from providers and closed in lifecycle events when appropriate.

@markzyu
Copy link
Author

markzyu commented Feb 15, 2016

I just looked it up and actually it was a re-creation rather than
re-starting, and that's why the code in onCreate is called again and
accessed Realm. BTW, when shall we close the Realm connection? Do we create
our Activity base class to do that?

Reference:
http://developer.android.com/training/basics/activity-lifecycle/recreating.html#SaveState

On Sunday, February 14, 2016, lafickens [email protected] wrote:

I concur. Realm instances should always be retrieved from providers and
closed in lifecycle events when appropriate.


Reply to this email directly or view it on GitHub
#9 (comment)
.

@mar29th
Copy link
Member

mar29th commented Feb 15, 2016

If we were to make a base activity, then it only makes sense to use on single realm within a single activity (and casting to the base class type perhaps?) . That can be tricky to manage sometimes. I would suggest calling Realm.close() in onPause and somehow reopen realm in onResume. Currently I only have realm closed in onDestroy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants