Nozbe / WatermelonDB

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

Diagnostic error: callReader/callWriter call must call a reader/writer synchronously #1344

Open ronitpadariya opened 2 years ago

ronitpadariya commented 2 years ago

When I'm going to update inside callWriter, I 'm facing this error. Please someone tell me how could I call update inside callWriter? Here is my Code.

@writer async updateContact(newContact) {
    return await this.callWriter(() => {
      this.update(c => {
        c.lastPresence = newContact.lastPresence;
      });
    });
await database.write(async () => {
    await contact.update((c) => {
         c.lastPresence = 'online';
    });
});
marvanov commented 2 years ago

i have same error, did you found decision?

KrisLau commented 2 years ago

It's because callWriter is only if you are nesting writers like shown here: https://nozbe.github.io/WatermelonDB/Writers.html?highlight=callWriter#advanced-nesting-writers-or-readers

If you are not nesting the writer you should follow this part instead: https://nozbe.github.io/WatermelonDB/Writers.html?highlight=callWriter#writer-methods where as you can see they just call the create/update/delete immediately instead of wrapping in a callWriter.

import { writer } from '@nozbe/watermelondb/decorators'

class Post extends Model {
  // ...

  @writer async addComment(body, author) {
    const newComment = await this.collections.get('comments').create(comment => {
      comment.post.set(this)
      comment.author.set(author)
      comment.body = body
    })
    return newComment
  }
}
ronitpadariya commented 2 years ago

Hello @KrisLau, In my app, randomly coming data from the server. In this case, I'm facing this warning.

The writer you're trying to run (unnamed) can't be performed yet, because there are 3 other readers/writers in the queue. Current writer: unnamed. If everything is working fine, you can safely ignore this message (queueing is working as expected). But if your readers/writers are not running, it's because the current writer is stuck. Remember that if you're calling a reader/writer from another reader/writer, you must use callReader()/callWriter(). See docs for more details.

How can I prevent this warning?

KrisLau commented 2 years ago
If everything is working fine, you can safely ignore this message (queueing is working as expected)

I just ignore it.