MA4: App architecture, state and storage on the phone

Lesson 4 of 5 · reading 15–25 minutes; practice and project time is on the module page

Saving reliably and changing the format

🎯You'll be able to

Save data so it can't be half-written or overtaken, read it back safely, and change the stored format between app versions.

🏠The everyday picture

A shop's ledger has a page for each day, written in order. When the shop changes its columns next year, it doesn't rewrite old pages; it starts a new format and knows how to read both.

📘What it really is

📝A worked example

Input: KhetNote 1.2 will add a followUp date to visits and rename issue to reported.

Steps:

  1. Keep reading khetnote:v1. On start, if it exists and khetnote:v2 doesn't, convert each visit and write v2.
  2. Keep v1 until v2 is written successfully, then remove it.
  3. Test: a store with v1 data opens in 1.2 with every visit intact.

Expected output: an upgrade that keeps every adviser's data.

🌍Where it's used

📝Case scenario

A notes app changed its storage format without a migration. After the update, it couldn't read old notes and showed an empty list. Many users thought their notes were deleted. A hotfix added the migration, but the reviews stayed.

🖱️Try it

Write the steps of a v1-to-v2 migration for one change to KhetNote's data, and the test you'd write for it. Success: your steps keep v1 until v2 is safely written.

⚠️Common mistakes

🛠️For your project

Project 1's store saves in order and offers flush(); tests 1, 3 and 8 restart the store and expect the data, outbox and cursor back.

✅Quick check

Why chain saves one after another?

Show the answer

So a slow earlier save can't overwrite a newer one.

What should a migration keep until the new format is safely written?

Show the answer

The old data.

← PreviousStoring data on the phone
ProLearners Assist