NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

SOLVED: Help! - "Foundation._GenericObjCError error 0"

Forums > Swift

I am writing an update to a small app whereby JSON data is taken from a web address, decoded and then loaded into a struct for use. I also want the to then copy the newly retreived data into core data for use if the app should be offline and connot reach the server.

All is working fine except when it comes to attempting to save the retreived data into core data. I keep getting the slightly vague error

'The operation couldn’t be completed. (Foundation._GenericObjCError error 0.)'

I cant seem to find much good info on this error and what causes it. The print line to check the data is what I'd expect is showing the expected output. Any ideas on causes and how to reslove would be appreciated. I'm fairly new to core data so I'm a bit stuck.

The code I am using to extract each entry and try to save it is as below:

                                       for count in rates {
                                        let newExchange = CURRENCIES(context: viewContext)
                                        newExchange.core_ID = UUID()
                                        newExchange.core_Code = count.CODE
                                        newExchange.core_Rate = count.RATE
                                        newExchange.core_Time = count.TIME
                                        newExchange.core_Flag = count.FLAG
                                        newExchange.core_Country = count.COUNTRY
                                        newExchange.core_Currency_Name = count.CURRENCY_NAME
                                                do {
                                                    try viewContext.save()
                                                print("Order saved.") } catch {
                                                print(error.localizedDescription)
                                                    print(newExchange.core_ID,count.CODE,count.RATE,count.TIME,count.FLAG,count.COUNTRY,count.CURRENCY_NAME)
                                                }
                                               }

   

Think I've sussed it out. (well I've overcome that issue for it to be undoubtably replaced by another one around the corner!)

I have a public class to fetch the data and put the fetched data into the struct. The fetching of the data was done under the init() of the class. I also had the code for writing this data to Core Data within the init also. I found a snippet on the Apple forums that it will not wirk within the init

Since moving the 'save to core data' part into a 'func' and calling the func when required everything is now working as it should.

   

Hacking with Swift is sponsored by Stream

SPONSORED Check out Stream's cross-platform open source chat SDK on GitHub! Write once and deploy your app with fully featured chat UI on iOS and macOS.

Go to GitHub

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.