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 instances never close onDestroy #5069

Closed
shidobecker opened this issue Aug 5, 2017 · 10 comments
Closed

Realm instances never close onDestroy #5069

shidobecker opened this issue Aug 5, 2017 · 10 comments
Labels

Comments

@shidobecker
Copy link

Goal

What do you want to achieve?

Close current Realm instance of the current activity when this activity gets destroyed.

Expected Results

I followed Realm documentation on how to manage instances lifecycles but still no success.

Actual Results

Realm instances doesn't close onDestroy method.

Steps & Code to Reproduce

I create my realm = Realm.getDefaultInstance() onCreate of my activities, and I call realm.close onDestroy of each one. (onDestroy Log always returns that realm is opened). But when I start a new activity, the realm.getLocalInstanceCount() variable returns the sum of the previous activity instance with the current one because previous activity realm instance never closes. Eventually it leads to Realm error Too many open files in

Code Sample

**_This is my ApplicationClass:_**

realmConfiguration = new RealmConfiguration.Builder()
                .schemaVersion(BuildConfig.VERSION_CODE)
                .migration(new MigrationUtil())
                .build();
        Realm.setDefaultConfiguration(realmConfiguration);



**_This is my SplashActivity Class:_**

protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.splash_activity);
        realm = Realm.getDefaultInstance();

        **Log.e(TAG, "REALM LOCAL INSTANCE COUNT ON + " + TAG + " : " + String.valueOf(realm.getLocalInstanceCount(AppCoordinator.get(this).getRealmConfiguration())));**

}

   protected void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "ON DESTROY");
        realm.close();
        if(realm.isClosed()){
            Log.e(TAG, "REALM IS CLOSED");
        }else{
            Log.e(TAG, "REALM IS OPENED");
        }

    }

**_This is my FirstActivityClass:_**

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        realm = Realm.getDefaultInstance();

        **Log.e(TAG, "REALM LOCAL INSTANCE COUNT ON + " + TAG + " : " + String.valueOf(realm.getLocalInstanceCount(AppCoordinator.get(this).getRealmConfiguration())));**

}
   @Override
    protected void onDestroy() {
        super.onDestroy();
        selectClassViewModel.destroy();
        AppCoordinator.getBusInstance().unregister(this);
        realm.close();
        if(realm.isClosed()){
            Log.e(TAG, "IS CLOSED");
        }else{
            Log.e(TAG, "IS NOT CLOSED");
        }

    }

Version of Realm and tooling

Realm version(s): 3.3.2

Realm sync feature enabled: no

Android Studio version: 2.4.6 preview 6/7 and 2.3.3

Which Android version and device: From 5.0 to 7.0

@Zhuinden
Copy link
Contributor

Zhuinden commented Aug 6, 2017

is FirstActivity.onDestroy() ever called?

@shidobecker
Copy link
Author

Yeah I always Log it and onDestroy() is called everytime

@zaki50
Copy link
Contributor

zaki50 commented Aug 7, 2017

Isn't FirstActivityClass#onCreate() called before SplashActivity#onDestroy()?

@Zhuinden
Copy link
Contributor

Zhuinden commented Aug 7, 2017

S: onPause()
F: onCreate()
F: onStart()
F: onResume()
S: onSaveInstanceState()
S: onStop()
S: onDestroy()

@shidobecker
Copy link
Author

Sorry for the delay. I should have used more activities as an example.
When I log in, App goes through SplashActivity which opens a Realm instance and it's instance count is 1
If I go to Activity 2 On Create it's instance count is 2.
If I go to another activity from it, Activity 3, it's instance count is 3
So the instance created on SplashActivity remaings even after that activity is completely destroyed. And two new onCreates has been executed (for Activity 2 and 3)
No matter how deep I go through the app, that first instance still remains

@bmeike
Copy link
Contributor

bmeike commented Aug 15, 2017

There are a few things to clear up here.

  • Your code will close the Realm, if onDestroy is called. I don't completely understand what is making you think that it is still open. Your code definitely closes it, when onDestroy is called.
  • If your SplashActivity is still on the backstack, its onDestroy may not be called except in extreme circumstances. Again, I don't completely understand why you think that SplashActivity.onDestroy is being called. I can definitely imagine that it isn't.
  • Related to the above onDestroy is called when Android needs memory and not before. Activity Complete the library for the other columns types [3-4h] #1's onDestroy is only occasionally called before Activity Implement code generation for the other columns types [2-3h] #2's onCreate (rotating the screen is an obvious exception). If your app depends on the ordering of those two lifecycle events, it will break.

Hope some of that helps a bit!

@cmelchior
Copy link
Contributor

You can also try to enable "Don't keep Activities" in the Developer settings. This should definitely kill the activity as soon as it is moved in the background.

@Zhuinden
Copy link
Contributor

Zhuinden commented Aug 16, 2017

@bmeike

Related to the above onDestroy is called when Android needs memory and not before. Activity #1's onDestroy is only occasionally called before Activity #2's onCreate (rotating the screen is an obvious exception). If your app depends on the ordering of those two lifecycle events, it will break.

I don't think this is true.


Anyways, I'd need to see the example project that reproduces this in order to find what's wrong with it, the provided sample is insufficient.

@cmelchior
Copy link
Contributor

What @Zhuinden said. We would need a full working example project to be able to debug this further.

@beeender
Copy link
Contributor

beeender commented Sep 4, 2017

close due to no response.

@beeender beeender closed this as completed Sep 4, 2017
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

8 participants