question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is Realm close automatically in method in which it was created?

See original GitHub issue

Realm 4.1. My activity:

public class MyActivity extends AppCompatActivity {
private Realm realm;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle extras = getIntent().getExtras();
        Produt product = null;
        if (extras != null) {
            product = extras.getInt(Product.PRODUCT);
        }
        setContentView(R.layout.shopping_details);
        realm = Realm.getDefaultInstance();
        imagesList = realm.copyFromRealm(product.getGallery());

        init(product);
}

  private void init(Product product) {
  // some stuff here
  }

   @Override
    public void onDestroy() {
        super.onDestroy();
        realm.close();
    }

OK. It’s work. As you can see I create Relam in onCreate() method and close in onDestroy().

But I need realm object ONLY in method onCreate(). I need to call method realm.copyFromRealm(...) only in `onCreate()

`So do the next change:

  1. removeonDestroy()method
  2. Remove declaration of variable: private Realm realm;
  3. Change onCreate() method

Here result:


@Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle extras = getIntent().getExtras();
        Produt product = null;
        if (extras != null) {
            product = extras.getInt(Product.PRODUCT);
        }
        setContentView(R.layout.shopping_details);
        Realm realm = Realm.getDefaultInstance();
        imagesList = realm.copyFromRealm(product.getGallery());

        init(product);
}

  private void init(Product product) {
  // some stuff here
}


So as result I have:

  1. I create Realm object only in specific method: onCreate()
  2. After method onCreate() finish the realm object will be destroy automatically
  3. In method init() the Realm object not exist
  4. I don’t need anymore to close Realm in method onDestroy()

Is I’m right?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Zhuindencommented, Nov 5, 2017

Yeah in that case your above code will work as you expect

0reactions
cmelchiorcommented, Nov 6, 2017

@Imdic It sounds like your question was answered. If you need more information you can also read https://realm.io/docs/java/latest/#closing-realms and https://realm.io/docs/java/latest/#realm-instance-lifecycle

Read more comments on GitHub >

github_iconTop Results From Across the Web

Open & Close a Realm - Java SDK - MongoDB
Create a configuration for the realm you want to open. Open the realm using the config. Close the realm to free up resources...
Read more >
When should I call realm.close()? - react native - Stack Overflow
a Realm instance is created/obtained when the application is visible; the instance is shared by all operations executed on the main thread ( ......
Read more >
Realm Auto-Updated Objects: What you need to know - Medium
Realm open and close methods will end up throughout your multithreaded project. They will appear inside your activity's and fragment's ...
Read more >
Create reactive mobile apps in a fraction of the time - Realm
RealmObject s are live, auto-updating views into the underlying data, which means objects never have to be refreshed. Modifying objects that affect the ......
Read more >
Realistic Realm - lessons learned after using it for 1.5 years
Call the Realm method and lock the thread until we get the object or null. ... Observable with an automatically closed realm at...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found