Search results for

“SwiftData inheritance relationship”

4,982 results found

Post

Replies

Boosts

Views

Activity

SwiftData "Auto Inserts" array into ModelContext
Definitely one of the stranger quirks of SwiftData I've come across. I have a ScriptView that shows Line entities related to a Production, and a TextEnterScriptView that’s presented in a sheet to input text. I’m noticing that every time I type in the TextEditor within TextEnterScriptView, a new Line shows up in ScriptView — even though I haven’t explicitly inserted it into the modelContext. I'm quite confused because even though I’m only assigning a new Line to a local @State array in TextEnterScriptView, every keystroke in the TextEditor causes a duplicate Line to appear in ScriptView. In other words, Why is SwiftData creating new Line entities every time I type in the TextEditor, even though I’m only assigning to a local @State array and not explicitly inserting them into the modelContext? Here is my minimal reproducible example: import SwiftData import SwiftUI @main struct testApp: App { var body: some Scene { WindowGroup { ContentView() .modelContainer(for: Line.self, isAutosave
1
0
93
Apr ’25
Can @Query and ModelActor co-exist? How?
Context: The SwiftUI @Query macro has an internal modelContext. The ModelActor also has a modelContext, from which the data should be read/written. Issue: When writing to @Model data fetched with @Query macro using a ModelActor, it will crash in the most not-obvious ways. Also, fetching @Model with ModelActor will result in errors in Swift 6 since @Model aren't sendable. Problem to Solve: - How to write a good amount of data to SwiftData/CoreData without blocking the UI thread? Would the recommendation from the Apple team be that a large amount of data should be read/written with ModelActor and a small amount should be done with the @Query's internal modelContext ?
1
0
158
Apr ’25
Reply to Strange media player overlay main screen
Resolved. In the app, all UI elements was created manually and all ViewController inherited from a BaseViewController, including MainMenuViewController. a. the code caused the issue: @interface BaseViewController: AVPlayerViewController b. modified code: @interface BaseViewController: UIViewController Why the base ViewController inherited from AVPlayerViewController, because there's a VideoViewController that will be used to play video. unfortunately, OC don't support multiple inheritance, so I have to embed the base ViewController's functions into VideoViewController instead of inheritance. It's an issue caused by not being careful enough when modifying code.
Topic: UI Frameworks SubTopic: UIKit
Apr ’25
Reply to App Store Connect analytics reports return 500 API error
Now I started facing another problem with pagination: next url is invalid. Get: https://api.appstoreconnect.apple.com/v1/analyticsReportRequests/{id}/relationships/reports?cursor=Mg Error: { errors: [ { id: 56e0cfab-4e86-4e4b-9a12-d76277ad9f94, status: 400, code: PARAMETER_ERROR.INVALID, title: A parameter has an invalid value, detail: 'Mg' is not a valid cursor for this request, source: { parameter: cursor } } ] }
Apr ’25
Reply to Xcode 16.3 / macOS 15.4: SwiftData ModelContainer 在关联 iCloud Container 后初始化时崩溃
When you create a ModelConfiguration, the default value of the cloudKitDatabase attribute .automatic, which tells SwiftData to enable CloudKit synchronization using the primary CloudKit container from the app’s entitlements. That is why you see that SwiftData automatically picks up the CloudKit container, if you configure one in your project, and enables CloudKit integration. If you don't like the default behavior, pass .none when you create the model configuration. Assuming that you use the Xcode template code to trigger the crash, most likely, you will see the following message in your Xcode console: Unresolved error loading container Error Domain=NSCocoaErrorDomain Code=134060 A Core Data error occurred. UserInfo={NSLocalizedFailureReason=CloudKit integration requires that all attributes be optional, or have a default value set. The following attributes are marked non-optional but do not have a default value: Item: timestamp} The log makes clear that the failure is triggered because Item:
Apr ’25
Reply to SwiftData data crashes with @Relationship
The stack trace seems to indicate that your code was trying to retrieve the value of an attribute, but the attribute didn't exist in the SwiftData model. The code snippet shows that TrainInfo and StopStation is a to-many relationship, which has nothing wrong. isOrigin and isStarting seem to be an attribute of StopStation. They are not included in your code snippet, and so I can't comment. Overall, the information you provided doesn't seem to be enough to diagnose the issue... Best, —— Ziqiao Chen  Worldwide Developer Relations.
Apr ’25
Reply to SwiftData migration crashes when working with relationships
Theoretically I could not assign the relationships in the willMigrate block Yeah, the context passed to willMigrate is tied to the existing store, which doesn't have the knowledge of your new schema, and so trying to persisting an ItemSchemaV2 object, which I believe is a part of the new schema, will fail. You can consider do the data transformation in didMigrate, where the context is tied to the migrated store, which allows you to access the new schema. but afterwards I am not able to tell which list and items belonged together. Would you mind to share why? When didMigrate is called, ItemSchemaV1.Item and ItemSchemaV1.ItemList have been migrated to ItemSchemaV2.Item and ItemSchemaV2.ItemList, and so you can still fetch the item list from ItemSchemaV2.ItemList, update ItemSchemaV2.ItemList.note, and save the change, can't you? Best, —— Ziqiao Chen  Worldwide Developer Relations.
Apr ’25
ModifyPromo 409: State Error
When calling modifypromo, I keep getting this error: error: HTTP status code 409, body { errors : [ { id : 7aeb5c20-576d-4500-ad65-525d298d2093, status : 409, code : STATE_ERROR, title : The request cannot be fulfilled because of the state of another resource., detail : Cannot invoke com.apple.its.pricing.api.model.Country.countryCodeISO2A() because the return value of com.apple.its.pricing.api.model.offers.AdHocOfferProduct.country() is null } ] } My request is: Where id is the encrypted values needed { data: { attributes: {}, relationships: { prices: { data: [ { id: id, type: subscriptionPromotionalOfferPrices } ] } }, type: subscriptionPromotionalOffers, id: id }, included: [ { attributes: {}, id: subscriptionPromotionalOfferPrices, relationships: { subscriptionPricePoint: { data: { id: id, type: subscriptionPricePoints } }, territory: { data: { id: USA, type: territories } } } } ] }
2
0
126
Apr ’25
Reply to SwiftUI & SwiftData: Fatal Error "Duplicate keys of type" Occurs on First Launch
Was just dealing with the same thing, and removing the id: .self from a ForEach I just added seems to have solved it - thanks! As to the reason, my guess is that SwiftUI is holding a Dictionary of every object, using the id: value as the key - SwiftData is likely mutating these objects behind the scenes, to reload data, fault data, etc. That would cause this error to happen. Omitting the id: parameter in the ForEach, or using id: .id would use the stable identifier as the key of the 'hidden' Dictionary, therefore eliminating the issue. All of that makes sense to me, at least - unfortunately, we use id: .self so frequently, that I can almost guarantee that I'll make this mistake again in the future. Would be nice if making this error were impossible, or if Xcode had a way to warn us about it.
Apr ’25
SwiftData migration crashes when working with relationships
The following complex migration consistently crashes the app with the following error: SwiftData/PersistentModel.swift:726: Fatal error: What kind of backing data is this? SwiftData._KKMDBackingData My app relies on a complex migration that involves these optional 1 to n relationships. Theoretically I could not assign the relationships in the willMigrate block but afterwards I am not able to tell which list and items belonged together. Steps to reproduce: Run project Change typealias CurrentSchema to ItemSchemaV2 instead of ItemSchemaV1. Run project again -> App crashes My setup: Xcode Version 16.2 (16C5032a) MacOS Sequoia 15.4 iPhone 12 with 18.3.2 (22D82) Am I doing something wrong or did I stumble upon a bug? I have a demo Xcode project ready but I could not upload it here so I put the code below. Thanks for your help typealias CurrentSchema = ItemSchemaV1 typealias ItemList = CurrentSchema.ItemList typealias Item = CurrentSchema.Item @main struct SwiftDataMigrationAp
1
0
185
Apr ’25
Reply to HealthKit SwiftData sync
I’m also building a health app. How do I handle a situation where the user doesn’t want to grant Health access, but I want my app to be able to still function using local data via SwiftData? How would these two data stores coexist if the user later grants health access?
Apr ’25
SwiftData data crashes with @Relationship
I've noticed that SwiftData's @Relationship seems to potentially cause application crashes. The crash error is shown in the image. Since this crash appears to be random and I cannot reproduce it under specific circumstances, I can only temporarily highlight that this issue seems to exist. @Model final class TrainInfo { @Relationship(deleteRule: .cascade, inverse: StopStation.trainInfo) var stations: [StopStation]? } @Model final class StopStation { @Relationship var trainInfo: TrainInfo? } /// some View var origin: StopStationDisplayable? { if let train = train as? TrainInfo { return train.stations?.first(where: { $0.isOrigin }) ?? train.stations?.first(where: { $0.isStarting }) } return nil } // Some other function or property func someFunction() { if let origin, let destination { // Function implementation } }
1
0
154
Apr ’25
Reply to Xcode? Hacking? Iphone11
hello, first my disclaimer...I am very new to Apple products and my knowledge base is minimal. That said, I have immersed myself in an effort to learn all I possibly can about all topics related to being hacked, because I have also been invaded. i will summarize my experiences and offer a nugget or two of gathered wisdom. i believe the invasion (access to my device) was not physical, and i think it began through using information held only by my provider. **Incidentally, in a separate though related research project, and with info gathered specific to corporate ownership, parent-child relationships, patterns found in company formation/agent for process data/physical addresses offered publicly through registration documentation, and probably more that I do not recall right now...I think I know the person who owns, or has financial control of, my cellular carrier. I will add that this person owns and controls many nationally known companies but disconnects himself by using proxy synthetic identities to
Apr ’25
SwiftData "Auto Inserts" array into ModelContext
Definitely one of the stranger quirks of SwiftData I've come across. I have a ScriptView that shows Line entities related to a Production, and a TextEnterScriptView that’s presented in a sheet to input text. I’m noticing that every time I type in the TextEditor within TextEnterScriptView, a new Line shows up in ScriptView — even though I haven’t explicitly inserted it into the modelContext. I'm quite confused because even though I’m only assigning a new Line to a local @State array in TextEnterScriptView, every keystroke in the TextEditor causes a duplicate Line to appear in ScriptView. In other words, Why is SwiftData creating new Line entities every time I type in the TextEditor, even though I’m only assigning to a local @State array and not explicitly inserting them into the modelContext? Here is my minimal reproducible example: import SwiftData import SwiftUI @main struct testApp: App { var body: some Scene { WindowGroup { ContentView() .modelContainer(for: Line.self, isAutosave
Replies
1
Boosts
0
Views
93
Activity
Apr ’25
Can @Query and ModelActor co-exist? How?
Context: The SwiftUI @Query macro has an internal modelContext. The ModelActor also has a modelContext, from which the data should be read/written. Issue: When writing to @Model data fetched with @Query macro using a ModelActor, it will crash in the most not-obvious ways. Also, fetching @Model with ModelActor will result in errors in Swift 6 since @Model aren't sendable. Problem to Solve: - How to write a good amount of data to SwiftData/CoreData without blocking the UI thread? Would the recommendation from the Apple team be that a large amount of data should be read/written with ModelActor and a small amount should be done with the @Query's internal modelContext ?
Replies
1
Boosts
0
Views
158
Activity
Apr ’25
Reply to Strange media player overlay main screen
Resolved. In the app, all UI elements was created manually and all ViewController inherited from a BaseViewController, including MainMenuViewController. a. the code caused the issue: @interface BaseViewController: AVPlayerViewController b. modified code: @interface BaseViewController: UIViewController Why the base ViewController inherited from AVPlayerViewController, because there's a VideoViewController that will be used to play video. unfortunately, OC don't support multiple inheritance, so I have to embed the base ViewController's functions into VideoViewController instead of inheritance. It's an issue caused by not being careful enough when modifying code.
Topic: UI Frameworks SubTopic: UIKit
Replies
Boosts
Views
Activity
Apr ’25
Reply to App Store Connect analytics reports return 500 API error
Now I started facing another problem with pagination: next url is invalid. Get: https://api.appstoreconnect.apple.com/v1/analyticsReportRequests/{id}/relationships/reports?cursor=Mg Error: { errors: [ { id: 56e0cfab-4e86-4e4b-9a12-d76277ad9f94, status: 400, code: PARAMETER_ERROR.INVALID, title: A parameter has an invalid value, detail: 'Mg' is not a valid cursor for this request, source: { parameter: cursor } } ] }
Replies
Boosts
Views
Activity
Apr ’25
Reply to SwiftData Many-To-Many Relationship: Failed to fulfill link PendingRelationshipLink
Do you have a minimal sample with detailed steps that reproduce the issue? The way you defined the relationship looks good to me, and so a runnable project may help diagnose the issue. Your post can contain a link to where your test project is hosted. Best, —— Ziqiao Chen  Worldwide Developer Relations.
Replies
Boosts
Views
Activity
Apr ’25
Reply to SwiftData Lightweight Migraton failed with VersionedSchema
Yeah, enum RoundAmount is not Codable and so SwiftData doesn't know how to encode / decode MyNumber.rounding. You can fix the issue by making your enum Codable, like below: enum RoundAmount: Int, Codable { case fiveMinute = 5, tenMinute = 10, thirtyMinute = 30 } Best, —— Ziqiao Chen  Worldwide Developer Relations.
Replies
Boosts
Views
Activity
Apr ’25
Reply to Xcode 16.3 / macOS 15.4: SwiftData ModelContainer 在关联 iCloud Container 后初始化时崩溃
When you create a ModelConfiguration, the default value of the cloudKitDatabase attribute .automatic, which tells SwiftData to enable CloudKit synchronization using the primary CloudKit container from the app’s entitlements. That is why you see that SwiftData automatically picks up the CloudKit container, if you configure one in your project, and enables CloudKit integration. If you don't like the default behavior, pass .none when you create the model configuration. Assuming that you use the Xcode template code to trigger the crash, most likely, you will see the following message in your Xcode console: Unresolved error loading container Error Domain=NSCocoaErrorDomain Code=134060 A Core Data error occurred. UserInfo={NSLocalizedFailureReason=CloudKit integration requires that all attributes be optional, or have a default value set. The following attributes are marked non-optional but do not have a default value: Item: timestamp} The log makes clear that the failure is triggered because Item:
Replies
Boosts
Views
Activity
Apr ’25
Reply to SwiftData data crashes with @Relationship
The stack trace seems to indicate that your code was trying to retrieve the value of an attribute, but the attribute didn't exist in the SwiftData model. The code snippet shows that TrainInfo and StopStation is a to-many relationship, which has nothing wrong. isOrigin and isStarting seem to be an attribute of StopStation. They are not included in your code snippet, and so I can't comment. Overall, the information you provided doesn't seem to be enough to diagnose the issue... Best, —— Ziqiao Chen  Worldwide Developer Relations.
Replies
Boosts
Views
Activity
Apr ’25
Reply to SwiftData migration crashes when working with relationships
Theoretically I could not assign the relationships in the willMigrate block Yeah, the context passed to willMigrate is tied to the existing store, which doesn't have the knowledge of your new schema, and so trying to persisting an ItemSchemaV2 object, which I believe is a part of the new schema, will fail. You can consider do the data transformation in didMigrate, where the context is tied to the migrated store, which allows you to access the new schema. but afterwards I am not able to tell which list and items belonged together. Would you mind to share why? When didMigrate is called, ItemSchemaV1.Item and ItemSchemaV1.ItemList have been migrated to ItemSchemaV2.Item and ItemSchemaV2.ItemList, and so you can still fetch the item list from ItemSchemaV2.ItemList, update ItemSchemaV2.ItemList.note, and save the change, can't you? Best, —— Ziqiao Chen  Worldwide Developer Relations.
Replies
Boosts
Views
Activity
Apr ’25
ModifyPromo 409: State Error
When calling modifypromo, I keep getting this error: error: HTTP status code 409, body { errors : [ { id : 7aeb5c20-576d-4500-ad65-525d298d2093, status : 409, code : STATE_ERROR, title : The request cannot be fulfilled because of the state of another resource., detail : Cannot invoke com.apple.its.pricing.api.model.Country.countryCodeISO2A() because the return value of com.apple.its.pricing.api.model.offers.AdHocOfferProduct.country() is null } ] } My request is: Where id is the encrypted values needed { data: { attributes: {}, relationships: { prices: { data: [ { id: id, type: subscriptionPromotionalOfferPrices } ] } }, type: subscriptionPromotionalOffers, id: id }, included: [ { attributes: {}, id: subscriptionPromotionalOfferPrices, relationships: { subscriptionPricePoint: { data: { id: id, type: subscriptionPricePoints } }, territory: { data: { id: USA, type: territories } } } } ] }
Replies
2
Boosts
0
Views
126
Activity
Apr ’25
Reply to SwiftUI & SwiftData: Fatal Error "Duplicate keys of type" Occurs on First Launch
Was just dealing with the same thing, and removing the id: .self from a ForEach I just added seems to have solved it - thanks! As to the reason, my guess is that SwiftUI is holding a Dictionary of every object, using the id: value as the key - SwiftData is likely mutating these objects behind the scenes, to reload data, fault data, etc. That would cause this error to happen. Omitting the id: parameter in the ForEach, or using id: .id would use the stable identifier as the key of the 'hidden' Dictionary, therefore eliminating the issue. All of that makes sense to me, at least - unfortunately, we use id: .self so frequently, that I can almost guarantee that I'll make this mistake again in the future. Would be nice if making this error were impossible, or if Xcode had a way to warn us about it.
Replies
Boosts
Views
Activity
Apr ’25
SwiftData migration crashes when working with relationships
The following complex migration consistently crashes the app with the following error: SwiftData/PersistentModel.swift:726: Fatal error: What kind of backing data is this? SwiftData._KKMDBackingData My app relies on a complex migration that involves these optional 1 to n relationships. Theoretically I could not assign the relationships in the willMigrate block but afterwards I am not able to tell which list and items belonged together. Steps to reproduce: Run project Change typealias CurrentSchema to ItemSchemaV2 instead of ItemSchemaV1. Run project again -> App crashes My setup: Xcode Version 16.2 (16C5032a) MacOS Sequoia 15.4 iPhone 12 with 18.3.2 (22D82) Am I doing something wrong or did I stumble upon a bug? I have a demo Xcode project ready but I could not upload it here so I put the code below. Thanks for your help typealias CurrentSchema = ItemSchemaV1 typealias ItemList = CurrentSchema.ItemList typealias Item = CurrentSchema.Item @main struct SwiftDataMigrationAp
Replies
1
Boosts
0
Views
185
Activity
Apr ’25
Reply to HealthKit SwiftData sync
I’m also building a health app. How do I handle a situation where the user doesn’t want to grant Health access, but I want my app to be able to still function using local data via SwiftData? How would these two data stores coexist if the user later grants health access?
Replies
Boosts
Views
Activity
Apr ’25
SwiftData data crashes with @Relationship
I've noticed that SwiftData's @Relationship seems to potentially cause application crashes. The crash error is shown in the image. Since this crash appears to be random and I cannot reproduce it under specific circumstances, I can only temporarily highlight that this issue seems to exist. @Model final class TrainInfo { @Relationship(deleteRule: .cascade, inverse: StopStation.trainInfo) var stations: [StopStation]? } @Model final class StopStation { @Relationship var trainInfo: TrainInfo? } /// some View var origin: StopStationDisplayable? { if let train = train as? TrainInfo { return train.stations?.first(where: { $0.isOrigin }) ?? train.stations?.first(where: { $0.isStarting }) } return nil } // Some other function or property func someFunction() { if let origin, let destination { // Function implementation } }
Replies
1
Boosts
0
Views
154
Activity
Apr ’25
Reply to Xcode? Hacking? Iphone11
hello, first my disclaimer...I am very new to Apple products and my knowledge base is minimal. That said, I have immersed myself in an effort to learn all I possibly can about all topics related to being hacked, because I have also been invaded. i will summarize my experiences and offer a nugget or two of gathered wisdom. i believe the invasion (access to my device) was not physical, and i think it began through using information held only by my provider. **Incidentally, in a separate though related research project, and with info gathered specific to corporate ownership, parent-child relationships, patterns found in company formation/agent for process data/physical addresses offered publicly through registration documentation, and probably more that I do not recall right now...I think I know the person who owns, or has financial control of, my cellular carrier. I will add that this person owns and controls many nationally known companies but disconnects himself by using proxy synthetic identities to
Replies
Boosts
Views
Activity
Apr ’25