Search results for

“SwiftData inheritance relationship”

4,980 results found

Post

Replies

Boosts

Views

Activity

one to many relationship
I am trying to get my head around a problem involving a one to many relationship. I'm not sure that my approach to this design is a good one.I have two entities, address and rooms. An address can have many rooms. I have set up a one to many relationship between the address table and room table. The problem is differentiating the Living Room of address 1 from the Living Room of address 2. Currently when I call NSManagedObject, the table rows are displaying all the rooms regardless of the address being chosen. I am not sure how to uniquely identify a room with an address.Sorry this is so vague, but I'm pretty new to core data, and I'm not even sure if my approach is a valid one. Any help to get me pointe in the right direction would be greatly appreciated.
2
0
349
Jun ’15
SwiftData .returnsDistinctResults ?
With NSFetchRequest of coreData it was possible to only return distinct results from the database with .returnsDistinctResults What if any is the fetch equivalent in SwiftData ? currently I'm appending entries into a Set. Only with over 200k rows and no newBackgroundContext() in SwiftData the UI is locked for a number of seconds
1
0
823
Oct ’23
How to save a singleton using SwiftData?
I am persisting a tree data structure Node using SwiftData with CloudKit enabled. I understand how to manage my NonSingleton models in SwiftUI. However, when a user first launches my app a Node model instance needs to be created that is separate from any NonSingleton model. So I think a Singleton class is appropriate but I don't know how to persist it? @Model final class Node { var parent: Node? @Relationship(deleteRule: .cascade, inverse: Node.parent) var children = [Node]() // ... } @Model final class NonSingleton { @Relationship(deleteRule: .cascade) var node: Node // ... } @Model // ? final class Singleton { static var node = Node() private init() {} }
1
0
997
Apr ’24
Is it possible to create relationships between objects in previews?
Is it possible to create one-to-many relationships between optional objects in SwiftData Xcode Previews? The below code I call from #Preview crashes the canvas on the post.authors?.append(tempAuthors.randomElement()!) when I try to append an array to an optional array. I can append the previewAuthors array to the authors array in the simulator or on device, but it crashes in Preview. My running theory is it has something to do with being in memory since this works on device, but I'm not sure that I missed something super obvious. static func previews(_ count: Int) -> [Post] { let previewAuthors = [Author(name: ...), Author(name: ...)] var posts: [Post] = [] for i in 0..
2
0
1k
Oct ’23
Reply to SwiftData: "Illegal attempt to establish a relationship 'item' between objects in different contexts
It's because all SwiftData relationships must be optional, at least at this time and in my experience thus far. I've received this error quite a bit. Changing your code to the below should resolve your issue, I'd be curious if not class MyItem { var name: String var item: Item? init(name: String, item: Item? = nil) { self.name = name self.item = item } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Nov ’23
Reply to SwiftData errors when trying to insert multiple objects
Some ideas: Verify there isn't already an item with id: 1 when re-running mock code Verify anything with @Attribute(.unique) is ACTUALLY unique in mock code Make sure that any models that have a @Relationship have a corresponding deleteRule, if you're running a series of delete(...) elsewhere. The first one caught me off guard -- if running a series of delete calls on PersistentModel types, make sure that any models that have a @Relationship also have a corresponding deleteRule. When deleting from either end, SwiftData can null out the child in its parent (.nullify), or delete its parent altogether (.cascade). Without this, a hanging reference will be left, causing null value errors. For example: @Model struct Aisle { @Relationship(deleteRule: .nullify, inverse: Item.aisle) var items: [Item] } @Model struct Item { @Relationship(deleteRule: .nullify) // works! var aisle: Aisle } Also, save the model context before running deletes. Otherwise, changes will be flushed
Feb ’24
Appintent and swiftdata
Hi, I'm new to Swift development and encountering an issue that I believe may be due to an error on my part. I have two questions regarding the following code: import AppIntents import SwiftUI import SwiftData @available(iOS 17.0, *) struct CopiarEventoIntent: AppIntent { @Environment(.modelContext) private var context @Parameter(title: Nome do Evento) var name: String @Parameter(title: Data Inicial) var datai: Date @Parameter(title: Data Final) var dataf: Date @Parameter(title: Tipo de Evento) var tipo: String @Parameter(title: Endereço) var endereco: String @Parameter(title: Lembrete) var reminder: Bool static var title: LocalizedStringResource = Adicionar Eventos static var description = IntentDescription(Copiar Eventos e alterar datas, resultValueName: Resultado) @MainActor func perform() async throws -> some IntentResult & ProvidesDialog { let calData = CalData(title: name, datei: datai, datef: dataf, tipo: tipo, endereco: endereco,reminder: reminder) context.insert(calData) return .resul
0
0
954
Apr ’24
NSPersistentCloudKitContainer exclude relationship from share
I am trying to add CloudKit sharing to my app using the new iOS 15 share method https://developer.apple.com/documentation/coredata/nspersistentcloudkitcontainer/3746834-share In the app there are 3 core data entities (Folder, Item, Comment) with the following relationships: A Folder contains many Items An Item has many Comments I want to use CloudKit to share just the Item entity, not any of its relationships. Is this possible to do with the share(_:to:completion:) method? Currently, when I pass an Item to the share method it also includes the Folder and Comments in the CKShare. How do I prevent this?
1
0
981
Feb ’22
When is SwiftData available?
Is SwiftData going to be immediately available to use with iOS 16? I ask because I'm working on an app which I'd like to release around the end of this month, and I'm about to implement CoreData for it, but wanted to see when SwiftData would be available. My guess was that SwiftData is for iOS 17 so I should just stick with CoreData for now and switch over later, but just wanted to check to make sure. Thanks!
4
0
5.2k
Jun ’23
protocol extension inheriting from Obj-C optional protocol
I have a couple of view controllers that can send email and was hoping to cleanup the code using a protocol extension. Something like this:protocol MailSender: class, MFMailComposeViewControllerDelegate { func composeMail(config: MFMailComposeViewController -> ()) func presentViewController(viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)?) func dismissViewControllerAnimated(flag: Bool, completion: (() -> Void)?) } extension MailSender { func composeMail(config: MFMailComposeViewController -> ()) { let mailer = MFMailComposeViewController() mailer.mailComposeDelegate = self config(mailer) presentViewController(mailer, animated: true, completion: .None) } // This method will never be called :( func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) { /* handle result */ dismissViewControllerAnimated(true, completion: .None) } }In use it looks like:class MyViewController: UIViewContr
6
0
1.2k
Aug ’15
why don't inherited iVars / instance variables show in the debugger?
I have sub-classed NSError to CDError. NSError shows what amounts to a partial list of instance variables in the debugger (suffices) but what is difficult is that my subclass instance of CDError shows none of the instance variables of the subclass.Something I am doing wrong? Do I have to define something special in CDError to get the debugger to show me all the instance variables available to it? Am I stuck creating custom expression like theError->_code in the debugger?I started to put an instance variable in my subclass so I could set it to 'self' to see the values held in NSError. That is the error instance variable (nil) shown in theError. Decided to post here before I went any farther so don't be side tracked by that iVar pls. I hate to think that every subclass I create I will be putting iVars in for each class below it to see the contents of the internals of the object without creating custom debugger expressions for each one...<img src=https://www.evernote.com/l/ACBPTDBHM1tIUoVIKry9EmrAWgXaoqBHC
0
0
255
Jan ’16
NSPersistentCloudKitContainer: Exclude Relationship From Share
I am trying to add CloudKit sharing to my app using the new iOS 15 share method https://developer.apple.com/documentation/coredata/nspersistentcloudkitcontainer/3746834-sharemanagedobjects In the app there are 3 core data entities (Folder, Item, Comment) with the following relationships: A Folder contains many Items An Item has many Comments I want to use CloudKit to share just the Item entity, not any of its relationships. Is this possible to do with the share(_:to:completion:) method? Currently, when I pass an Item to the share method it also includes the Folder and Comments in the CKShare. How do I prevent this? A similar question was posted here with no response: https://developer.apple.com/forums/thread/697630
1
0
903
Jun ’22
one to many relationship
I am trying to get my head around a problem involving a one to many relationship. I'm not sure that my approach to this design is a good one.I have two entities, address and rooms. An address can have many rooms. I have set up a one to many relationship between the address table and room table. The problem is differentiating the Living Room of address 1 from the Living Room of address 2. Currently when I call NSManagedObject, the table rows are displaying all the rooms regardless of the address being chosen. I am not sure how to uniquely identify a room with an address.Sorry this is so vague, but I'm pretty new to core data, and I'm not even sure if my approach is a valid one. Any help to get me pointe in the right direction would be greatly appreciated.
Replies
2
Boosts
0
Views
349
Activity
Jun ’15
SwiftData .returnsDistinctResults ?
With NSFetchRequest of coreData it was possible to only return distinct results from the database with .returnsDistinctResults What if any is the fetch equivalent in SwiftData ? currently I'm appending entries into a Set. Only with over 200k rows and no newBackgroundContext() in SwiftData the UI is locked for a number of seconds
Replies
1
Boosts
0
Views
823
Activity
Oct ’23
NSBatchInsertRequest and 2 tables with one-to-relationship
hi everybody, I have a database in which there are 2 tables with a one-many relationship. I need to use NSBatchInsertRequest because I have a large data’s to insert in db. I know that NSBatchInsertRequest doesn’t manages relationships. so how can I use NSBatchInsertRequest to insert data and insert the relationships? thank you
Replies
0
Boosts
0
Views
811
Activity
Feb ’21
How to save a singleton using SwiftData?
I am persisting a tree data structure Node using SwiftData with CloudKit enabled. I understand how to manage my NonSingleton models in SwiftUI. However, when a user first launches my app a Node model instance needs to be created that is separate from any NonSingleton model. So I think a Singleton class is appropriate but I don't know how to persist it? @Model final class Node { var parent: Node? @Relationship(deleteRule: .cascade, inverse: Node.parent) var children = [Node]() // ... } @Model final class NonSingleton { @Relationship(deleteRule: .cascade) var node: Node // ... } @Model // ? final class Singleton { static var node = Node() private init() {} }
Replies
1
Boosts
0
Views
997
Activity
Apr ’24
SwiftData and UIManagedDocument
Can SwiftData be used with UIManagedDocument? Also if the UIManagedDocument is integrated with CloudKit?
Replies
1
Boosts
0
Views
961
Activity
Jun ’23
Is it possible to create relationships between objects in previews?
Is it possible to create one-to-many relationships between optional objects in SwiftData Xcode Previews? The below code I call from #Preview crashes the canvas on the post.authors?.append(tempAuthors.randomElement()!) when I try to append an array to an optional array. I can append the previewAuthors array to the authors array in the simulator or on device, but it crashes in Preview. My running theory is it has something to do with being in memory since this works on device, but I'm not sure that I missed something super obvious. static func previews(_ count: Int) -> [Post] { let previewAuthors = [Author(name: ...), Author(name: ...)] var posts: [Post] = [] for i in 0..
Replies
2
Boosts
0
Views
1k
Activity
Oct ’23
Reply to SwiftData: "Illegal attempt to establish a relationship 'item' between objects in different contexts
It's because all SwiftData relationships must be optional, at least at this time and in my experience thus far. I've received this error quite a bit. Changing your code to the below should resolve your issue, I'd be curious if not class MyItem { var name: String var item: Item? init(name: String, item: Item? = nil) { self.name = name self.item = item } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Nov ’23
didSelectRowAt method is not being called when inheriting from BaseViewController.
class BaseViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() self.hideKeyboardWhenTappedAround() } }
Replies
1
Boosts
0
Views
416
Activity
Mar ’21
Reply to SwiftData errors when trying to insert multiple objects
Some ideas: Verify there isn't already an item with id: 1 when re-running mock code Verify anything with @Attribute(.unique) is ACTUALLY unique in mock code Make sure that any models that have a @Relationship have a corresponding deleteRule, if you're running a series of delete(...) elsewhere. The first one caught me off guard -- if running a series of delete calls on PersistentModel types, make sure that any models that have a @Relationship also have a corresponding deleteRule. When deleting from either end, SwiftData can null out the child in its parent (.nullify), or delete its parent altogether (.cascade). Without this, a hanging reference will be left, causing null value errors. For example: @Model struct Aisle { @Relationship(deleteRule: .nullify, inverse: Item.aisle) var items: [Item] } @Model struct Item { @Relationship(deleteRule: .nullify) // works! var aisle: Aisle } Also, save the model context before running deletes. Otherwise, changes will be flushed
Replies
Boosts
Views
Activity
Feb ’24
Appintent and swiftdata
Hi, I'm new to Swift development and encountering an issue that I believe may be due to an error on my part. I have two questions regarding the following code: import AppIntents import SwiftUI import SwiftData @available(iOS 17.0, *) struct CopiarEventoIntent: AppIntent { @Environment(.modelContext) private var context @Parameter(title: Nome do Evento) var name: String @Parameter(title: Data Inicial) var datai: Date @Parameter(title: Data Final) var dataf: Date @Parameter(title: Tipo de Evento) var tipo: String @Parameter(title: Endereço) var endereco: String @Parameter(title: Lembrete) var reminder: Bool static var title: LocalizedStringResource = Adicionar Eventos static var description = IntentDescription(Copiar Eventos e alterar datas, resultValueName: Resultado) @MainActor func perform() async throws -> some IntentResult & ProvidesDialog { let calData = CalData(title: name, datei: datai, datef: dataf, tipo: tipo, endereco: endereco,reminder: reminder) context.insert(calData) return .resul
Replies
0
Boosts
0
Views
954
Activity
Apr ’24
NSPersistentCloudKitContainer exclude relationship from share
I am trying to add CloudKit sharing to my app using the new iOS 15 share method https://developer.apple.com/documentation/coredata/nspersistentcloudkitcontainer/3746834-share In the app there are 3 core data entities (Folder, Item, Comment) with the following relationships: A Folder contains many Items An Item has many Comments I want to use CloudKit to share just the Item entity, not any of its relationships. Is this possible to do with the share(_:to:completion:) method? Currently, when I pass an Item to the share method it also includes the Folder and Comments in the CKShare. How do I prevent this?
Replies
1
Boosts
0
Views
981
Activity
Feb ’22
When is SwiftData available?
Is SwiftData going to be immediately available to use with iOS 16? I ask because I'm working on an app which I'd like to release around the end of this month, and I'm about to implement CoreData for it, but wanted to see when SwiftData would be available. My guess was that SwiftData is for iOS 17 so I should just stick with CoreData for now and switch over later, but just wanted to check to make sure. Thanks!
Replies
4
Boosts
0
Views
5.2k
Activity
Jun ’23
protocol extension inheriting from Obj-C optional protocol
I have a couple of view controllers that can send email and was hoping to cleanup the code using a protocol extension. Something like this:protocol MailSender: class, MFMailComposeViewControllerDelegate { func composeMail(config: MFMailComposeViewController -> ()) func presentViewController(viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)?) func dismissViewControllerAnimated(flag: Bool, completion: (() -> Void)?) } extension MailSender { func composeMail(config: MFMailComposeViewController -> ()) { let mailer = MFMailComposeViewController() mailer.mailComposeDelegate = self config(mailer) presentViewController(mailer, animated: true, completion: .None) } // This method will never be called :( func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) { /* handle result */ dismissViewControllerAnimated(true, completion: .None) } }In use it looks like:class MyViewController: UIViewContr
Replies
6
Boosts
0
Views
1.2k
Activity
Aug ’15
why don't inherited iVars / instance variables show in the debugger?
I have sub-classed NSError to CDError. NSError shows what amounts to a partial list of instance variables in the debugger (suffices) but what is difficult is that my subclass instance of CDError shows none of the instance variables of the subclass.Something I am doing wrong? Do I have to define something special in CDError to get the debugger to show me all the instance variables available to it? Am I stuck creating custom expression like theError->_code in the debugger?I started to put an instance variable in my subclass so I could set it to 'self' to see the values held in NSError. That is the error instance variable (nil) shown in theError. Decided to post here before I went any farther so don't be side tracked by that iVar pls. I hate to think that every subclass I create I will be putting iVars in for each class below it to see the contents of the internals of the object without creating custom debugger expressions for each one...<img src=https://www.evernote.com/l/ACBPTDBHM1tIUoVIKry9EmrAWgXaoqBHC
Replies
0
Boosts
0
Views
255
Activity
Jan ’16
NSPersistentCloudKitContainer: Exclude Relationship From Share
I am trying to add CloudKit sharing to my app using the new iOS 15 share method https://developer.apple.com/documentation/coredata/nspersistentcloudkitcontainer/3746834-sharemanagedobjects In the app there are 3 core data entities (Folder, Item, Comment) with the following relationships: A Folder contains many Items An Item has many Comments I want to use CloudKit to share just the Item entity, not any of its relationships. Is this possible to do with the share(_:to:completion:) method? Currently, when I pass an Item to the share method it also includes the Folder and Comments in the CKShare. How do I prevent this? A similar question was posted here with no response: https://developer.apple.com/forums/thread/697630
Replies
1
Boosts
0
Views
903
Activity
Jun ’22