Is Realm close automatically in method in which it was created?
See original GitHub issueRealm 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:
- remove
onDestroy()
method - Remove declaration of variable:
private Realm realm;
- 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:
- I create Realm object only in specific method:
onCreate()
- After method
onCreate()
finish the realm object will be destroy automatically - In method
init()
the Realm object not exist - I don’t need anymore to close Realm in method
onDestroy()
Is I’m right?
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Yeah in that case your above code will work as you expect
@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