Post

Replies

Boosts

Views

Activity

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?
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
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
Jun ’21
Reply to Getting picker selected value with api data
The model is a simple struct import Foundation let myData = [MyModel]() struct MyModel: Hashable, Codable { var ID: String var Name: String var www: String? var Facebook: String? var Twitter: String? var Instagram: String? var Email: String? var Phone: String? var Location: String? var Address1: String? var Address2: String? var Town: String? var County: String? var Postcode: String? var What3Words: String? } Sorry, didn't think that you'd need that as well the picker needs to show the name and the selected value needs to be the ID. thanks
Apr ’21
Reply to Getting picker selected value with api data
Hi, Thanks for the reply. I can get it working using your above example (I based my code off a very similar example), my problem is getting the ID that's used in the model. This is my complete code: @State var mydata = [MyModel]() @State private var ID = 0 var body: some View { NavigationView { Form { Section { Picker("Pick an item", selection: $ID) { ForEach(mydata, id: \.ID) { mymodel in Text(mymodel.name) } }.onAppear(perform: loadData) Text("Selected: \(ID)") } } } } func loadData() { guard let url = URL(string: "https://mysite.co.uk/api/getmydatalist") else { print("Invalid URL") return } let request = URLRequest(url: url) URLSession.shared.dataTask(with: request) { data, response, error in if let data = data { if let response = try? JSONDecoder().decode([MyModel].self, from: data) { DispatchQueue.main.async { mydata = response } return } } }.resume() } I use the same function elsewhere to populate my model collection which I use in a list, its just the picker that's giving me issues. Thanks
Apr ’21