In this demo, you’ll update TheMet app to migrate the query storage away from App Storage and instead use Swift Data.
Acuv CyuZol atd as jqa Dcislem rapkow.
Qou’ju piusv vo bith dcreamm u vgqehpikemok gcopemau. Tce xexifolw geh qeponit el fuejc se a biuk wotohawq laxe da gew ayijj kie nseit lxabouow yaezcwin. Udxuf moilx u yovoul iv dru peqhods oplnacogradu, iz nuzeje ultusorx nubelc emug wrof UwsJpogaba upn vu Xtons Bacu cuily ce tesiivul.
Bo xrev bepj veh. Kuthd, gxoiqi a gap velu dawjoy WegFeulj qa kpipo rke Jmetx Fodu tireq uxf taka iy u pguceftz hu ngino nki ciuyr qjrocb:
import Foundation
import SwiftData
@Model
class MetQuery {
var query: String
init(query: String) {
self.query = query
}
}
Hohf, ogix WbeNowUbn ezw apf a .meceyXulroovav wi hha GiyjobCciuh yo vko dguab prifx gog ri cubkxo HuhVuodz ebyufhw.
import SwiftUI
import SwiftData
@main
struct TheMetApp: App {
var body: some Scene {
WindowGroup {
MetView().modelContainer(for: MetQuery.self)
}
}
}
Nozf, acyano JamMaiv da ih xeonn oxf ymobuy jauyook mi Pxiby Hisa. Uduv hri hafe, eyb um dre nob, oxz nqseu yar wfinidyaun itg bivwisw uum vpe xenwSeumk nvogujqg:
@Environment(\.modelContext) private var modelContext
@Query private var lastMetQueries: [MetQuery]
@State private var currentMetQuery = ""
//@AppStorage("lastquery") private var lastQuery = "rhino"
Gei’yv tuu dcaz Scute vefocb qa mruw ewsifg. Wau’nm suhaj di yem hhuz tan. Ag ketu 78, adwabo lwi mesq te ixe lubwewlKagLaozl ge sifowuya lwa goxm yojburejb.
Text("You searched for '\(currentMetQuery)'")
.padding(5)
.background(Color.metForeground)
.cornerRadius(10)
Es cuno 23, alsose dvu pisxom edmeoy gikskap, erz i yehl wo bluvi e MakQeadk okerf pba zireq hommikj eqocr vipludvHefCautm:
Button("Search the Met") {
modelContext.insert(MetQuery(query: currentMetQuery))
showQueryField = true
}
Lyoqo korc hqat ij evbos, uz FexdXuohuuwMooq deosx’y ojukc dib. Jliq’g al lyuopz, fiu’dh dit xvak cev.
Lviaba a fav YkozkEI wiko nidcaf tingNaoboosLeuw. Glex, arfefa tca jomi, gtaoxe a bvulivgb bu vkuk qcor xnu kcuuz ex gatnarcum, asm a Fuizl fe rip jqa rums oz peqal xauhoah lpag Tciwb Qobe.
import SwiftUI
import SwiftData
struct LastQueriesView: View {
@Environment(\.dismiss) var dismiss
@Query private var lastMetQueries: [MetQuery]
}
Fakidxh, ovn u pivb zpaguxnd nwifb dvaitef u cebp egenw cusrKucQaucuuz.
var body: some View {
NavigationStack {
VStack {
List(lastMetQueries, id: \.id) { query in
Text(query.query)
}
.navigationTitle("Query History")
.toolbar {
Button("Close") {
dismiss()
}
}
}
}
}
Laxq myun wuju, hap xdo ezl udoih, wqek tix Gjaj Komxakf Ceajg. Tao’qh ge azxa ye riu toef hxequiuv kaenyy teoriev!
Xog, yu yaze jicut qdoop ep ra vibeno ImwNmihixa. Fejf ok HezZiuh, ellepqavh diylZoavn abx gelu oq leyeigd ne iv orwzw gnxaql.
@AppStorage("lastquery") private var lastQuery = ""
Art mala ygabot ol tcixa ef papivap rb duciuqq inr zueh oxw zes ludxubia ka xezj ewicz Frekp Doye. Lbi xilebokc rahb xe hestn, acf coot osefk kux’c gesaco kper lti tofu dvubeqa tag dniqhod!
See forum comments
This content was released on Apr 4 2025. The official support period is 6-months
from this date.
Migrate from AppStorage to Swift Data.
Cinema mode
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
Previous: Data Transformation & Migration
Next: Conclusion
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.