0

I am saving some data I get from an Api with Core Data. Sometimes when I want to save the data an error occurs:

Thread 4: EXC_BAD_ACCESS (code=1, address=0x0)

in this line:

coin.name = ticker.value as? String

This is my full function:

 private func saveDictionary(version: ApiParameter, _ data: [String:Any?]?, _ fetchRequest: NSFetchRequest<Coin>) throws {

        if checkVersion(version: version.versionKey()) {
            if data != nil {
                for ticker in data! {
                    if ticker.value != nil {
                        fetchRequest.predicate = NSPredicate.init(format: "ticker = %@", "\(ticker.key)")
                        do {
                            let coins = try managedContext.fetch(fetchRequest)

                            for coin in coins {
                                //                        print("ticker:",coin.ticker)
                                if ticker.key == coin.ticker {
                                    switch version {
                                    case .linksWebsites:
                                        coin.website = ticker.value as? String
                                    case .fdefSymbolsTallCc:
                                        coin.ccsymbol = ticker.value as? String
                                    case .icons32Exall:
                                        coin.icon = ticker.value as? NSData
                                    case .fdefCoinsTarrExall:
                                        coin.group = ticker.value as? [String]
                                    case .coinNamesExall:

                                        coin.name = ticker.value as? String
                                    default: break
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        do {
            try managedContext.save()
        } catch let error {
            print(error.localizedDescription)
            throw error
        }
    }

After some research I found out that the error indicates there is a NULL pointer, but when I inspect the variable it is not null:

enter image description here

PaFi
  • 888
  • 1
  • 9
  • 24
  • At least change the type of the dictionary to `[String:Any]`. An optional value is pointless because a `nil` value means *key is missing* and it avoids a *double optional* type. And remove all `do - catch` blocks. As the function `throws` all errors are handed over. And use optional binding rather than *objective-c-ish* `if data != nil` – vadian Dec 04 '18 at 09:52
  • thanks for the feedback, but does this solve the problem with the null pointer? – PaFi Dec 04 '18 at 13:45
  • PaFi, you've got a lot of code there, and stuff is complicated nowadays. You should do as @vadian recommends and then answer the question of whether or not it fixes the problem. When you've seemingly reached a dead end on a problem, consider fixing nearby stuff which you've learned is wrong. Maybe it will fix the main problem and maybe it won't, but as a more senior engineer told me decades ago, "even if we haven't done something, we'll have done something". – Jerry Krinock Dec 04 '18 at 16:00

0 Answers0