Android To share data between two apps on android we need to use the android:sharedUserId (1) key in the manifest. Once we do this we then need to use a content provider and an associated plugin (2) to access data stored in one app from another. Drawbacks:
- If the first app is deleted then the data is lost
- Binds the apps together because of ^
- There is also the risk of data corruption if both apps make writes to the data at the same time. This is possible due to multitasking on devices.
- The plugin (2) to access content has not had any maintenance in 2 years, and I was unable to get it working in the time frame I imposed on myself.
We can also save data to the SD card, however these are sandboxed per app as well so this doesn’t really improve anything except for the case above where the app is uninstalled and the data is now not deleted. I time boxed myself to 4 hours for Android and I was unable to get sharing working in that time. However I was succesfully able to add both apps to the same sharedUserId. The issue I had was with the contentProvider plugin not being able to read the data. *Resources: (1) https://developer.android.com/guide/topics/manifest/manifest-element#uid (2) https://github.com/phearme/cordova-ContentProviderPlugin iOS Shared App groups (1) is a concept we could leverage here to share a DB between apps. There is a pattern available in an existing cordova plugin (2) although it is old. There is also a PR open (3) (for over a year) to implement shared NS data across iOS apps using SQLite. Drawbacks:
- The plugin (2) to access the shared data directly is not maintained. Could possibly require us to write our own plugin. ~4 years since last commit.
iOS would be better than on Android as the data is created independently from the app and instead is under the shared id. I set a similar time box on myself for iOS and I was also unable to get this working on iOS. I am not very familiar with iOS development though so I had some (possibly) unrelated issues here. Resources: (1) https://developer.apple.com/library/archive/documentation/General/Conceptual/WatchKitProgrammingGuide/SharingData.html (2) https://github.com/protonet/cordova-plugin-nsuserdefaults-for-app-groups (3) https://github.com/brodybits/cordova-sqlite-ext/pull/75 Overall thoughts I think the comparison to Realm db is very different from “multiple apps should be able to see data saved by any other”. The fact I could not get this working with either platform in a short period of time makes this seem like a large body of work to me. Maybe somebody with more platform specific knowledge may see it as less but to me, at a minimum we would need to;
- set up the storage so that both Android and iOS can use it seamlessly in our SDK.
- create/implement at least 2 plugins, one for each platform to allow us to leverage the sharedUserId in Android and the shared app groups in iOS
- Figure out a way to lock this storage so that only one app can write to it at a time
- For Android, figure out a way to make sure the data is not tied to the app. This may not be possible as even SD card storage is sandboxed per app. This means an app id change would mean no access to data previously created by another app.
Use Cases within Sync
- Seamless offline story for multiple apps. In a situation where the user has more than one app any offline changes on one would not require network to see those changes on the other. This would be useful in a situation where users use more than one app for their workflow with overlapping data.
- Multiple apps using the same (large) data set. Saves on network calls.
Sidebar - Storage Options In the course of this spike I also naturally found some resources related to general storage options. This may be of interest to a subsequent spike so I have collected them along with some thoughts here. SQLite Multiple plugins to use SQLite as main source of storage. Supported on both Android and iOS. Pros
- Provides a more efficient storage method than local storage does.
- SQL like syntax querying
- Less chance of data loss
Cons
- Apollo cache doesn’t support it out of the box
LocalForage Uses asynchronous storage with a simple API (same as localStorage) Pros
- Easy interface
- Supported out of the box with Apollo cache
- Can use SQLite under the hood with Cordova plugins
- Plugins available to support encryption on both Android and iOS
Resources: (1) https://github.com/localForage/localForage (2) https://github.com/xpbrew/cordova-sqlite-storage (3) https://github.com/thgreasi/localForage-cordovaSQLiteDriver (4) https://github.com/brodybits/cordova-sqlcipher-adapter |