Home > Blockchain >  Swift - how to proceed to decode a local json file if online fetch failed?
Swift - how to proceed to decode a local json file if online fetch failed?

Time:10-15

I have a hosted json file online which i fetch and decode with the code below. And I have a local json file in my Xcode Workspace which should be the fallback file.

Can someone please help me how to change this to use and decode the local file when an error with the hosted file occurs? - e.g. no internet connection, ...

WineryModel.swift

import Foundation

let url = URL(string: "https://***/wineries.json")

class WineryViewModel: ObservableObject {
    @Published var wineries: [Winery]?
    @Published var currentWinery: Winery?
    
    init(){
        Task{
            do{
                try await fetchJsonData()
            }catch{
                // HANDLE ERROR
                print("           
  • Related