ios_catalogue1-10 Exercise 1: Build Backup and Round-Trip It ============================================================= 1. Repository split (CatalogueRepository): func stage(_ item: Item, tagNames: [String]) throws { // exactly what save() did before, EXCEPT the final context.save(): // insert the item if it is new, then for each name: let tag = try tag(named: name); collect; // then item.tags = collected (insert the model before assigning relationships) } func save(_ item: Item, tagNames: [String]) throws { try stage(item, tagNames: tagNames) try context.save() } 2. Add BackupFile/BackupItem, BackupService, BackupDocument, BackupView, and in ListView a toolbar Menu: Menu { Button("Backup", systemImage: "externaldrive") { showBackup = true } } label: { Image(systemName: "ellipsis.circle") } .sheet(isPresented: $showBackup) { BackupView() } 3. Export with 3+ items. Opened in a text editor the file is pretty-printed JSON, keys sorted: { "exportedAt": 17..., "items": [ { "createdAt": 16..., "creator": "...", "formatDetail": "...", "notes": "", "releaseYear": 1965, "tags": ["classic","sci-fi"], "title": "Dune", "type": "Book" }, ... ], "version": 1 } (an item with no year simply has no releaseYear key.) Dates are Unix milliseconds, types and tags are names. 4. Import into a fresh install (delete the app first): "Added 3 item(s), skipped 0 already present." Importing the same file again: "Added 0 item(s), skipped 3 already present." Problems you may meet: the file picker greys out the file (content type: try [.json, .plainText]); a "file couldn't be opened" error on import (the security-scoped bracket is missing or the read is outside it); the exporter never appears (the document is nil when showExporter becomes true); the sheet's environment is missing ErrorCenter (supply it where the sheet is presented, or in the preview).