private void SetupViewAndQuery() { var view = _db.GetView("list/listsByName"); view.SetMap((doc, emit) => { if (!doc.ContainsKey("type") || doc["type"] as string != "task-list" || !doc.ContainsKey("name")) { return; } emit(doc["name"], null); }, "1.0"); _byNameQuery = view.CreateQuery().ToLiveQuery(); _byNameQuery.Changed += (sender, args) => { TasksList.Replace(args.Rows.Select(x => new TaskListCellModel(x.DocumentId, x.Key as string))); }; _byNameQuery.Start(); var incompleteTasksView = _db.GetView("list/incompleteTasksCount"); incompleteTasksView.SetMapReduce((doc, emit) => { if (!doc.ContainsKey("type") || doc["type"] as string != "task") { return; } if (!doc.ContainsKey("taskList")) { return; } var list = JsonUtility.ConvertToNetObject <IDictionary <string, object> >(doc["taskList"]); if (!list.ContainsKey("id") || (doc.ContainsKey("complete") && (bool)doc["complete"])) { return; } emit(list["id"], null); }, BuiltinReduceFunctions.Count, "1.0"); _incompleteQuery = incompleteTasksView.CreateQuery().ToLiveQuery(); _incompleteQuery.GroupLevel = 1; _incompleteQuery.Changed += (sender, e) => { var newItems = TasksList.ToList(); foreach (var row in e.Rows) { var item = newItems.FirstOrDefault(x => x.DocumentID == row.Key as string); if (item != null) { item.IncompleteCount = (int)row.Value; } } TasksList.Replace(newItems); }; _incompleteQuery.Start(); }