Post

Replies

Boosts

Views

Activity

Lightweight Migration Issues
I am trying to run a lightweight migration in which I am changing the name of a model property from name to title. The database is already populated with few records. Those records must be preserved. Here is my schema versions: enum TripsSchemaV1: VersionedSchema { static var versionIdentifier: String? = "Initial version" static var models: [any PersistentModel.Type] { [Trip.self] } @Model class Trip { var name: String init(name: String) { self.name = name } } } enum TripsSchemaV2: VersionedSchema { static var versionIdentifier: String? = "name changed to title" static var models: [any PersistentModel.Type] { [Trip.self] } @Model class Trip { @Attribute(originalName: "name") var title: String init(title: String) { self.title = title } } } Migration plan: enum TripsMigrationPlan: SchemaMigrationPlan { static var schemas: [any VersionedSchema.Type] { [TripsSchemaV1.self, TripsSchemaV2.self] } static var stages: [MigrationStage] { [migrateV1toV2] } static let migrateV1toV2 = MigrationStage.lightweight(fromVersion: TripsSchemaV1.self, toVersion: TripsSchemaV2.self) } And finally the usage: @main struct TripsApp: App { let container: ModelContainer init() { do { container = try ModelContainer(for: [Trip.self], migrationPlan: TripsMigrationPlan.self, ModelConfiguration(for: [Trip.self])) } catch { fatalError("Could not initialize the container.") } } var body: some Scene { WindowGroup { ContentView() .modelContainer(container) } } } When I run the app, all my data for the Trips is gone and I get the following message on the output window. Unresolved error loading container Error Domain=NSCocoaErrorDomain Code=134504 "Cannot use staged migration with an unknown coordinator model version." UserInfo={NSLocalizedDescription=Cannot use staged migration with an unknown coordinator model version.} Any ideas?
7
0
1.5k
Jul ’23
SwiftData Migrations to Change Property Name
Has anyone able to run SwiftData migrations successfully? I have a Budget class with name property and I want to update it to title property while preserving all the existing data. I have defined V1 and V2 to Budget class and now I am trying to run a migration plan. // get all existing budgets // This is the V1 Budget. This contains the name property let oldBudgets = try? context.fetch(FetchDescriptor<SpendTrackerSchemaV1.Budget>()) print(oldBudgets?[0].name) // contains the value correctly like Colorado // SpendTrackerSchemaV2.Budget contains the title property } }, didMigrate: nil)``` So, oldBudgets contains the budgets with the name property and they have values too. I want to migrate all those Budgets to the Budget with title property. Any ideas or anyone else able to perform migration actions with SwiftData framework.
2
0
1.4k
Jun ’23
Predicate based on the relationship
I am trying to use SwiftData to perform a Query based on the name of the actor, which is inside a movie model. Here is the Movie model. @Model final class Movie { var title: String var year: Int @Relationship(.noAction, inverse: \Actor.movies) var actors: [Actor] = [] init(title: String, year: Int) { self.title = title self.year = year } } Here is my actual Query: _movies = Query(filter: #Predicate { $0.actors.contains(where: { $0.name.contains(actorName) }) }) But it returns nothing, even though I am passing actorName which exists in the movie.
3
1
1.6k
Jun ’23
I was wrong! MVVM is NOT a good choice for building SwiftUI applications
SwiftUI was introduced at WWDC 2019 and it completely changed how we build our apps for Apple platform. SwiftUI provided a declarative framework, which allowed developers to quickly and easily build user interface as compared to its predecessor UIKit or AppKit. Somewhere along the lines we adopted MVVM (Model View ViewModel) design pattern as the default pattern when building SwitUI applications. In this post, I will cover my experience of using MVVM pattern with SwiftUI framework and how it worked against the SwiftUI framework, making things more complicated. Before I begin, I want to point out that I was the biggest advocate of the MVVM design pattern. I have written books on SwiftUI and MVVM, I created video courses, YouTube videos and even wrote dozens of articles explaining the benefits of using MVVM pattern for SwiftUI applications. If you are a developer and you jumped on the MVVM bandwagon then I was the driver. You can read the complete article here: https://azamsharp.com/2022/07/17/2022-swiftui-and-mvvm.html
1
1
2.7k
Jul ’22
RealityKit ARGeoAnchor Now Showing
I am trying to place a ARGeoAnchor right outside my house. I have hard-coded the coordinates but I cannot see the anchor. I believe the anchor should just show up even though I am sitting inside the house. Here is my implementation. It should show a box virtual object but I don't see anything. import SwiftUI import RealityKit import MapKit import ARKit struct ContentView : View {   var body: some View {                return ARViewContainer().edgesIgnoringSafeArea(.all)   } } struct ARViewContainer: UIViewRepresentable {       init() {           guard ARGeoTrackingConfiguration.isSupported else { return }           ARGeoTrackingConfiguration.checkAvailability { available, error in       guard available else { return }               print(available)     }   }       func makeUIView(context: Context) -> ARView {           let arView = ARView(frame: .zero)     arView.session.delegate = context.coordinator     context.coordinator.arView = arView     arView.session.run(ARGeoTrackingConfiguration())           let latitude = 444 // put correct value here     let longitude = 444 // put correct value here     //let coordinate = CLLocationCoordinate2D(latitude: 29.95681449118455, longitude: -95.50191902521054)     let coordinate = CLLocationCoordinate2D(latitude: 29.95685683319887, longitude: -95.50187836180176)     let geoAnchor = ARGeoAnchor(coordinate: coordinate)     arView.session.add(anchor: geoAnchor)                 return arView   }       func makeCoordinator() -> Coordinator {     Coordinator()   }       func updateUIView(_ uiView: ARView, context: Context) {} } class Coordinator: NSObject, ARSessionDelegate {       weak var arView: ARView?       func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {           if let anchor = anchors.first {               print(anchor)       print("adding anchor")       let box = ModelEntity(mesh: MeshResource.generateBox(size: 0.5), materials: [SimpleMaterial(color: .blue, isMetallic: true)])               let geoAnchorEntity = AnchorEntity(anchor: anchor)       geoAnchorEntity.addChild(box)       //box.generateCollisionShapes(recursive: true)               arView!.scene.addAnchor(geoAnchorEntity)     }         } }
1
0
1.3k
Apr ’22