Post

Replies

Boosts

Views

Activity

Maintaining a local copy of server data - question about the sample code
I've been going through SwiftData documentation, as I'm trying to learn how to work with SwiftData, especially how to use it to cache web responses. "Maintaining a local copy of server data" sample seems to be perfect source for me then, but I find it a little bit confusing, or lacking. There's this function: /// Loads new earthquakes and deletes outdated ones. @MainActor static func refresh(modelContext: ModelContext) async { do { // Fetch the latest set of quakes from the server. logger.debug("Refreshing the data store...") let featureCollection = try await fetchFeatures() logger.debug("Loaded feature collection:\n\(featureCollection)") // Add the content to the data store. for feature in featureCollection.features { let quake = Quake(from: feature) // Ignore anything with a magnitude of zero or less. if quake.magnitude > 0 { logger.debug("Inserting \(quake)") modelContext.insert(quake) } } logger.debug("Refresh complete.") } catch let error { logger.error("\(error.localizedDescription)") } } It says specifically that it "deletes outdated ones", but where does the actual deletion happens? All I can see is call to modelContext.insert(quake) which will update existing entries or create new ones if they don't exist yet, if I'm understanding things right. But quakes that were already in the database, and are not present in the response, don't seem to be deleted anywhere in this function. Or am I missing something?
1
0
336
May ’24