TL;DR
What is the recipe or best practice to save a large collection of MutableDocuments to a database?
Back Story
Our code makes an API call to fetch about 11,000 model classes from an API call. We then persist these into a local CBL database, and we are NOT (yet) using a sync gateway. So everything is on the device, and we’re just using CBL as our local database of choice, with the goal of setting up a sync gateway in the future.
The model class has a Couchbase MutableDocument
property, which is instantiated by the particular model constructor invoked by the API consumer code, since our model is immutable. I say this so you know that by the time we pass the 11,000 models to our CBL database, the MutableDocument for each model already exists, and the only work to do is to invoke Database.Save(myMutableDocument)
for each MutableDocument.
The model class has 23 attributes, most of which are simple value types; only a few are collections which contain fewer than 5 items each.
I’ve done a lot of A/B testing in the code, with and without Database.InBatch
, with and without PLINQ operators, but the gist of my code is this:
myMutableDocuments.AsParallel().ForAll(mutableDocument => database.Save(mutableDocument))
Also, we’re doing this in the background, on a brand new, empty database. Only after this temporary database is built do we swap it with the existing database file so that our repository classes can see it.
The 20 seconds is on older devices like iPhone 6, but even on newer devices it’s like 10-15 seconds. That just can’t be right…