Search results for

“SwiftData inheritance relationship”

4,982 results found

Post

Replies

Boosts

Views

Activity

Data Protection and SwiftData Containers
SwiftData ModelContainer instances don't seem to have a value for setting the Data Protection class. Is the best way to set that by setting the Data Protection in the app capabilities? Is that the only way? I have a need for log data that would be Complete unless open and user data that would be Complete, but how do I change one of the containers data protection class?
3
0
924
Jan ’25
Int128 fail in @Model with SwiftData
Swift recently added support for Int128. However, they do need NOT seem to be supported in SwiftData. Now totally possible I'm doing something wrong too. I have the project set to macOS 15 to use a UInt128 in @Model class as attribute. I tried using a clean Xcode project with Swift Data choosen in the macOS app wizard. Everything compiles, but it fails at runtime in both my app and Xcode default SwiftData: SwiftData/SchemaProperty.swift:380: Fatal error: Unexpected property within Persisted Struct/Enum: Builtin.Int128 with the only modification to from stock is: @Model final class Item { var timestamp: Date var ipv6: UInt128 init(timestamp: Date) { self.timestamp = timestamp self.ipv6 = 0 } } I have tried both Int128 and UInt128. Both fails exactly the same. In fact, so exactly, when using UInt128 it still show a Int128 in error message, despite class member being UInt128 . My underlying need is to store an IPv6 addresses with an app, so the newer UInt128 would work to persist it. S
7
0
583
Feb ’25
Use CoreData alongside SwiftData for the "Sharing" feature in the app.
Hello! 😊 I currently manage an app called MoneyKeeper that uses SwiftData for its data storage framework. Many users have requested a sharing feature, but unfortunately, SwiftData does not yet support this functionality, and it’s unclear when it will. 😭 Initially, I considered using CloudKit and CKSyncEngine to implement quick synchronization and sharing. However, due to the complexity of the current data model’s relationships, modeling it with CloudKit’s schema alone seemed overly complicated and likely to introduce bugs. As a result, I’ve decided to implement the sharing feature using CoreData and CloudKit. My plan to avoid conflicts with SwiftData includes: Keeping the local storage locations for SwiftData and CoreData separate. Using entirely separate CloudKit containers for SwiftData and CoreData. I believe these measures will minimize potential issues, but I’m wondering if there’s anything else I should consider. Using both SwiftData (for
1
0
964
Dec ’24
Reply to Find available TCP port
Ideally I'd use Pipe but the file descriptors doesn't seem to be inherited by the child process that is spawned with Process. That’s correct. Under the covers Process calls posix_spawn with the POSIX_SPAWN_CLOEXEC_DEFAULT flag, meaning that file descriptors are not inherited by default. It then explicitly sets up inheritance for std{in,out,err}. I'd assume it would be possible if I used posix_spawn and friends Correct. but I'd prefer to stay with the higher-level APIs Wouldn’t we all (-: Share and Enjoy — Quinn “The Eskimo!” @ Developer Technical Support @ Apple let myEmail = eskimo + 1 + @ + apple.com
Topic: Programming Languages SubTopic: Swift Tags:
Jan ’23
How to observe SwiftData DB changes
How can one observe changes in the SwiftData DB? I'm aware that this is possible via Queries, but I need to fetch the data in background, so a query is not an option. I'm aware of the ModelContext.didSave / .willSave notifications, but these don't work with iOS 17. -> How can I observe changes to models of a certain type? I don't want to observe the whole database.
0
0
896
Aug ’24
Core Data foreign key/relationship between two tables
hi, this is a very simple concept that I can't seem to solve using relationships with core data between two tables. for instance, I have a client table with a name field that I want to link to a client details table with a clientName field and when I update/edit the client name, this still stays links to the client details record. make sense? I tried relationships but it does not link up when I edit the client name. I feel like I'm missing something very obvious here and have tried multiple scenarios but I'm not finding a solution nor am I finding any documentation on how this should work. any help/ideas is appreciated. Thank you, Pete
0
0
616
Aug ’23
CoreData + CloudKit -- Many-to-Many Relationship not Syncing
In an iOS App that uses CKShare I have a many-to-many relationship that does not consistently sync between the share's N participants. The relationship is between Group and Player as group.players and player.groups. As an example, given 3 group each with 4 players (aka 4:4:4), some devices show CoreData (it is NOT a UI issue) with 4:2:3 or 3:4:4. (A deletion of CoreData from a device, forcing a full re-sync from CloudKit, seems to populate the group:player relationships consistently; but obviously that is impractical to resolving the issue). How do I avoid these sync-from-CloudKit inconsistencies? Note: AI agents generally suggest adding a CoreData 'join' entity - such as 'GroupPlayer'. Is that THE fix?
1
0
74
1w
Custom struct Codable for SwiftData
I'm encountering an issue encoding/decoding a custom struct from SwiftData. As it's all happening behind the generated code of SwiftData and a decoder, I'm not really sure what's going on. I have a custom type defined kind of like this: public struct Group { private var elementGroups: [Element: Key] private var groupedElements: [Key: [Element]] } In short, it allows multiple elements (usually a string), to be grouped, referenced by some key. I have Codable conformance to this object, so I can encode and decode it. For simplicity, the elementGroups is encoded/decoded, and the groupedElements is rebuilt when decoding. My implementation is similar to this: extension Group: Codable where Key: Codable, Element: Codable { private enum Keys: CodingKey { case groups } public init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: Keys.self) let decoded = try container.decode([Element: Key].self, forKey: .groups) // Enumerate over the element groups, and populate the list
4
0
1.9k
Oct ’23
Import sqlite database to SwiftData
Hello, I recently switched my app from flutter which was using sqlite3 database. I would like to migrate my existing users to SwiftData. The issue is I do not know where to start in finding the database for the current users, reading the data and inserting into the SwiftData schema. I tried searching but not clear on how to proceed. Any help pointing in the right direction would be great. Thanks.
1
0
447
Nov ’24
Compiler Incorrectly Warns when declaring a delegate property that conforms to a protocol which inherits from a protocol
So, I have a subclass of NSTableView. I extend NSTableViewDelegate. My interface looks something like this:@protocol MyTableViewDelegate; @interface MyTableView : NSTableView @property (nullable,weak) id <MyTableViewDelegate> delegate; @end @protocol MyTableViewDelegate <NSTableViewDelegate> //Added methods here... @endNow this always would warn, but not if you used @dynamic to acknowledge intent:@implementation MyTableView @dynamic delegate; @endBut now the warning is always present in Xcode 8
1
0
231
Sep ’16
SwiftData won't work with Measurement types
I'm trying out SwiftData and converting my existing data models using Structs and Codable to Classes and SwiftData. I've got a model that has a Measurement type. When I run the app after converting everything to use classes with the @Model macro, I get a fatal error: SwiftData/SchemaProperty.swift:325: Fatal error: Unexpected type for CompositeAttribute: NSUnitMass. Since Measurement conforms to Codable, and SwiftData should work with Codable types, why is this not working? I also tried marking the property with @Attribute(.transformable) and it didn't make a difference.
11
0
4.6k
Jun ’23
SwiftData Predicate Issue
I'm in the process of converting one of my apps to SwiftData. I converted the other ones over. This app has a much more involved Schema, but SwiftData seems to handle it well. My issue is when I try to fetch data based on a. #Predicate. Here's my code. let predicate = #Predicate { data in data.result == result && data.variable!.starts(with: SomeString) } let sortBy = [SortDescriptor(.result] let descriptor = FetchDescriptor(predicate: predicate, sortBy: sortBy) if let theData = try? context?.fetch(descriptor) { The if let at the bottom fails. Note that 'variable' is optional while result is not. I have to use ! in the predicate since it's a bool that is returned. If my predicate were just data.result == result, then all is well. I can then check the received data for the second condition in the predicate just fine. It just doesn't work inside of the Predicate. The same is true if I used contains. Anybody else having this issue? I'm using Xcode 15.0 beta 6.
3
0
2.1k
Aug ’23
SwiftData know how specific information
Re SwiftData: is my understanding correct : generally speaking and by default insert method inserts objects into the context and context automatically persist - e.g. inserts them into container while the delete method does not - it only deletes from context and context does not delete them from the container unless save is called ? It is not clear from the documentation nor from the definitions : public func delete(model: T.Type, where predicate: Predicate? = nil, includeSubclasses: Bool = true) throws where T : PersistentModel //How can I test it ? I’m keen to learn where I can confirm this in Apple’s documentation or official articles, code definitions, apart from experimenting or consulting third-party materials. Where does it explicitly state that SwiftData includes an automatic saving feature but does not offer automatic deletion? Meet SwiftData (WWDC23): Around the 14:30 mark, Apple mentions that SwiftData automatically saves changes at opportune moments. But nothing
1
0
500
Oct ’24
Reply to SwiftData crash on adding sort argument to Query
A similar issue was discussed in this thread. Your case has another layer of complexity - inheritance. Would you mind to have a check if your issue still happens without using inheritance? Also, would you mind to share the OS and SDK version you tested with? Thanks. Best, —— Ziqiao Chen  Worldwide Developer Relations.
Replies
Boosts
Views
Activity
Jan ’26
Data Protection and SwiftData Containers
SwiftData ModelContainer instances don't seem to have a value for setting the Data Protection class. Is the best way to set that by setting the Data Protection in the app capabilities? Is that the only way? I have a need for log data that would be Complete unless open and user data that would be Complete, but how do I change one of the containers data protection class?
Replies
3
Boosts
0
Views
924
Activity
Jan ’25
Int128 fail in @Model with SwiftData
Swift recently added support for Int128. However, they do need NOT seem to be supported in SwiftData. Now totally possible I'm doing something wrong too. I have the project set to macOS 15 to use a UInt128 in @Model class as attribute. I tried using a clean Xcode project with Swift Data choosen in the macOS app wizard. Everything compiles, but it fails at runtime in both my app and Xcode default SwiftData: SwiftData/SchemaProperty.swift:380: Fatal error: Unexpected property within Persisted Struct/Enum: Builtin.Int128 with the only modification to from stock is: @Model final class Item { var timestamp: Date var ipv6: UInt128 init(timestamp: Date) { self.timestamp = timestamp self.ipv6 = 0 } } I have tried both Int128 and UInt128. Both fails exactly the same. In fact, so exactly, when using UInt128 it still show a Int128 in error message, despite class member being UInt128 . My underlying need is to store an IPv6 addresses with an app, so the newer UInt128 would work to persist it. S
Replies
7
Boosts
0
Views
583
Activity
Feb ’25
Use CoreData alongside SwiftData for the "Sharing" feature in the app.
Hello! 😊 I currently manage an app called MoneyKeeper that uses SwiftData for its data storage framework. Many users have requested a sharing feature, but unfortunately, SwiftData does not yet support this functionality, and it’s unclear when it will. 😭 Initially, I considered using CloudKit and CKSyncEngine to implement quick synchronization and sharing. However, due to the complexity of the current data model’s relationships, modeling it with CloudKit’s schema alone seemed overly complicated and likely to introduce bugs. As a result, I’ve decided to implement the sharing feature using CoreData and CloudKit. My plan to avoid conflicts with SwiftData includes: Keeping the local storage locations for SwiftData and CoreData separate. Using entirely separate CloudKit containers for SwiftData and CoreData. I believe these measures will minimize potential issues, but I’m wondering if there’s anything else I should consider. Using both SwiftData (for
Replies
1
Boosts
0
Views
964
Activity
Dec ’24
Reply to Find available TCP port
Ideally I'd use Pipe but the file descriptors doesn't seem to be inherited by the child process that is spawned with Process. That’s correct. Under the covers Process calls posix_spawn with the POSIX_SPAWN_CLOEXEC_DEFAULT flag, meaning that file descriptors are not inherited by default. It then explicitly sets up inheritance for std{in,out,err}. I'd assume it would be possible if I used posix_spawn and friends Correct. but I'd prefer to stay with the higher-level APIs Wouldn’t we all (-: Share and Enjoy — Quinn “The Eskimo!” @ Developer Technical Support @ Apple let myEmail = eskimo + 1 + @ + apple.com
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Jan ’23
How to observe SwiftData DB changes
How can one observe changes in the SwiftData DB? I'm aware that this is possible via Queries, but I need to fetch the data in background, so a query is not an option. I'm aware of the ModelContext.didSave / .willSave notifications, but these don't work with iOS 17. -> How can I observe changes to models of a certain type? I don't want to observe the whole database.
Replies
0
Boosts
0
Views
896
Activity
Aug ’24
Core Data foreign key/relationship between two tables
hi, this is a very simple concept that I can't seem to solve using relationships with core data between two tables. for instance, I have a client table with a name field that I want to link to a client details table with a clientName field and when I update/edit the client name, this still stays links to the client details record. make sense? I tried relationships but it does not link up when I edit the client name. I feel like I'm missing something very obvious here and have tried multiple scenarios but I'm not finding a solution nor am I finding any documentation on how this should work. any help/ideas is appreciated. Thank you, Pete
Replies
0
Boosts
0
Views
616
Activity
Aug ’23
CoreData + CloudKit -- Many-to-Many Relationship not Syncing
In an iOS App that uses CKShare I have a many-to-many relationship that does not consistently sync between the share's N participants. The relationship is between Group and Player as group.players and player.groups. As an example, given 3 group each with 4 players (aka 4:4:4), some devices show CoreData (it is NOT a UI issue) with 4:2:3 or 3:4:4. (A deletion of CoreData from a device, forcing a full re-sync from CloudKit, seems to populate the group:player relationships consistently; but obviously that is impractical to resolving the issue). How do I avoid these sync-from-CloudKit inconsistencies? Note: AI agents generally suggest adding a CoreData 'join' entity - such as 'GroupPlayer'. Is that THE fix?
Replies
1
Boosts
0
Views
74
Activity
1w
Custom struct Codable for SwiftData
I'm encountering an issue encoding/decoding a custom struct from SwiftData. As it's all happening behind the generated code of SwiftData and a decoder, I'm not really sure what's going on. I have a custom type defined kind of like this: public struct Group { private var elementGroups: [Element: Key] private var groupedElements: [Key: [Element]] } In short, it allows multiple elements (usually a string), to be grouped, referenced by some key. I have Codable conformance to this object, so I can encode and decode it. For simplicity, the elementGroups is encoded/decoded, and the groupedElements is rebuilt when decoding. My implementation is similar to this: extension Group: Codable where Key: Codable, Element: Codable { private enum Keys: CodingKey { case groups } public init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: Keys.self) let decoded = try container.decode([Element: Key].self, forKey: .groups) // Enumerate over the element groups, and populate the list
Replies
4
Boosts
0
Views
1.9k
Activity
Oct ’23
Import sqlite database to SwiftData
Hello, I recently switched my app from flutter which was using sqlite3 database. I would like to migrate my existing users to SwiftData. The issue is I do not know where to start in finding the database for the current users, reading the data and inserting into the SwiftData schema. I tried searching but not clear on how to proceed. Any help pointing in the right direction would be great. Thanks.
Replies
1
Boosts
0
Views
447
Activity
Nov ’24
Compiler Incorrectly Warns when declaring a delegate property that conforms to a protocol which inherits from a protocol
So, I have a subclass of NSTableView. I extend NSTableViewDelegate. My interface looks something like this:@protocol MyTableViewDelegate; @interface MyTableView : NSTableView @property (nullable,weak) id <MyTableViewDelegate> delegate; @end @protocol MyTableViewDelegate <NSTableViewDelegate> //Added methods here... @endNow this always would warn, but not if you used @dynamic to acknowledge intent:@implementation MyTableView @dynamic delegate; @endBut now the warning is always present in Xcode 8
Replies
1
Boosts
0
Views
231
Activity
Sep ’16
SwiftData won't work with Measurement types
I'm trying out SwiftData and converting my existing data models using Structs and Codable to Classes and SwiftData. I've got a model that has a Measurement type. When I run the app after converting everything to use classes with the @Model macro, I get a fatal error: SwiftData/SchemaProperty.swift:325: Fatal error: Unexpected type for CompositeAttribute: NSUnitMass. Since Measurement conforms to Codable, and SwiftData should work with Codable types, why is this not working? I also tried marking the property with @Attribute(.transformable) and it didn't make a difference.
Replies
11
Boosts
0
Views
4.6k
Activity
Jun ’23
SwiftData Predicate Issue
I'm in the process of converting one of my apps to SwiftData. I converted the other ones over. This app has a much more involved Schema, but SwiftData seems to handle it well. My issue is when I try to fetch data based on a. #Predicate. Here's my code. let predicate = #Predicate { data in data.result == result && data.variable!.starts(with: SomeString) } let sortBy = [SortDescriptor(.result] let descriptor = FetchDescriptor(predicate: predicate, sortBy: sortBy) if let theData = try? context?.fetch(descriptor) { The if let at the bottom fails. Note that 'variable' is optional while result is not. I have to use ! in the predicate since it's a bool that is returned. If my predicate were just data.result == result, then all is well. I can then check the received data for the second condition in the predicate just fine. It just doesn't work inside of the Predicate. The same is true if I used contains. Anybody else having this issue? I'm using Xcode 15.0 beta 6.
Replies
3
Boosts
0
Views
2.1k
Activity
Aug ’23
SwiftData know how specific information
Re SwiftData: is my understanding correct : generally speaking and by default insert method inserts objects into the context and context automatically persist - e.g. inserts them into container while the delete method does not - it only deletes from context and context does not delete them from the container unless save is called ? It is not clear from the documentation nor from the definitions : public func delete(model: T.Type, where predicate: Predicate? = nil, includeSubclasses: Bool = true) throws where T : PersistentModel //How can I test it ? I’m keen to learn where I can confirm this in Apple’s documentation or official articles, code definitions, apart from experimenting or consulting third-party materials. Where does it explicitly state that SwiftData includes an automatic saving feature but does not offer automatic deletion? Meet SwiftData (WWDC23): Around the 14:30 mark, Apple mentions that SwiftData automatically saves changes at opportune moments. But nothing
Replies
1
Boosts
0
Views
500
Activity
Oct ’24
Relationship rules for Core Data batch deletes
This year's WWDC announced that relationship rules would be observed when performing a batch delete in Core Data. I've seen conflicting information on whether or not this has been the case in years past, and would appreciate a confirmation. Is this only available on > iOS 14? Thanks.
Replies
0
Boosts
0
Views
337
Activity
Jul ’20