2

I'm getting started with PouchDB and I'm using the BulkDocs function, this is used to insert bulk data in the database, documented here: https://pouchdb.com/guides/bulk-operations.html.

Works pretty well, but, as the documentation itself says, it is the same as calling a series of chained put() functions.

The problem with this is that I'm also using couchdb's changes function to listen for changes in the database and update the UI, with this:

db.changes({
      since: 'now',
      live: true,
  }).on('change', refreshCarList);

When I call the bulk update, it triggers the 'change' event multiple times (once for every updated document) and depending on the bulk size, hundreds or even thousands of times at every save. This results on calling the callback multiple times, freezing my UI.

My question is: is there a way/option/hack to make bulk update call changes just once?

sigmaxf
  • 7,998
  • 15
  • 65
  • 125
  • [This](http://stackoverflow.com/questions/12713564/function-in-javascript-that-can-be-called-only-once) might be helpful. – Difster Apr 14 '17 at 06:33
  • A `bulkDocs` operation updates multiple docs. A changes response is for a single document. How would you expect a single change event for multiple updates to be handled? – Jonathan Hall Apr 14 '17 at 19:30
  • I would expect the changes not respond to a single document, but to a single query. – sigmaxf Apr 15 '17 at 05:08
  • @raphadko: That doesn't really answer my question, though. What would you want a `change` event for multiple documents to look like? (And how would you handle multiple event types--such as some documents successfully synced, and others with errors) There's no format for that within PouchDB or CouchDB. It seems you're asking to change the 'changes' feed format in a fundamental way. Or am I missing something? – Jonathan Hall Apr 15 '17 at 16:37
  • Honestly I don't know how a solution would look like, I'm pointing potential performance issues when using both changes and bulkDocs, specially when changes update the UI. As @Jason Jarrett pointed out, maybe turning off the listener and then single updating afterwards could solve this, a bit hackish, but gets the job done. – sigmaxf Apr 15 '17 at 19:34

1 Answers1

3

You might consider turning off the listener before calling the bulk operation, or setting a flag before (that the listener uses to ignore) until the build operation is complete, and then call it once at the end.

Jason Jarrett
  • 3,857
  • 1
  • 27
  • 28