I am using realm.write
however my default.realm
file is not being updated. I have also added another object to my schema and I can use this whilst developing my React Native app, but the default.realm
file is not updated to include this new object.
I can confirm it is not being saved by opening default.realm
in Realm Browser. Also, after turning my mac on this morning (after shutting down last night) and running my React Native app then data was not in my Realm when I tried to access it.
Example code:
#queries.js
import Realm from 'realm'
// Define your models and their properties
class Car {}
Car.schema = {
name: 'Car',
primaryKey: 'id',
properties: {
id: 'int',
make: 'string',
model: {type: 'string', optional: true}
}
}
class Person {}
Person.schema = {
name: 'Person',
properties: {
id: 'int',
name: 'string'
}
}
// Get the default Realm with support for our objects
let realm = new Realm({schema: [Car, Person], schemaVersion: 3});
class Queries {
async syncCars()
{
try {
let responsePromise = await fetch(`http://api.localhost.com:3000/v1/cars?auth_token=xxx`).catch(function(error) {
console.log('There has been a problem with your fetch operation: ' + error.message);
});
let responseJson = await responsePromise.json();
realm.write(() => {
responseJson.forEach((values) => {
realm.create('Car', values, true);
});
});
}
catch(error) {
console.log("error " + error);
}
}
}
export default new Queries
and then in my component code I have:
import Queries from './queries'
await Queries.syncCars();