Creating a diff from within a database change listener


#1

From within a change listener on iOS CBL 2.0, I can see how to get the current state of the document but how do I get the previous state so I can get a diff?

[database addChangeListener:^(CBLDatabaseChange * change) { // diff? }];

I’m looking for something similar to what Core Data provides with its NSManagedObjectContextObjectsDidChangeNotification or NSManagedObjectContextWillSaveNotification Does something like that exist in Couchbase lite?


#2

No, CBL 2’s API doesn’t provide access to prior revisions of a document. This is a good feature request, though (paging @priya.rajagopal)


#3

@priya.rajagopal Yes, it would be really helpful. I’d imagine there’s a good percentage of folks coming from Core Data. Just the ability to get notified before and after a document is changed would go a long way. I can send these notifications myself for local changes but I don’t see any way to do this for changes due to a replication. Currently the only workaround I can think of involves saving the document sync state for every single device that’s syncing to the database. Just saving the changes made in the last revision would not be enough since that doesn’t handle the case for a device that didn’t sync every revision.


#4

I don’t think we’d provide notifications before a document change. Those would need to be synchronous to be any use, i.e. they would block the change to the document until they returned, and that would be a performance hit esp. for replication.


#5

That’s true. I’ve got to switch over to thinking in terms of how Couchbase does things. I was still thinking in terms of how things work in Core Data where it makes sense to offer the ability to intercept a change. In the Couchbase world it wouldn’t make sense since any change would require creating another revision anyway. Perhaps Couchbase lite could pass the diff used for replication on through to the application?


#6

In version 1 the public API offered access to the revision history, so you could access the last few revisions; we took that out in 2.0 to simplify the API because it wasn’t used much. But being able to see the parent revision in a notification would be useful (paging PM @priya.rajagopal)


#7

Looks like I’ll have to switch to CBL 1.4.x until I can do something similar in 2.0. I’m definitely feeling both the improvements and omissions in CBL 2.0. I’ve still got a lot to learn about Couchbase. I’ll share my thoughts about CBL 2.0 once I’ve spent more time with Couchbase.