我尝试创建 CBLModel 对象并将其保存在 couchbase lite 中。文档保存时没有错误,但是从数据库中检索文档时,文档的属性始终为零。
// 模型
class TestModelUser: CBLModel {
var userName:NSString?
}
// 创建并保存
let userModel:TestModelUser = TestModelUser(newDocumentInDatabase: db)
userModel.userName = "test user name 10"
userModel.setValue(NSStringFromClass(TestModelUser), ofProperty: "type")
let saved = userModel.save(&error)
if saved {
println("saved")
}else{
if let err:NSError = error {
println(err.localizedDescription)
}
}
// 取回
let dbView:CBLView = db.viewNamed("userView")
dbView.setMapBlock({ (document, emit) -> Void in
emit(document["_id"], document)
if let docType = document["type"] as? String {
if docType == NSStringFromClass(TestModelUser) {
emit(document["_id"], document)
}
}
}, version: "5")
let queryResult1:CBLQueryEnumerator = db.viewNamed("userView").createQuery().run(&error)
for row:CBLQueryRow in queryResult1.allObjects as [CBLQueryRow] {
let user:TestModelUser = TestModelUser(forDocument: row.document)
if let uName = user.userName {
println(uName)
}
}
并且 user.userName 始终为零,也许还有另一种保存文档的方法?