Sync Gateway - Locally delete and remove user from channel

Hello folks,

I hope i picked the right subforum for my question. I have checked the already existing topics and documentation, but couldn’t find a solution for my problem.

What I need:
I want to store data on a CouchBase Server, for example events, which will be continuously extended.

The Server data will be synced to mobile clients. Events that a user has already seen should be removed from the local Couchbase Lite Database, but not from the Server.
The user should also be removed from the events channel.

What would be the best way to achieve this? The only thing I can currently think of would be keeping a list with all seen documents, but that doesn’t seem like a good solution for me…

Thanks in advance

It looks like in your case you want mobile users to only have read access to documents stored on the server.

The difficulty here is that once a user has “seen” the event on the device and delete it, you don’t want to pull it again from the server.

So I think you would need to have a seen property on your documents to add/remove them to the according channels in the sync function when the property is true/false.

Would users first authenticate with Sync Gateway and then get the events accordingly or would it be using the GUEST account mode?

1 Like

The client can “purge” a document, which removes all traces of it locally but doesn’t propagate a deletion back to the server. It’s basically like “forgetting” the document.

1 Like

Thanks for your replies.

The Users would authenticate with the Gateway and get assigned to their channels via the sync function.

Thank you, I will look up on this and report back to you here.

To confirm, on iOS, the

database.deleteLocalDocumentWithID(<#localDocID: String#>, error: <#NSErrorPointer#>)

function is this purge function that you referred to that will delete documents locally but not propagate these deletions back up to the server?

No, that is for local documents which are separate entities. The API call is actually called “purge”.

Hi Jens,
Once I purge a document from local database, Is there any way I read the same doc from server again if required?

Unfortunately there’s not a straightforward way of doing that currently, because Sync Gateway is still missing support for the protocol feature to replicate a set of named documents. (Please comment in the issue if this is an important feature for you.)

Thanks for replying.

The thing is even if sync gateway will support fetching data using doc_id it wont support my use case.

My use case:
There are multiple events on server created by different photographers. And the event viewers (any Guest) can subscribe to multiple event channel to get the event photos, videos and other details. I want to give them functionality to unsubscribe the event to reduce the data usage on local iphone storage. so I want to remove all docs for that event if user unsubscribes (I can do this by purging all of them) but in future if the same user wants to subscribe that event again then how will CBLReplication will replicate those docs from server again? At this time I would not have any data related to that event to fetch from sync gateway other than channel name.

That might be easier, actually, since you want to restore an entire channel.

What I think you can do is to create a pull replication filtered to just that channel (set the channels property.) That will download all the docs from that channel. You may need to set the reset property to true in the customProperties property, to make sure that it starts fresh and gets all the docs.

OMG! thanks a lot… actually I was searching for reset word on CBLReplication header file but never looked at custom properties.
btw I am a big fan of you n your work!

How to set the “reset” property in couchbase mobile 2.0?

It would be a property of the ReplicationConfig object, if it’s supported in 2.0 (I can’t remember if it is.)

Don’t believe we have the reset capability in 2.0 …

What’s the correct way to restore all doc in a channel in 2.0? ie. I remove a user’s access of a channel, purge all docs of that channel in local database, and the user gain the access again.

According the doc,

“After a user is granted access to a new channel, the changes feed incorporates all existing documents in that channel, even those from earlier sequences than the current request’s since parameter. That way the next pull request retrieves all documents to which the user now has access.”

I thought the replicator would automatically pull “all” docs whenever a user is granted, but it seems only that once. Why can’t it do that automatically when the accessibility to a new channel change?

If you purge the documents from the local database then all bets are off. What the documentation is referring to is updates that happened after the loss of access, but before it was gained back again (or, alternatively, everything that the user gains if this is the first time access is granted). If there are further edits to the document, they will come back after the purge but you will not get back the revisions that you purged. Those are gone forever.

To put it in simpler terms, getting access to a document causes the replication to receive all of the revisions you now have access to, and then as far as it’s concerned its job is done. If you purge them after that, the replicator will not check that you do not have them.