Tried to get data immediately from Realm DB after writing into DB using Redux via dispatching Action
Data on the screen update from Realm DB immediately after insert / update / delete
When I 'read' Realm DB and then handle previously read information it goes synchronously and next code is runned step by step.
According to documentation 'write' method takes much time so it must be called synchronously.
But when I put 'write' some data and run next some code (redux dispatch to update Store Data) it's not up to date.
Only Notifications via 'addListener' works, but those updates are often slowly even in NOT Debugger Mode.
So if 'write' is handle
...
const {updateScreenData} = this.props;
...
realm.write(()=>{
...some update/insert...
});
updateScreenData(); // call Redux Action
Is Realm JS 'write' method a/synchronously?
'Cause I can't understand those things above
wrap this in a Promise:
function write(data) {
return new Promise(resolve => {
realm.write(() => {
const new = realm.create('Model', data);
resolve(new);
});
})
}
And have fun:
write(data).then(new => {
callReduxAction();
});
Even more fun with async/await:
async function doit(data) {
const new = await write(data);
callReduxAction();
}
Realm.write executes synchronously, so it seems entirely pointless to wrap it in a promise rather than just executing code after it returns. It may even be harmful, since you're resolving the promise within the write transaction, while you almost certainly want subsequent code to run after the transaction has been committed.
@bdash So I was plain wrong. That's good to know anyway. With async/await the subsquent statements will be run though. What doesn't make sense for me is why operations are synchronous in realm. The only asynchronous call is the Realm.open.
Resolving the promise within a write transaction will likely violate ACID principles.
Most helpful comment
Realm.writeexecutes synchronously, so it seems entirely pointless to wrap it in a promise rather than just executing code after it returns. It may even be harmful, since you're resolving the promise within the write transaction, while you almost certainly want subsequent code to run after the transaction has been committed.