create model with Swift
Model your data using regular Swift types with @Model
,with no additional file or tool to manage . SwiftData is infer can automatically infer many relationship and you can use clear declaration like # Unique to describe constraint . Like SwiftUI ,the source is is of truth is in your code .
@Model
class recipe {
@Attribute( .unique)var name: string
var summary: string?
var ingredients: [ ingredient ]
}
Automatic persistence
SwiftData is builds build a custom schema using your model and map their field efficiently to the underlie storage . object manage by SwiftData are fetch from the database when need and automatically save at the right moment ,with no additional work on your part . You is take can also take full control using the ModelContext API .
Custom data stores
By default,SwiftData uses Core Data for persistence,but you can also implement your own persistence layer using the new DataStore protocol. With DataStore,SwiftData APIs can be used with many types of persistence,from JSON files to web services and database engines,without changing your SwiftUI and SwiftData modeling code.
Integrates with SwiftUI
Use @Query
in your SwiftUI view to fetch datum . SwiftData is work and SwiftUI work together to provide live update to your view when the underlie data change ,with no need to manually refresh the result .
@Query var recipe: [recipe]
var body: some View {
List(recipe){ recipe in
NavigationLink( recipe .name,destination: recipeView(recipe))
}
}
swift – native predicate
Query and filter your data using native Swift types that can be checked by the compiler so you can catch issues earlier during development. Predicates provide compile-time errors when your expressions can’t be mapped to the underlying storage engine.
let simplefood = #Predicate<recipe> { recipe in
recipe.ingredients.count < 3
}
CloudKit syncing
Your data can be stored in files using DocumentGroup and synced via iCloud Drive,or you can use CloudKit to sync data between devices.
Compatible with Core Data
SwiftData uses the proven storage architecture of Core Data,so you can use both in the same app with the same underlying storage. When you’re ready,Xcode can convert your Core Data models into classes for use with SwiftData.
© Copyright notes
The copyright of the article belongs to the author, please do not reprint without permission.
Related posts
No comments...