Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I call realm.close()?

I'm using Realm for my React Native app. I'm making Realm instances available to my components like this, as shown in the official example app:

export default new Realm({schema: [Todo, TodoList]});.

When I subsequently ran tests with Jest, I realized that the process didn't finish as long as I wouldn't call

afterAll(() => { realm.close(); });

at the end of the test suite.

This made me think about if and when I should call realm.close() in the production code. What are the consequences of not calling close? If recommended, what would be the best way to close Realm instances?

like image 419
Johannes Avatar asked Nov 14 '16 11:11

Johannes


1 Answers

realm.close() is used when you are done with your current schema. In Realm api page it says the following:

close(): Closes this Realm so it may be re-opened with a newer schema version. All objects and collections from this Realm are no longer valid after calling this method.

If you don't want to change your schema, you don't have to worry about close method.

Full reference here: Realm close method.

like image 183
leo7r Avatar answered Oct 19 '22 03:10

leo7r