Reputation: 69
I've been using Couchbase Lite in a cross-platform (Xamarin) app project as a local datastore. I have been retrieving objects from an Amazon Web-service backend, serialized using Newtonsoft JSON. Everything in my object serializes correctly since the answer to this question I asked a couple weeks ago. However, adding the "group" properties of my recipes to a document in couchbase lite has been unsuccessful. No exceptions are thrown on saving the doc, but the "IngredientsWithHeaders" and "InstructionsWithHeaders" both return null when the doc is retrieved from the database. The following is save/retrieval code:
public void SaveRecipe (Recipe recipe)
{
var document = db.GetDocument (recipe.RecipeId);
var properties = new Dictionary<string, object> (){
{ "UserId",recipe.UserId },
{ "Title",recipe.Title },
{ "IngredientsList",recipe.IngredientsList },
{ "Notes",recipe.Notes },
{ "Tags",recipe.Tags },
{ "Yield",recipe.Yield },
{ "IngredientsWithHeaders",recipe.IngredientsWithHeaders },
{ "InstructionsWithHeaders",recipe.InstructionsWithHeaders }
};
var rev = document.PutProperties(properties);
Debug.Assert (rev != null, "Revision is null");
var newRev = document.CurrentRevision.CreateRevision ();
newRev.SetAttachment ("image.jpg", "image/jpeg", recipe.Image);
newRev.Save ();
}
public IEnumerable<Recipe> GetRecipes ()
{
var query = db.CreateAllDocumentsQuery ();
query.AllDocsMode = AllDocsMode.AllDocs;
var rows = query.Run ();
Debug.WriteLine ("Recipes in Query: "+rows.Count);
return rows.Select (recipe => ToRecipe (recipe.Document));
}
private Recipe ToRecipe(Document doc){
Recipe recipe = new Recipe(doc.Id);
recipe.UserId = doc.GetProperty<string> ("UserId");
recipe.Title = doc.GetProperty<string> ("Title");
recipe.IngredientsList = doc.GetProperty<List<string>> ("IngredientsList");
recipe.Notes = doc.GetProperty<List<string>> ("Notes");
recipe.Tags = doc.GetProperty<ISet<string>> ("Tags");
recipe.Yield = doc.GetProperty<int> ("Yield");
recipe.IngredientsWithHeaders = doc.GetProperty<List<Group<string,IngredientJson>>> ("IngredientsWithHeaders");
recipe.InstructionsWithHeaders = doc.GetProperty<List<Group<string,string>>> ("InstructionsWithHeaders");
var rev = doc.CurrentRevision;
var image = rev.GetAttachment ("image.jpg");
if (image != null) {
Debug.WriteLine ("There is an image here");
recipe.Image = image.Content.ToArray();
}
return recipe;
}
Upvotes: 1
Views: 331
Reputation: 69
Changing the ToRecipe()
method to the following solved the problem:
private Recipe ToRecipe(Document doc){
Recipe recipe = new Recipe(doc.Id);
recipe.UserId = doc.GetProperty<string> ("UserId");
recipe.Title = doc.GetProperty<string> ("Title");
recipe.IngredientsList = doc.GetProperty<List<string>> ("IngredientsList");
recipe.Notes = doc.GetProperty<List<string>> ("Notes");
recipe.Tags = doc.GetProperty<ISet<string>> ("Tags");
recipe.Yield = doc.GetProperty<int> ("Yield");
recipe.IngredientsWithHeaders = JsonConvert.DeserializeObject<List<Group<string, IngredientJson>>>(doc.GetProperty ("IngredientsWithHeaders").ToString());
recipe.InstructionsWithHeaders = JsonConvert.DeserializeObject<List<Group<string, string>>>(doc.GetProperty("InstructionsWithHeaders").ToString());
var rev = doc.CurrentRevision;
var image = rev.GetAttachment ("image.jpg");
if (image != null) {
Debug.WriteLine ("There is an image here");
recipe.Image = image.Content.ToArray();
}
return recipe;
}
Upvotes: 0