How can I store data from multiple applications in one place?

I have several applications built on one engine. This engine stores data in SQLite. The database structure is the same for all applications. I need to organize a common storage for all these applications.

For example, there is application A and B. The first user installed application A and added data, then installed application B, and this is necessary to synchronize the databases of these two applications. Using the Content Providers function will not work, because in this case application B will not work without application A. And we will not know which user installs the application first.

There is an idea to synchronize the database of all applications, and each application will remain with its own database, but then I will have a copy of the databases. Means that as many applications are installed as many copies of the databases that I will have.

Perhaps there is some neat way to implement the idea?

+5
source share
3 answers

You can save the database to external storage and access it from all your applications (use getExternalStorageDirectory ).

Use the openOrCreateDatabase method of the SQLiteDatabase class to access it and read / write data from it.

Since access to several processes can lead to locks, I suggest that you independently manage this file with the name / packagename of the lock application so that you can synchronize access to data between your applications.

+1
source

Why don't you try using "sharedUserId" in all of your applications. At the same time, you can also access data from other applications.

Assuming that you already know all the applications of other applications, the first time you download each application, you begin to search for the shared folder in which you create your database. This folder may be in your SD card. If you find a DB file, you can open it and use. Otherwise, you create it. Then use the same database in all applications?

I'm not sure what are the consequences of opening / writing the same database from different applications. Perhaps you need to figure out a way to lock the mechanism during recording (perhaps create a .lock file when writing to the database?)

+4
source

I faced the same design problem (several applications using the same engine contain persistent SQLite database tables, services, and tons of logic).

after I have done my own research, I am afraid that the conclusion is that the only way to do this is to do it in the Google way. :

Google Play Services is a process that Google has definitely decided to solve this problem for its own scenario - an engine that should always be available to all applications (including their own). this means that it is an independent application that provides services and content providers to all applications.

Google will launch Google Play Services in the background using the Google Play Store app. what a privilege no other application can have, since they control both applications and the Android OS itself (not fair!)

therefore, if you can force your users to download a dedicated application only for your engine (for example, Google), this is great!

if not - then you will have to settle until the main / main application has to be installed, and this is the only thing that provides data and services for everyone else.

+2
source

Source: https://habr.com/ru/post/1212643/


All Articles