Android 上的 Couchbase lite 未保留文档
Couchbase lite on Android not persisting document
我是 Couchbase lite 的新手。添加文档后,我可以用准确的数据取回文档。但是在删除数据库,重新创建它,然后创建一个新文档时,我仍然得到旧文档。
在调试时,我发现在写入时,它对 'store' 执行此操作,而在读取时,它从 'docCache'.
读取它
我遵循的步骤是:
1) 第一次写:
Document doc = database.getDocument(DOC_ID);
doc.putProperties(map);
2) 第一次阅读:
Document doc = database.getDocument(DOC_ID);
Map<String, Object> map = doc.getProperties();
3) 删除数据库并重新创建它:这是我发现删除所有文档的唯一方法
if (database != null) {
database.delete();
database = null;
}
DatabaseOptions options = new DatabaseOptions();
options.setCreate(create);
database = manager.openDatabase(dbName, options);
4) 我再次执行步骤 1 和 2。
现在,我得到的是旧文档的数据,而不是我在步骤 4 中添加的新数据。我也尝试在删除文档后获取文档,但没有得到任何东西。
有什么我遗漏的吗?
Delete database and re-create it: This is the only way I found to
delete all the documents
删除所有文档的一种方法是创建一个包含所有文档的 View。然后迭代View的Query结果,对你迭代的每个文档执行一次删除:
final View allDocumentsView = couchDatabase.getView("allDocumentsView");
if (allDocumentsView.getMap() == null) {
allDocumentsView.setMap(
new Mapper() {
@Override
public void map(Map<String, Object> document, Emitter emitter) {
if (doc != null) {
emitter.emit((String) document.get("_id"), document);
}
}
}, "1"
);
}
final Query allDocumentsQuery = allDocumentsView.createQuery();
try {
for (Iterator<QueryRow> iterator = allDocumentsQuery.run(); iterator.hasNext();) {
iterator.next().getDocument().delete();
}
} catch (Exception e) {
Log.e("Error", "Error deleting document.", e);
}
您在评论中提到您正在使用两个数据库句柄。如果您不使用相同的 Manager
来获取句柄,那么这是一个大问题。如果是,那么两个数据库应该是相同的引用,当您删除它时,当您尝试访问另一个时会出现错误。如果你不是,那么一旦多个线程开始访问它们,你就可以很容易地破坏你的数据。
我是 Couchbase lite 的新手。添加文档后,我可以用准确的数据取回文档。但是在删除数据库,重新创建它,然后创建一个新文档时,我仍然得到旧文档。
在调试时,我发现在写入时,它对 'store' 执行此操作,而在读取时,它从 'docCache'.
读取它我遵循的步骤是:
1) 第一次写:
Document doc = database.getDocument(DOC_ID);
doc.putProperties(map);
2) 第一次阅读:
Document doc = database.getDocument(DOC_ID);
Map<String, Object> map = doc.getProperties();
3) 删除数据库并重新创建它:这是我发现删除所有文档的唯一方法
if (database != null) {
database.delete();
database = null;
}
DatabaseOptions options = new DatabaseOptions();
options.setCreate(create);
database = manager.openDatabase(dbName, options);
4) 我再次执行步骤 1 和 2。
现在,我得到的是旧文档的数据,而不是我在步骤 4 中添加的新数据。我也尝试在删除文档后获取文档,但没有得到任何东西。
有什么我遗漏的吗?
Delete database and re-create it: This is the only way I found to delete all the documents
删除所有文档的一种方法是创建一个包含所有文档的 View。然后迭代View的Query结果,对你迭代的每个文档执行一次删除:
final View allDocumentsView = couchDatabase.getView("allDocumentsView");
if (allDocumentsView.getMap() == null) {
allDocumentsView.setMap(
new Mapper() {
@Override
public void map(Map<String, Object> document, Emitter emitter) {
if (doc != null) {
emitter.emit((String) document.get("_id"), document);
}
}
}, "1"
);
}
final Query allDocumentsQuery = allDocumentsView.createQuery();
try {
for (Iterator<QueryRow> iterator = allDocumentsQuery.run(); iterator.hasNext();) {
iterator.next().getDocument().delete();
}
} catch (Exception e) {
Log.e("Error", "Error deleting document.", e);
}
您在评论中提到您正在使用两个数据库句柄。如果您不使用相同的 Manager
来获取句柄,那么这是一个大问题。如果是,那么两个数据库应该是相同的引用,当您删除它时,当您尝试访问另一个时会出现错误。如果你不是,那么一旦多个线程开始访问它们,你就可以很容易地破坏你的数据。