1

目前我正在使用Couchbase Lite,並在UITableView中顯示每個文檔。CBLLiveQuery vs kCBLDocumentChangeNotification

我的問題是,如果一個DOCUMENT_ID:ABC12345是在服務器端(CouchDB的)(手動或任何其他應用的iOS/Android設備/網絡),這是更好的方法來更新文檔_id更新:abc12345 in UITableView

在目前的情況下我使用CBLLiveQuery,我不喜歡它,因爲它需要一個CBLView (圖/縮小功能,而我基於對CBLDocument_rev建立索引),並創建CBLQuery,然後調用的liveQuery [livequery start];,然後用KVO觀察,然後等等等等......

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

,我觀察到的事實是,每當實況查詢實例調用首次 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(@"Document : [%@]",changes.documentID); [self updateUserInterface:changes]; }];

請我需要知道哪些概念是更好地實現其中任意一個,其中一個將花費更少的時間(如果可能的話技術上)。

回答

2

查詢是查看視圖索引結果的操作。現在,視圖可以索引數據庫中的任何內容。它與查詢一起可以檢索文檔中的所有文檔,一組文檔,一堆屬性或單個屬性或者根據文檔中的屬性來計算一些值。

而且,liveQuery是一種觀察視圖的這些索引的更新的機制,即通知視圖的發射塊的更改。

A 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更新所有文檔。

+0

謝謝,在'liveQuery'中,KVO只需調用' - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context'即使沒有任何在服務器上更改文檔。這不需要在棧中推入一個方法。而'kCBLDocumentChangeNotification'不會對方法進行不必要的調用。我知道'kCBLDocumentChangeNotification'比'liveQuery'慢,但延遲可以忽略不計。任何進一步的澄清將受到關注。 – iNasir

+0

正如我所說,在liveQuery中,您正在觀察「行」,並且每次有新值時都會收到通知。 –

相關問題