2015-09-02 13 views
1

Я использую Couchbase Lite в проекте кросс-платформенных приложений (Xamarin) в качестве локального хранилища данных. Я извлекал объекты с бэкэнда веб-сервиса Amazon, сериализованный с использованием Newtonsoft JSON. Все в моем объекте сериализуется правильно с момента ответа на вопрос this, который я задал пару недель назад. Однако добавление свойств «группы» моих рецептов в документ в couchbase lite не увенчалось успехом. Никакие исключения не бросаются на сохранение документа, но «IngredientsWithHeaders» и «InstructionsWithHeaders» возвращают null, когда документ извлекается из базы данных. Ниже сохранения/извлечения кода:Couchbase Lite Doc отсутствующие свойства

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

Это звучит так же, как проблему сериализации вы упомянули в вашем последнем вопросе. Какой подход вы предприняли для его решения? Я бы предложил использовать 'Словарь' вместо' Group' – borrrden

+0

Я действительно считал это, но, похоже, он корректно сериализуется в базе данных couchbase lite. По какой-то причине он просто не десериализуется в желаемый тип. –

ответ

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; 
} 
Смежные вопросы