Closed skhizerabass closed 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.
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?
@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
@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...
@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
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.
closing – seems like solved issue. let me know it it's not
Another nice way to do it by simply exporting the database: https://github.com/Nozbe/WatermelonDB/issues/250#issuecomment-464191016
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.