Couchbase lite document entries converted to generic list from Dictionary on document update

In my Xamarin Forms app I’m creating a database and 2 documents using Couchbase lite. After creating these 2 documents and putting properties into them from the server, there’s no problem in reading that data by using:

var database = Manager.SharedInstance.GetDatabase("indigodb");
var retrievedDocument = database.GetExistingDocument("user");

List<string> companiesList = new List<string>();
IDictionary<string, object> contents = retrievedDocument.Properties;

foreach (KeyValuePair<string, object> entry in contents)
{
      if (entry.Key == "Companies" && entry.Value != null)
       foreach (string item in (entry.Value as JArray).ToObject<string[]>())
                    companiesList.Add(item);
}

entry.Value is stored as a JArray when putting the properties at earlier stage.

The problem arises when it comes to updating the document using:

var userDocument = database.GetExistingDocument("user");
SavedRevision newVersion = userDocument.Update(rev =>
                {
                    var propertiesUser = rev.UserProperties;
                    propertiesUser["Code"] = Globals.User.ApplicantCode;
                    propertiesUser["FirstName"] = Globals.User.FirstName;
                    propertiesUser["LastName"] = Globals.User.LastName;
                    propertiesUser["Companies"] = Globals.User.Companies;
                    rev.SetUserProperties(propertiesUser);
                    return true;
                });

where Globals.User.ApplicantCode, FirstName and LastName are strings and Globals.User.Companies is of type List.

When I’m trying to go through the properties data again using the first code above, entry.Value now becomes of type generic list and not as JArray anymore so that throws an exception.

I’m trying to see what I’m doing wrong in reading the document properties data or in updating the document (or both), as I cannot seem to find any help online. I hope you understand the issue well.

Thanks! :slight_smile:

You’ve hit upon one of the biggest sources of current pain for me. The best advice I can offer at the moment is the have a method that will convert to a generic list if it is not already so that you can be sure of the type you are getting. For 2.0, we are going to put an end to this abstract way and use models to get data serialized into a more understandable format so keep an eye out for developer previews that will come starting around mid next year.