ios - CBLLiveQuery 与 kCBLDocumentChangeNotification

标签 ios objective-c nsnotificationcenter key-value-observing couchbase-lite

目前我正在开发 Couchbase Lite,并在 UITableView 中显示每个文档。

我的问题是,如果 document_id : abc12345 在服务器端 (CouchDB)(手动或从任何其他 iOS/Android/Web 应用) 更新,这是更新 UITableView 中的 document _id : abc12345 的更好方法。

在目前的情况下,我正在使用 CBLLiveQuery,但我对此并不满意,因为它需要一个 CBLView (map/reduced 函数,我正在根据 对其进行索引_rev of CBLDocument),然后创建 CBLQuery 然后调用 livequery [livequery start]; 然后观察KVO 然后等等等等......

self.liveQuery = [self startLiveQueryViewWithDatabase:database]; [self.liveQuery addObserver:self forKeyPath:@"rows"options:0 context:NULL]; [self.liveQuery 开始];

我观察到的事实是,每当实时查询实例被第一次调用时,KVO 方法-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context 在服务器上的 CBLDocument 没有任何变化的情况下被调用。

当服务器上的任何文档更新 -(void)observeValueForKeyPath: 时,不会告诉我发生了什么变化/哪个文档发生了变化 _id,它只会给我一个一堆文档 ID _id

当我了解到 kCBLDocumentChangeNotification 时,它为我提供了已更新的正确文档 ID _id

[[NSNotificationCenter defaultCenter] addObserverForName:kCBLDocumentChangeNotification object:self queue:nil usingBlock:^(NSNotification *note) { CBLDatabaseChange* changes = note.userInfo[@"change"]; NSLog(@"文档:[%@]",changes.documentID); [ self 更新用户界面:变化]; }];

请问我需要知道哪个概念更适合实现其中任何一个,哪个将花费更少的时间(如果技术上可能的话)。

最佳答案

查询是从 View 的索引中查找结果的操作。现在, View 可以索引数据库中的任何内容。它与查询一起可以检索所有文档、一组文档、一堆属性或来自文档的单个属性或一些基于文档中的属性计算的值。

而且,liveQuery 是一种观察 View 索引更新的机制,即通知 View 的 emit block 的更改。

kCBLDocumentChangeNotification 只是通知特定文档发生的更改,即通知新修订。

/** This notification is posted by a CBLDocument in response to a change, i.e. a new revision.
    The notification's userInfo contains a "change" property whose value is a CBLDatabaseChange
    containing details of the change.
    NOTE: This is *not* a way to detect changes to all documents. Only already-existing CBLDocument
    objects will post this notification, so when a document changes in the database but there is
    not currently any CBLDocument instance representing it, no notification will be posted.
    If you want to observe all document changes in a database, use kCBLDatabaseChangeNotification.*/
extern NSString* const kCBLDocumentChangeNotification;

所以,
- 使用 liveQuery 接收查询更新。
- 使用 kCBLDocumentChangeNotification 从文档更新。
- 使用 kCBLDatabaseChangeNotification 更新所有文档。

关于ios - CBLLiveQuery 与 kCBLDocumentChangeNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33100649/

相关文章:

ios - 使用 Swift 3 的 DynamoDB 中的保留关键字 ExpressionAttributeValues

ios NSNotification 在处理程序中未收到

ios - 从 URL 方案打开完全退出的应用程序未从 iOS 中的 postNotificationName 调用该函数

ios - Facebook iOS iOS SDK 3.1 iOS 5.1 XCode 4.5

ios - 为什么在通用函数中检查可选的 UIView 参数时会出错?

ios - 如何增加 UITableView 中自定义单元格的高度

ios - 带索引的 NSArray 递归搜索

ios - 滚动 UICollectionView 以结束加载 viewController

objective-c - objc 到 swift 的转换

iOS8 + UIDeviceOrientationDidChangeNotification 没有被调用