MA4: App architecture, state and storage on the phone
Storing data on the phone
🎯You'll be able to
Choose between key-value storage, SQLite, secure storage and files, and decide what shouldn't be stored at all.
🏠The everyday picture
At home you keep spices in a rack, documents in a locked drawer, photos in albums and the week's menu on the fridge. Each place suits what goes in it.
📘What it really is
- Key-value storage (AsyncStorage in React Native) keeps text under a key. Good for settings and small or medium data saved as JSON.
-
SQLite (expo-sqlite, Room on Android, Core Data or SwiftData on iOS) is a database on the phone: better for thousands of records, searching and partial updates.
-
Secure storage (the iOS Keychain and Android Keystore, through expo-secure-store) is for small secrets such as sign-in tokens.
- Files hold photos and downloads; store the path in your data, not the file itself.
- Don't store what you don't need, passwords in plain storage, or other people's data longer than necessary. The phone can be lost.
📝A worked example
Input: KhetNote must keep visits, the outbox and the sync cursor between launches.
Steps:
- For a few thousand records, key-value storage is enough: one JSON object under
khetnote:v1. - Write after each change, in order, so a later write can't be overtaken by an earlier one.
- Note when to move to SQLite: tens of thousands of visits, or searching on the phone.
Expected output: a storage choice with a reason and a point at which you'd change it.
🌍Where it's used
- Settings and drafts in key-value storage.
- Offline maps, messages and records in SQLite.
- Sign-in tokens in secure storage.
📝Case scenario
A delivery app kept a sign-in token in plain key-value storage. On a rooted test phone, a security review read it in a minute. Moving it to secure storage, and keeping only non-secret data in the plain store, closed the finding.
🖱️Try it
Read the starter's src/storage/store.js and write one sentence on why its data disappears when the app restarts. Success: you name the
line where the data is kept.
⚠️Common mistakes
- Secrets in plain storage. Sign: a token in AsyncStorage. Fix: secure storage.
- One huge value for a huge data set. Sign: slow saves with many records. Fix: SQLite when data grows.
- Storing files inside JSON. Sign: a giant stored value or bundle. Fix: files on disk, paths in data.
🛠️For your project
Project 1's test 1 creates a store, saves a visit, then creates a new store with the same storage and checks the visit is still there.
✅Quick check
Where should a sign-in token be stored on a phone?
Show the answer
In secure storage (the Keychain or Keystore), not in plain key-value storage.
When would you move from key-value storage to SQLite?
Show the answer
When there are many records, or the app needs to search or update parts of the data.
