UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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)
                                                }
                                               }

2      

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.

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.