Here Couchbase Liteで文書を更新する方法が説明されています。CouchBase Lite - ドキュメントの更新 - Android、なぜ "properties.putAll(..)"が必要ですか?
以下のコードでproperties.putAll(doc.getProperties());
のコメントを外すと、更新が行われないことがわかりました。なぜ?
Document doc = database.getDocument(myDocID);
Map<String, Object> properties = new HashMap<String, Object>();
properties.putAll(doc.getProperties()); // IF I UNCOMMENT THIS LINE, THE UPDATE DOES NOT WORK, WHY ?
properties.put("title", title);
properties.put("notes", notes);
try {
doc.putProperties(properties);
} catch (CouchbaseLiteException e) {
e.printStackTrace();
}
私の推測によれば、それは隠れた性質のためですが、確かにありません。
EDIT:あなたはドキュメントを取得するとき、あなたはデータの不変のバージョンが含まれているコピーを取得
static public void storeDoc(Database db, String key, Map<String, Object> p){
// Save the document to the database
Document document = db.getDocument(key);
Map<String, Object> p1 = new HashMap<>();
Map<String, Object> oldprops=document.getProperties();
if (oldprops!=null) p1.putAll(oldprops); //if I uncomment this line then the update does not work
for (Map.Entry<String, Object > e:p.entrySet()) {
p1.put(e.getKey(),e.getValue());
}
try {
document.putProperties(p1);
} catch (CouchbaseLiteException e) {
e.printStackTrace();
}
}
申し訳ありませんが、最初の答えは間違ったコードを使用していました。 CBLのどのバージョンを使用していますか?これは1.2.1で私のために働いています。 – Hod
「うまくいかない」とはどういう意味ですか?例外はありますか?データは単に更新されませんか? – borrrden
うん。更新されていない。 – jhegedus