0

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();
Marklar
  • 1,247
  • 4
  • 25
  • 48
  • Your code looks fine to me. I assume you have tried logging the result from `responsePromise.json()` to make sure you are actually calling `realm.create()`? – Kristian Dupont Mar 15 '17 at 09:43
  • I've stepped through the code to make sure that happens. I should have added that the data is created/stored correctly (not in default.realm but in memory???) that I can get it with `realm.objects('Car')`. I just searched my computer and there are no other realm files (besides `sync_metadata.realm`). It's even persisted if I stop and start my iOS simulator. Just to reiterate, the `default.realm` is not updated after all of this. I'm not sure where the data is being stored before it's eventually lost. – Marklar Mar 15 '17 at 10:22
  • With Realm there is no such thing as "in memory" -- every write is immediately persisted to disk when the transaction completes, so it does seem like you are looking at the wrong file. Are you sure you are locating the right file? Take a look at [this answer](http://stackoverflow.com/questions/28465706/how-to-find-my-realm-file) for more information about where to find it. – Kristian Dupont Mar 15 '17 at 10:36
  • Thank you! You are correct. I used `realm.path` and found another `default.realm` in a path that included `/Library/Developer/CoreSimulator/Devices/`. I'm not sure why OSX finder wasn't able to find this file. Do you know why a `default.realm` was also created in my React Native directory? It's an old version of the schema, so maybe it was created when I added a schema version? Am I able to delete this old version? – Marklar Mar 16 '17 at 00:24
  • Hm, I can't think of any reason why such a file would exist. Did you perhaps run unit tests with Jest or something? – Kristian Dupont Mar 16 '17 at 08:17
  • No, at least not intentionally. I'll delete it and see if it gets recreated at all. – Marklar Mar 16 '17 at 22:14
  • Have you found out why realm writes do not persist? I am facing the same problem but on Android and RN 0.47 – SoundStage Sep 05 '17 at 17:05
  • @Rai I just posted an answer to recap my solution from the comments above – Marklar Sep 06 '17 at 23:06

1 Answers1

0

As Kristian Dupont pointed out in the comments above, I was looking at the wrong file.

Following this answer I used realm.path in the Chrome debugger console and found the correct default.realm in a path that included /Library/Developer/CoreSimulator/Devices/. I'm not sure why OSX finder wasn't able to find this file. I'm unsure why a default.realm file was also created in my React Native directory but I have since deleted this file to avoid confusion and have had no further issues.

Marklar
  • 1,247
  • 4
  • 25
  • 48