Wednesday, November 8, 2023
HomeiOS Developmentios - Crashes when accessing SwiftData mannequin in View after it has...

ios – Crashes when accessing SwiftData mannequin in View after it has been decoded from JSON, however it hundreds wonderful after the app reloads. What might be inflicting it?


I’ve an app that request knowledge from Reddit. When the view seems, within the onAppear modifier, it runs a perform that fetches the info.

service.fetch(from: url, completion: { lead to
                        swap outcome {
                        case .success(let itemizing):
                            save(itemizing)
                        case .failure:
                            print("Did not retrieve and decode knowledge from Reddit")
                        }
                    })
func save(_ itemizing: Itemizing) {
        modelContext.insert(itemizing)
        do {
            strive modelContext.save()
        } catch {
            print("Error saving context")
            print(error.localizedDescription)
        }
    }
func fetch(from url: URL, completion: @escaping (End result<Itemizing, Error>) -> Void) {
        
        let request = URLService.addUserAgentTo(url)
        
        session.dataTask(with: request, completionHandler: { knowledge, urlResponse, error in
            // Verify for error
            if let error = error {
                print("Error beginning the session")
                print(error.localizedDescription)
                completion(.failure(error))
            } else {
                do {
                    let knowledge    = knowledge ?? Information()
                    let itemizing = strive self.decoder.decode(Itemizing.self, from: knowledge)
                    completion(.success(itemizing))
                } catch {
                    // Notify of failure to decode knowledge
                    print("Did not decode Reddit knowledge")
                    print(error.localizedDescription)
                    completion(.failure(error))
                }
            }
        }).resume()
    }

Within the view itself, all it does is print the put up title names right into a Record.

Record {
                ForEach(listings) { itemizing in
                    ForEach(itemizing.knowledge.youngsters.sorted(by: {$0.knowledge.timestamp < $1.knowledge.timestamp})) { put up in
                        Textual content(put up.knowledge.title)
                    }
                }
                 
            }

That is the place the difficulty happens. When it first tries to fetch knowledge, it’ll crash and level to this code accessing $0.knowledge. The message it supplies you would not inform you a lot.

Thread 1: EXC_BREAKPOINT (code=1, subcode=0x1c3c5ca68)

Then it’ll additionally level to the Put up mannequin I’ve created and develop some code that appears to be inside to SwiftData and the @Mannequin wrapper.

Post error

The bizarre factor is, when you simply press the Begin button in Xcode to rerun the app, it’s going to load all the info wonderful. So it clearly is decoding and saving it correctly, however there appears to be some kind of disconnect between saving and viewing it. Though, I am undecided why it could be a problem to start with. If listings was empty it would not attempt to entry $0.knowledge in any respect.

Does anybody have any concepts on issues I can do to resolve this challenge?

I do not know what to strive. I’ve tried transferring some code into DispatchQueue.important.async, however it did not work. I additionally tried utilizing Job.



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments