Search results for

“SwiftData inheritance relationship”

4,982 results found

Post

Replies

Boosts

Views

Activity

Reply to iOS 18.1 and SeiftData
The solution to this was that prior to iOS 18 if you had a one to many relationship in your SwiftData models that adding to the array in the one data model entity would automatically update the many data item so that its optional attribute that pointed back to the data item containing the array of many items when you added items to the array. In iOS 18 now this is no longer true and if you don't set that optional attribute to explicitly point to the data model that will contain the array of many items then you will see the aforementioned error when you try to append the new item to that array. Hopefully this makes sense and will help someone down the road.
Aug ’24
How to correctly fetch data using SwiftData
Hi there! I'm making an app that stores data for the user's profile in SwiftData. I was originally going to use UserDefaults but I thought SwiftData could save Images natively but this is not true so I really could switch back to UserDefaults and save images as Data but I'd like to try to get this to work first. So essentially I have textfields and I save the values of them through a class allProfileData. Here's the code for that: import SwiftData import SwiftUI @Model class allProfileData { var profileImageData: Data? var email: String var bio: String var username: String var profileImage: Image { if let data = profileImageData, let uiImage = UIImage(data: data) { return Image(uiImage: uiImage) } else { return Image(DefaultProfile) } } init(email:String, profileImageData: Data?, bio: String, username:String) { self.profileImageData = profileImageData self.email = email self.bio = bio self.username = username } } To save this I create a new class (I think, I'm new) and save it throu
7
0
339
Nov ’25
SwiftData with two Stores
Hi, has anybody managed to get two sqlite stores working? If I define the stores with a configuration for each it seems like that only the first configuration and and therefore the store is recognised. This is how I define the configuration and container: import SwiftData @main struct SwiftDataTestApp: App { var modelContainer: ModelContainer init() { let fullSchema = Schema([ SetModel.self, NewsModel.self ]) let setConfiguration = ModelConfiguration( setconfig, schema: Schema([SetModel.self]), url: FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent(Sets.sqlite), readOnly: false) let newsConfiguration = ModelConfiguration( newsconfig, schema: Schema([NewsModel.self]), url: FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!.appendingPathComponent(News.sqlite), readOnly: false) modelContainer = try! ModelContainer(for: fullSchema, configurations: [setConfiguration,newsConfiguration]) } var body: some Scene { WindowGroup { Conten
5
0
3.5k
Aug ’23
Equivalent of NSManagedObjectContext.existingObject in SwiftData?
CoreData contexts expose a very convenient existingObject API that allows to fallback to retrieve from the persistent store in case the object is not registered in the context yet. I don't see any equivalent in SwiftData. Does that mean we should fallback to fetch methods instead? Is there any rationale behind the lack of this API other than that it's still in development. For more details about what I'm trying to do. I have a read-only context (the main context) and a read-write context. After inserting a model in the read-write context (and saving), I'd like to retrieve the equivalent object from the read-only context.
1
0
705
Jun ’23
SwiftData equivalent of NSFetchedResultsController
As far as I can tell, there’s no equivalent to Core Data’s NSFetchedResultsController in SwiftData. It would be very helpful to have a way to respond to sets of changes in a ModelContext outside of a SwiftUI view. For instance, this is very helpful when using a Model-View-ViewModel architecture. The @Query property wrapper is great for use in a SwiftUI view, but sometimes it’s helpful to process data outside of the view itself. The fetch() method on ModelContext is helpful for one-time operations, but as far as I can't tell it doesn't address receiving changes on an ongoing basis. Am I missing some equivalent for this use case? Also filed as feedback FB12288916
2
0
2.2k
Jun ’23
Is it possible to track history using HistoryDescriptor in SwiftData?
Is it possible to track history using the new HistoryDescriptor feature in SwiftData? Or can I only get the current most recent data? Or is it possible to output the changed data itself, along with timestamps? I am hoping that it is possible to track by a standard feature like NSPersistentHistoryTransaction in CoreData. Do we still have to use a method in SwiftData that creates more tracking data itself?
4
0
1.3k
Sep ’24
Reply to Protocol-oriented architecture
I'm not sure if I understood correctly. So both ItemCollection and PointCollection should inherit from another protocol that does not have a Self or associated type constraints? So does the same apply to the Match protocol if I want to use it from match controller? It can't have Self or associated type constraints?var match: Match = TennisMatch(participants: [player1, player2) //Error now because Match inherits from ItemCollection
Topic: Programming Languages SubTopic: Swift Tags:
Jul ’15
ShieldConfigurationExtension & SwiftData
Hi, I am developing a Screen Time App and I am having issues with the ShieldConfigurationExtension (ShieldConfigurationDataSource). I know this extensions is sandboxed but I should be able to read data from the main app. I am using SwiftData as my database, but I am unable to initialize it in the extensions with an error indicating insufficient file permissions. I have App Group set up and I am able to share data using UserDefaults but that is just inconvenient. Is there any way I could just open the SwiftData in read only mode so that I could display the user some info on the shield? SwiftData Init: private func setupContainer() throws { let schema = Schema([ DogEntity.self, HouseEntity.self ]) // Use app group container if available let config: ModelConfiguration if let containerURL = FileManager.default.containerURL( forSecurityApplicationGroupIdentifier: group.(Bundle.app.bundleIdentifier ?? ) ) { config = ModelConfiguration(schema: schema, url: containerURL.appendingPathCompone
1
0
195
May ’25
Share Extension Lifetime and SwiftData
I have an app that uses a Share Extension that allows the user to share videos, from Files and Photos etc., the video URL and some related data is then persisted with SwiftData and synchronized with CloudKit. This code has worked consistently for a long time although recently, with iOS 26 and recent builds of iOS 18, I have observed that the video is either not saved to SwiftData (iOS 26.0), or available locally when the app is opened on the same device where the share occurred, but not synchronized to other devices (iOS 18.7 and iOS 26.1 beta). Assuming the video is opened locally after being shared into the app, it is typically synchronized with CloudKit to other devices although it's not as reliable as it should be. Is there a reliable approach in the Share Extension to ensure that the data is saved to the local SwiftData database and then synchronized with CloudKit. I suspect it could be that the lifetime of the Share Extension has become even more constrained in recent OS updat
1
0
194
Oct ’25
CloudKit integration requires does not support ordered relationships.
I'm trying to use Apple's CoreDataCloudkitDemo app. I've only changed the app settings per the README document. On running the demo, I'm getting the error: CloudKit integration requires does not support ordered relationships.The console log shows:Fatal error: ###persistentContainer: Failed to load persistent stores:Error Domain=NSCocoaErrorDomain Code=134060 A Core Data error occurred. UserInfo={NSLocalizedFailureReason=CloudKit integration requires does not support ordered relationships. The following relationships are marked ordered: Post: attachmentsDoes anyone have a workaround, or preferably, a fix? Or did I do something wrong?Thanks
6
0
5k
Jul ’19
SwiftData And Non Simple Data Types
I have an SwiftUI app that uses CloudKit. I have model classes and I read and write data to CloudKit. I wanted to convert the app to use SwiftData. Here is a sample model class. @Model class User { var firstName: String? var lastName: String? var location: CLLocation? var photo: Image? } When I read and write this to CloudKit the variables map like this: String -> String CLLocation -> LOCATION Image -> ASSET When I try to compile the above class, I get these error for CLLocation and Image. Instance method 'setValue(forKey:to:)' requires that 'CLLocation' conform to 'PersistentModel' Instance method 'setValue(forKey:to:)' requires that 'Image' conform to 'PersistentModel' How can I use variable types such as CLLocation and Image with SwiftData?
0
0
709
Oct ’23
Reply to Custom images/dots in UIPageControl
You can only change UIPageControl's indicator tint color. I would make a custom class inherited from the UIControl.
Topic: UI Frameworks SubTopic: UIKit Tags:
Replies
Boosts
Views
Activity
Oct ’15
Reply to Why after pod install throw that error?
I had a $(inherited) but recursive, just change it to non-recursive project target -> build settings -> framework_search_path
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Jun ’21
Reply to iOS 18.1 and SeiftData
The solution to this was that prior to iOS 18 if you had a one to many relationship in your SwiftData models that adding to the array in the one data model entity would automatically update the many data item so that its optional attribute that pointed back to the data item containing the array of many items when you added items to the array. In iOS 18 now this is no longer true and if you don't set that optional attribute to explicitly point to the data model that will contain the array of many items then you will see the aforementioned error when you try to append the new item to that array. Hopefully this makes sense and will help someone down the road.
Replies
Boosts
Views
Activity
Aug ’24
How to correctly fetch data using SwiftData
Hi there! I'm making an app that stores data for the user's profile in SwiftData. I was originally going to use UserDefaults but I thought SwiftData could save Images natively but this is not true so I really could switch back to UserDefaults and save images as Data but I'd like to try to get this to work first. So essentially I have textfields and I save the values of them through a class allProfileData. Here's the code for that: import SwiftData import SwiftUI @Model class allProfileData { var profileImageData: Data? var email: String var bio: String var username: String var profileImage: Image { if let data = profileImageData, let uiImage = UIImage(data: data) { return Image(uiImage: uiImage) } else { return Image(DefaultProfile) } } init(email:String, profileImageData: Data?, bio: String, username:String) { self.profileImageData = profileImageData self.email = email self.bio = bio self.username = username } } To save this I create a new class (I think, I'm new) and save it throu
Replies
7
Boosts
0
Views
339
Activity
Nov ’25
Reply to UIConfigurationStateCustomKey with UIContentView.
Thanks for the answer! I am using UICollectionView.CellRegistration. So seems like I need to inherit from UICollectionViewListCell with code from documentation.
Topic: UI Frameworks SubTopic: UIKit Tags:
Replies
Boosts
Views
Activity
Dec ’21
Reply to How do we know which CNContainer represents iCloud?
Maybe iCloud inherits the traditional title of AddressBook syncing service since MobileMe (or .Mac?).
Topic: UI Frameworks SubTopic: AppKit Tags:
Replies
Boosts
Views
Activity
Nov ’15
SwiftData with two Stores
Hi, has anybody managed to get two sqlite stores working? If I define the stores with a configuration for each it seems like that only the first configuration and and therefore the store is recognised. This is how I define the configuration and container: import SwiftData @main struct SwiftDataTestApp: App { var modelContainer: ModelContainer init() { let fullSchema = Schema([ SetModel.self, NewsModel.self ]) let setConfiguration = ModelConfiguration( setconfig, schema: Schema([SetModel.self]), url: FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent(Sets.sqlite), readOnly: false) let newsConfiguration = ModelConfiguration( newsconfig, schema: Schema([NewsModel.self]), url: FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!.appendingPathComponent(News.sqlite), readOnly: false) modelContainer = try! ModelContainer(for: fullSchema, configurations: [setConfiguration,newsConfiguration]) } var body: some Scene { WindowGroup { Conten
Replies
5
Boosts
0
Views
3.5k
Activity
Aug ’23
Equivalent of NSManagedObjectContext.existingObject in SwiftData?
CoreData contexts expose a very convenient existingObject API that allows to fallback to retrieve from the persistent store in case the object is not registered in the context yet. I don't see any equivalent in SwiftData. Does that mean we should fallback to fetch methods instead? Is there any rationale behind the lack of this API other than that it's still in development. For more details about what I'm trying to do. I have a read-only context (the main context) and a read-write context. After inserting a model in the read-write context (and saving), I'd like to retrieve the equivalent object from the read-only context.
Replies
1
Boosts
0
Views
705
Activity
Jun ’23
SwiftData equivalent of NSFetchedResultsController
As far as I can tell, there’s no equivalent to Core Data’s NSFetchedResultsController in SwiftData. It would be very helpful to have a way to respond to sets of changes in a ModelContext outside of a SwiftUI view. For instance, this is very helpful when using a Model-View-ViewModel architecture. The @Query property wrapper is great for use in a SwiftUI view, but sometimes it’s helpful to process data outside of the view itself. The fetch() method on ModelContext is helpful for one-time operations, but as far as I can't tell it doesn't address receiving changes on an ongoing basis. Am I missing some equivalent for this use case? Also filed as feedback FB12288916
Replies
2
Boosts
0
Views
2.2k
Activity
Jun ’23
Is it possible to track history using HistoryDescriptor in SwiftData?
Is it possible to track history using the new HistoryDescriptor feature in SwiftData? Or can I only get the current most recent data? Or is it possible to output the changed data itself, along with timestamps? I am hoping that it is possible to track by a standard feature like NSPersistentHistoryTransaction in CoreData. Do we still have to use a method in SwiftData that creates more tracking data itself?
Replies
4
Boosts
0
Views
1.3k
Activity
Sep ’24
Reply to Protocol-oriented architecture
I'm not sure if I understood correctly. So both ItemCollection and PointCollection should inherit from another protocol that does not have a Self or associated type constraints? So does the same apply to the Match protocol if I want to use it from match controller? It can't have Self or associated type constraints?var match: Match = TennisMatch(participants: [player1, player2) //Error now because Match inherits from ItemCollection
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Jul ’15
ShieldConfigurationExtension & SwiftData
Hi, I am developing a Screen Time App and I am having issues with the ShieldConfigurationExtension (ShieldConfigurationDataSource). I know this extensions is sandboxed but I should be able to read data from the main app. I am using SwiftData as my database, but I am unable to initialize it in the extensions with an error indicating insufficient file permissions. I have App Group set up and I am able to share data using UserDefaults but that is just inconvenient. Is there any way I could just open the SwiftData in read only mode so that I could display the user some info on the shield? SwiftData Init: private func setupContainer() throws { let schema = Schema([ DogEntity.self, HouseEntity.self ]) // Use app group container if available let config: ModelConfiguration if let containerURL = FileManager.default.containerURL( forSecurityApplicationGroupIdentifier: group.(Bundle.app.bundleIdentifier ?? ) ) { config = ModelConfiguration(schema: schema, url: containerURL.appendingPathCompone
Replies
1
Boosts
0
Views
195
Activity
May ’25
Share Extension Lifetime and SwiftData
I have an app that uses a Share Extension that allows the user to share videos, from Files and Photos etc., the video URL and some related data is then persisted with SwiftData and synchronized with CloudKit. This code has worked consistently for a long time although recently, with iOS 26 and recent builds of iOS 18, I have observed that the video is either not saved to SwiftData (iOS 26.0), or available locally when the app is opened on the same device where the share occurred, but not synchronized to other devices (iOS 18.7 and iOS 26.1 beta). Assuming the video is opened locally after being shared into the app, it is typically synchronized with CloudKit to other devices although it's not as reliable as it should be. Is there a reliable approach in the Share Extension to ensure that the data is saved to the local SwiftData database and then synchronized with CloudKit. I suspect it could be that the lifetime of the Share Extension has become even more constrained in recent OS updat
Replies
1
Boosts
0
Views
194
Activity
Oct ’25
CloudKit integration requires does not support ordered relationships.
I'm trying to use Apple's CoreDataCloudkitDemo app. I've only changed the app settings per the README document. On running the demo, I'm getting the error: CloudKit integration requires does not support ordered relationships.The console log shows:Fatal error: ###persistentContainer: Failed to load persistent stores:Error Domain=NSCocoaErrorDomain Code=134060 A Core Data error occurred. UserInfo={NSLocalizedFailureReason=CloudKit integration requires does not support ordered relationships. The following relationships are marked ordered: Post: attachmentsDoes anyone have a workaround, or preferably, a fix? Or did I do something wrong?Thanks
Replies
6
Boosts
0
Views
5k
Activity
Jul ’19
SwiftData And Non Simple Data Types
I have an SwiftUI app that uses CloudKit. I have model classes and I read and write data to CloudKit. I wanted to convert the app to use SwiftData. Here is a sample model class. @Model class User { var firstName: String? var lastName: String? var location: CLLocation? var photo: Image? } When I read and write this to CloudKit the variables map like this: String -> String CLLocation -> LOCATION Image -> ASSET When I try to compile the above class, I get these error for CLLocation and Image. Instance method 'setValue(forKey:to:)' requires that 'CLLocation' conform to 'PersistentModel' Instance method 'setValue(forKey:to:)' requires that 'Image' conform to 'PersistentModel' How can I use variable types such as CLLocation and Image with SwiftData?
Replies
0
Boosts
0
Views
709
Activity
Oct ’23