Comments (3)
IPC for multiprocess support
To share data between two processes you need inter-process communication (IPC).
A common android example for this are SyncAdapters
which run on different processes than your UI. When you want to trigger an action in the UI from your SyncAdapter
you can't simply call a function of your activity you need to use IPC. When you use the SharedPreferences
in the SyncAdapter you are able to save the data. But the UI (in a different process) uses a different SharedPreferences
object and does not receive a data change. Even worse, if you read the value written in the SyncAdapter
from the UI process you get the old value for some seconds. (issue on StackOverflow)
Implementing Binder
for IPC is hard and low level. But ContentProviders
do IPC out of the box. That's why a ContentProvider
is used to support multiple processes.
Why a database
A ContentProvider
doesn't mean the data is written to a database. It's just a interface which supports CRUD operations. And those operations are process save. Yes, Tray is using a sqlite database under the hood but if would be possible to swap out this implementation and save the data to any other persistence framework. This works because the ContentProvider
would use the same persistence Object for all operations. But the multi process support would only be guaranteed when you access the data with Tray and the ContentProvider
and not directly with the other persistence frameworks.
So ContentProviders
makes CRUD operations multi process save.
The Sqlite database is used to store the data. It's no just a simple key value store (like the shared preferences) because you can get additional informations for every key value pair saved to Tray automatically. You can get informations when the data was saved first or when the data was last updated. (see here).
ContentProvider facts
- The
ContentProvider
used is only accessible from your Application. - You can override this permission in your manifest to make it public accessible.
- If you want you can access the data in the ContentProvider directly with
getContext().getContentResolver().insert(uri, values);
. This means Tray is just a high level abstraction to access the data in theContentProvider
.
Tray is not async
Tray doesn't support asynchronous operations. Everything runs synchronous. A mPrefs.put("key", "data");
immediately writes the data into the database. If this function returns the data is saved to disk.
It's up to the developer where to save the data, inside the UI Thread or in a worker thread. In my cases this was never a problem because I was writing data in a non-UI process where I didn't have to care about frames.
I hope I could help you understand this lib 😄!
from tray.
so now everything is clear.
I propose to describe(copy your text) it on the main page or in the documentation
from tray.
I have a question,when a call mPrefs.get("key") on the ui thread, and it is synchronous, will it block the UI thread when the data is so big?
from tray.
Related Issues (20)
- minSdkVersion HOT 1
- ConcurrentModificationException in ContentProviderStorage.java:93 HOT 3
- NullPointerException in ContentProviderStorage.java:104 HOT 1
- Report ContentProvider Not Found information to android bugs HOT 2
- How to upgrade data? HOT 2
- TrayRuntimeException in TrayContract
- RunTimeException: Unable to create application caused by TrayContract.getAuthority HOT 4
- How to migrate from default module to my own module HOT 1
- List all prefs in module HOT 1
- Question: minSdkVersion value HOT 1
- android-DirectBoot HOT 3
- Share data between apps. HOT 1
- How to share values between apps? HOT 2
- delet tray.db and how to recreate HOT 1
- Accessing Outside of Container App 2.0 HOT 1
- java.lang.NoClassDefFoundError: Failed resolution of: Lnet/grandcentrix/tray/R$string; HOT 1
- can not support multi app share data HOT 1
- 不知道怎么回事的bug HOT 1
- NullPointerException at unregisterOnTrayPreferenceChangeListener in Service HOT 4
- ANR that leads to app crashing, support needed HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tray.