Post

Replies

Boosts

Views

Activity

Reply to Changing a fetch request
Have this at the top: `struct EditSocialView: View {     @Environment(.managedObjectContext) var managedObjectContext     @Environment(.presentationMode) var presentationMode     var lodge: Lodge = getLodgeData()     //this gets the list from CD     @FetchRequest(         entity: CDLodge.entity(),         sortDescriptors: [             NSSortDescriptor(keyPath: \CDLodge.lodge, ascending: true),         ],         predicate: NSPredicate(format: "lodgeid == 3")     ) var cdlodges: FetchedResults     var body: some View {` As you can see, I have the id hardcoded as 3 the actual id that I want to use comes from the getLodgeData() method at the top (lodge.id) this function retrieves a json string from UserDefaults and converts it to an instance of Lodge. There is a button on the page that deletes this entry from CoreData. I tried to add the FetchRequest there as I can then use predicate: NSPredicate(format: "lodgeid == (lodge.id)") but I get a segmentation error. seem to be going around in circles where I can do what I need to do, just not where I need to do it for it to work. thanks
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’21
Reply to Changing a fetch request
I have this at the top of the page: struct EditSocialView: View {     @Environment(\.managedObjectContext) var managedObjectContext     @Environment(\.presentationMode) var presentationMode var lodge: Lodge = getLodgeData()     //this gets the list     @FetchRequest(         entity: CDLodge.entity(),         sortDescriptors: [             NSSortDescriptor(keyPath: \CDLodge.lodge, ascending: true),         ],         predicate: NSPredicate(format: "lodgeid == 3")     ) var cdlodges: FetchedResults<CDLodge>     var body: some View { As you can see, I have the id hardcoded as 3. The actual id that I want to use is obtained from the getLodgeData() method which is called before the FetchEvent. This method pulls a json string from UserDefaults and converts it to an instance of Lodge, which gives me lodge.id On this page, there is a button which will delete the Lodge record from CoreData. Can I either add the predicate: NSPredicate(format: "lodgeid == \(lodge.id)") when the button is clicked? or, can I have my FetchRequest get all of the list and then I just delete the item where the id is 3? thanks
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’21
Reply to Changing a fetch request
This works great, so thanks for that and I have marked the answer as solution. However, I have managed to find a way where it doesn't work quite as I expected. I have used the same way of doing it on another page where I'm checking to see if the result set is empty and if its not, then set a @State var to true. I put the check in after the fetch request, but it always reports that the results are empty. if cdlodges.isEmpty == false {             //not empty so set fav to true             favourite = true         }         else {             //not a favourite so set to false             favourite = false         } If I add a button to the page with the same code in, it works fine. I'm assuming that where its done after the FetchRequest, the query hasn't had chance to fill the resultset in time. Is there a way to get the check to run on completion of the FetchRequest?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jun ’21
Reply to Changing a fetch request
Have this at the top: `struct EditSocialView: View {     @Environment(.managedObjectContext) var managedObjectContext     @Environment(.presentationMode) var presentationMode     var lodge: Lodge = getLodgeData()     //this gets the list from CD     @FetchRequest(         entity: CDLodge.entity(),         sortDescriptors: [             NSSortDescriptor(keyPath: \CDLodge.lodge, ascending: true),         ],         predicate: NSPredicate(format: "lodgeid == 3")     ) var cdlodges: FetchedResults     var body: some View {` As you can see, I have the id hardcoded as 3 the actual id that I want to use comes from the getLodgeData() method at the top (lodge.id) this function retrieves a json string from UserDefaults and converts it to an instance of Lodge. There is a button on the page that deletes this entry from CoreData. I tried to add the FetchRequest there as I can then use predicate: NSPredicate(format: "lodgeid == (lodge.id)") but I get a segmentation error. seem to be going around in circles where I can do what I need to do, just not where I need to do it for it to work. thanks
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jun ’21
Reply to Changing a fetch request
I have this at the top of the page: struct EditSocialView: View {     @Environment(\.managedObjectContext) var managedObjectContext     @Environment(\.presentationMode) var presentationMode var lodge: Lodge = getLodgeData()     //this gets the list     @FetchRequest(         entity: CDLodge.entity(),         sortDescriptors: [             NSSortDescriptor(keyPath: \CDLodge.lodge, ascending: true),         ],         predicate: NSPredicate(format: "lodgeid == 3")     ) var cdlodges: FetchedResults<CDLodge>     var body: some View { As you can see, I have the id hardcoded as 3. The actual id that I want to use is obtained from the getLodgeData() method which is called before the FetchEvent. This method pulls a json string from UserDefaults and converts it to an instance of Lodge, which gives me lodge.id On this page, there is a button which will delete the Lodge record from CoreData. Can I either add the predicate: NSPredicate(format: "lodgeid == \(lodge.id)") when the button is clicked? or, can I have my FetchRequest get all of the list and then I just delete the item where the id is 3? thanks
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jun ’21
Reply to Changing a fetch request
have just don't that as it wouldn't let me format it properly (and the comment vanished) so have repeated as best I could below. sorry. Appreciate you spending the time to look.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jun ’21
Reply to Changing a fetch request
This works great, so thanks for that and I have marked the answer as solution. However, I have managed to find a way where it doesn't work quite as I expected. I have used the same way of doing it on another page where I'm checking to see if the result set is empty and if its not, then set a @State var to true. I put the check in after the fetch request, but it always reports that the results are empty. if cdlodges.isEmpty == false {             //not empty so set fav to true             favourite = true         }         else {             //not a favourite so set to false             favourite = false         } If I add a button to the page with the same code in, it works fine. I'm assuming that where its done after the FetchRequest, the query hasn't had chance to fill the resultset in time. Is there a way to get the check to run on completion of the FetchRequest?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Jun ’21