Home IOS Development ios – Crashes when accessing SwiftData mannequin in View after it has been decoded from JSON, nevertheless it hundreds superb after the app reloads. What could possibly be inflicting it?

ios – Crashes when accessing SwiftData mannequin in View after it has been decoded from JSON, nevertheless it hundreds superb after the app reloads. What could possibly be inflicting it?

0
ios – Crashes when accessing SwiftData mannequin in View after it has been decoded from JSON, nevertheless it hundreds superb after the app reloads. What could possibly be inflicting it?

[ad_1]

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

service.fetch(from: url, completion: { end in
                        change outcome {
                        case .success(let itemizing):
                            save(itemizing)
                        case .failure:
                            print("Didn't retrieve and decode information 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 (Outcome<Itemizing, Error>) -> Void) {
        
        let request = URLService.addUserAgentTo(url)
        
        session.dataTask(with: request, completionHandler: { information, urlResponse, error in
            // Test for error
            if let error = error {
                print("Error beginning the session")
                print(error.localizedDescription)
                completion(.failure(error))
            } else {
                do {
                    let information    = information ?? Information()
                    let itemizing = strive self.decoder.decode(Itemizing.self, from: information)
                    completion(.success(itemizing))
                } catch {
                    // Notify of failure to decode information
                    print("Didn't decode Reddit information")
                    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.information.youngsters.sorted(by: {$0.information.timestamp < $1.information.timestamp})) { put up in
                        Textual content(put up.information.title)
                    }
                }
                 
            }

That is the place the problem happens. When it first tries to fetch information, it would crash and level to this code accessing $0.information. The message it offers you would not inform you a lot.

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

Then it would 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, in the event you simply press the Begin button in Xcode to rerun the app, it will load all the info superb. 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 will be a problem to start with. If listings was empty it would not attempt to entry $0.information in any respect.

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

I do not know what to strive. I’ve tried transferring some code into DispatchQueue.fundamental.async, nevertheless it did not work. I additionally tried utilizing Process.

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here