Nozbe / WatermelonDB

🍉 Reactive & asynchronous database for powerful React and React Native apps ⚡️
https://watermelondb.dev
MIT License
10.49k stars 589 forks source link

Observer doesn't listen to changes #225

Closed skhizerabass closed 5 years ago

skhizerabass commented 5 years ago

I have applied the Observer on my table but It doesn't display the change. It only re renders with the old data in the table. I have already checked that the data was successfully updated .

Just to mention if I make changes through the same instance of the DB on which Observer is made . It listens to the changes But if I make any change from another DB instance. That creating a new object of a DB outside the Component.Actually We are using some listeners that receive data from server which i am updating into the db. The data is updated accurately even if i reload the app. The data i see is overwritten but it doesn't listens to the changes. To resolve this I created a static object for DB and tried to use that but unfortunately it is throwing an exception.

Diagnostic error: Record ID subscriptions#S.... was sent over the bridge, but it's not cached

radex commented 5 years ago

@skhizerabass I'm not sure if I understand your problem, but it seems like you're creating multiple Database instances. That is not correct! You ought to have only a single Database, unless you actually want to have completely separate Databases. If you create multiple Database instances, connecting to the same underlying database, you will get a ton of strange bugs.

saadqbal commented 5 years ago

Hey @radex ,

I have created the following class for creating a DBInstance. Since we are doing the insertion and updation outside components as we are suing redux-saga, we cannot use the Database Provider :

export class DBInstance {

  static adapter = new SQLiteAdapter({
    dbName: 'WatermelonDemo1',
    schema: mySchema
  });

  static dbWater = new Database({adapter: DBInstance.adapter,
    modelClasses: [Subscriptions,SubscriptionRoles, Contacts],

  });

  static getCurrentDB() {
      return DBInstance.dbWater;
  }
}

But this does not work as well. Could you please let me know how can we use a single db instance with redux-saga?

radex commented 5 years ago

@saadqbal I don't know redux-saga. I definitely do not recommend having singleton classes in your app, but I don't see a reason why this wouldn't work. You do seem to have only a single Database instance, so all is good. You need to provide more diagnostics about your problem for me to be able to help you

cwagner22 commented 5 years ago

@saadqbal Did you find a solution for how to access the database outside the components? I'm also using redux-saga but I guess it's the same issue with redux-thunk for example...

radex commented 5 years ago

@cwagner22 Database is just an object. It really has nothing to do with React. So you can just use it — it's up to you how you pass it down so that you can access it

saadqbal commented 5 years ago

Hey @cwagner22 yup we solved it by creating a single Water Melon database instance. and Stored it in the Global object.

e.g:


const adapter = new SQLiteAdapter({
    dbName: 'WatermelonDb',
    schema: mySchema
});

const dbWater = new Database({
    adapter,
    modelClasses: [ContactsModel, Messages],
    actionsEnabled: true,

});
global.db= dbWater;

you can now use the global.db anywhere.

radex commented 5 years ago

closing – seems like solved issue. let me know it it's not

cwagner22 commented 5 years ago

Another nice way to do it by simply exporting the database: https://github.com/Nozbe/WatermelonDB/issues/250#issuecomment-464191016