Sharing a CBL Database between apps

I have a use case where two separate apps will use the same data. In this scenario the local databases are like to get quite large and, if possible, I will need replication of the data to occur locally between them rather than to a sync gateway.

Rather than having two exact copies of the same database on the iPad, is it possible to share the same database between apps? I know that you can supply the directory when instantiating the Manager class but I don’t know if it is possible to do that in-between app spaces.

If it is possible, are there any limitations or possible issues that I may run into?

Thanks for your time.

We don’t support sharing a database file between processes. (Most things will work, but notifications won’t because one process doesn’t know when the other changes the db, and that causes other things to fail.)

It’s even more difficult on iOS, where apps are completely sandboxed and it’s not possible for two apps, even by the same developer, to share files.

@jens About this scenario, I was wondering if you can access the data between the apps through the CBL REST API. Otherwise, is there another better way to do this?

It’s probably possible to do that, but there are a number of issues I can think of:

  • The ‘server’ app has to be running at the same time as the ‘client’. This is difficult to arrange on iOS because the OS doesn’t want apps to keep running in the background unless they’re doing specific things like audio playback or handing push notifications. Otherwise the app tends to get suspended after it’s been deactivated.
  • Authentication is important, unless you want every app on the device to be able to access the database. You can set up a username/password in the ‘server’, but somehow the ‘client’ has to learn those credentials in a secure way. (On iOS a shared Keychain item might work for this.)
  • And of course the ‘client’ app can’t use the native Couchbase Lite APIs to access the database; instead it has to express everything as HTTP requests.

You’re right, the fact that the ‘server’ application should run at the same time that the ‘client’ is a big problem. For Android, the content provider is something better for this, I think.

These posts may be of relevance to you -
how-do-i-reference-the-same-cb-db-in-an-always-running-background-service-an-activity-on-android

accessing-couchbase-lite-via-a-content-provider-in-android

Ironically, if the two apps aren’t running at the same time, it becomes perfectly fine to share the same database file.

This is exactly what I was thinking that if the database not in use the other app can use it. But there may be a case where one has to find out whether the other app is dead or not. Secondly both apps should share the same path of the couch base lite database.

Hello Jens,

I know that this is an old topic but I would like to know how the status is right now for this thing. I could not find a lot of things about that reading on the internet and forums. We are using currently CBL in one app and what I found is this link so far: https://github.com/couchbaselabs/couchbase-mobile-ios-app-extension
This repository talks about how to share the data between an app and extension, but then a question comes to my mind. Would be possible to share a database between different apps? Technically, I guess the answer is yes because actually an app and extension are different sandboxes like two different apps either.
My question is more about if you make a change in an app what would happen to the other app, regarding the extension is able to see the change straight, but would it be the same for a different app?
I make this question because in my organization we are thinking to go for multiapp strategy and I am not sure if I should share the same model and data among apps or build different models within apps regarding the functionality that is need for each one and sync the necessary data in each app.

I want to congratulate you guys for the hard work and hope that you keep working like this!

Thanks and regards

Maximo

The blog corresponding to that GitHub repo is here and probably a better read . There is a follow on blog that includes sync via Sync gateway.

The code and blog discusses this in the context of 1.x but the same App group methodology applies for 2.x as well.
The idea is simple. On iOS, if you want to share data between apps, you can host it in a secure container that is shared via App groups. So by hosting the CBL database file in this shared container, you can provide access to different apps to the same database.

That is correct. It can be two user apps as well as long as they belong to same app group. Technically an app extension and an app are executed in the context of two separate processes / apps so the same principles apply

If you make a change in one app, the second app will not be automatically notified of the change. However, if the second app queries the data , it will see the updates that were made by first app.

Why is this the case ?
Since the database file itself is shared, changes made by either app will be available to the other. However, since the two apps are running in two different processes, each will have it’s own handle of the Database with independent access to the database, it’s own set of replicators, event listeners etc. So notifications are not propagated across process boundaries.

Also are the apps expected to be running at the same time, syncing to the same database. Because if you do expect them to do simultaneous replications to same database instance, the behavior is not well defined…

What are your use cases for sharing the database locally between apps ? I think that will help decide if you should pursue the approach of sharing data.