我一直在跨平台(Xamarin)应用项目中使用Couchbase Lite作为本地数据存储区。我一直在从Amazon Web服务后端检索对象,使用Newtonsoft JSON进行序列化。自从几周前我问过this问题的答案后,我的对象中的所有内容都会正确序列化。但是,将我的食谱的“组”属性添加到couchbase lite中的文档是不成功的。保存文档时不会抛出异常,但是当从数据库中检索文档时,“IngredientsWithHeaders”和“InstructionsWithHeaders”都返回null。以下是保存/检索代码:
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;
}
答案 0 :(得分:0)
将ToRecipe()
方法更改为以下解决了问题:
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;
}