Giter Club home page Giter Club logo

Comments (3)

passsy avatar passsy commented on May 29, 2024

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 the ContentProvider.

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.

kamilwlf avatar kamilwlf commented on May 29, 2024

so now everything is clear.

I propose to describe(copy your text) it on the main page or in the documentation

from tray.

chechuangfeng avatar chechuangfeng commented on May 29, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.