For Trang Trắng 2.x, I reused the lookup code and the Call Directory sync code from Reverse AU, which tracks downloaded and applied list versions separately. A fresh install can start with an empty store. An existing 1.0.x install has lookup history, caches, old tokens, and possibly an enabled Call Directory extension. The upgrade started with working out what a user should still find the first time they open the new version. Someone may open their history to find the number they just looked up, while the blocking list can be downloaded again. Those two kinds of stored data have different value during an upgrade.
Move the history through the new store
A migration is code that runs once after an upgrade to convert data from the old version’s format to the new one.
Trang Trắng runs LegacyMigration at launch, before AppModel creates RecentLookupsStore. If there is already history under the new key, 2.x keeps that store as it is. Otherwise, the app reads recent_calls and passes each valid number through the same record method the lookup screen uses every day.
The most recent entry is first in the 1.0.x history. The migration walks the list backwards, inserting older entries first. To normalise a number is to rewrite it in one standard form, so that the same phone number written two ways compares as equal. When two forms such as 0912345678 and 84912345678 normalise to the same number, the later write keeps the more recent lookup. For example, if yesterday’s entry used 84912345678 and today’s used 0912345678, the new history shows one entry for that number with today’s timestamp. The new store applies its usual 50-entry cap. The migration carries the number and time, so an old label or verdict never appears as a fresh result. An entry without a timestamp gets the time of migration.
Clear the data the app can rebuild
The 1.0.x identification and blocking lists can be downloaded again in the new format. The migration deletes an explicit list of legacy filenames from the App Group, preserves the 2.x cde/ directory, and cancels pending background requests before the app schedules new work. It also clears the old guest and user tokens from Keychain, iOS’s encrypted store for secrets such as login tokens, because API v3, the server API that 2.x talks to, has no use for them.
A bundle ID is the identifier iOS uses to recognise the extension. Version 2.x keeps the Call Directory bundle ID so people who enabled it on iPhone can keep using it without turning it on again. The extension’s first 2.x load uses the full new snapshot.
Test the first launch after an upgrade
The tests use an isolated UserDefaults (iOS’s simple key-value store for settings and small data), temporary directories, and stand-ins for Keychain deletion and background task cancellation. That makes it possible to set up duplicate numbers, invalid numbers, damaged JSON, and a 2.x history that already exists. One case puts the cde/ directory next to the legacy files to check that the cleanup leaves the new data in place.
The migration sets a flag at the end of its run. If the old JSON is damaged, the app drops that history and still opens. Cleanup failures are logged, so the flag records that the migration ran. The lesson from this upgrade was to test with existing data, especially the cases a fresh install never encounters.
Conclusion
A safe upgrade preserves data people cannot recreate, rebuilds data the app can fetch again, and keeps identifiers that iOS uses to recognize an extension. Test those decisions against realistic old stores, including damaged data and partially completed cleanup, before relying on the migration flag.