Reputation: 531
I have been looking for resources all day on what the best practices are for implementing realm in react-native.
My 2 problems are this:
First, reading the documentation over and over again, don't understand about initializing vs using a database. I have a few const's and create a schema using their example:
// Initialize a Realm with Car and Person models let realm = new Realm({schema: [CarSchema, PersonSchema]});
Does this get done on every page (Component object) that I use? The const's which create the schema are very large.
I tried to move them to their own file, but keep getting an error trying to import the object returned (a Realm object).
Does everytime the application use a Realm object, must it be redefined each time?
Thanks!
Upvotes: 1
Views: 2156
Reputation: 1578
The new Realm
only has to be done one time in the applications lifetime. The first time it's done it will create the database and define the schema. When your app launches again, it will open this database and check that the schema matches. So it's like "open database".
Once opened, you use the handle (that you called realm
) to reference the database and perform the needed operation with it.
You can see this in the Example on Github.
Upvotes: 5
Reputation: 1447
In RealmExample the Realm is defined in a separate file, and then imported in any file it is used as import realm from './realm'
The code to do this looks like:
import Realm from 'realm';
class Todo {}
Todo.schema = { ... };
class TodoList {}
TodoList.schema = { ... };
export default new Realm({schema: [Todo, TodoList]});
Upvotes: 2