Section 14/181 menit
14. Perbandingan dengan Core Data
14. Perbandingan dengan Core Data
| Aspek | SwiftData | Core Data |
|---|---|---|
| Versi minimum | iOS 17 / macOS 14 | iOS 3 / macOS 10.4 |
| Definisi model | @Model macro di Swift |
.xcdatamodeld + NSManagedObject subclass |
| Type safety | Penuh — compile-time | Parsial — NSFetchRequest typed, predicate runtime |
| Predicate | #Predicate — compile-time |
NSPredicate — format string, runtime |
| Thread safety | @ModelActor — Swift concurrency |
performBlock/performBlockAndWait — GCD |
| SwiftUI integration | Native (@Query, @Bindable) |
Manual + @FetchRequest (terbatas) |
| Migration | VersionedSchema + SchemaMigrationPlan |
NSMigrationManager + mapping model |
| CloudKit sync | Built-in konfigurasi | NSPersistentCloudKitContainer |
| Debugging | Terbatas | SQLDebug, Core Data instrument |
| Interop | Bisa akses Core Data stack | N/A |
| Custom store | Tidak bisa | NSIncrementalStore |
Kapan Tetap Pakai Core Data
- App perlu support iOS 16 atau lebih lama
- Butuh custom NSIncrementalStore
- Project sudah mature dengan banyak NSFetchedResultsController
- Butuh fine-grained migration control yang kompleks
- Butuh Core Spotlight integration level rendah
Interoperabilitas
SwiftData dan Core Data bisa berjalan berdampingan — SwiftData mengexpose Core Data stack melalui ModelContainer:
swift
let container: ModelContainer = ...
// Akses NSPersistentContainer di balik SwiftData
let coreDataContainer = container.configurations.first // ModelConfiguration
// Untuk akses penuh, gunakan initializer dengan NSPersistentContainer yang sudah ada