GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

Question about Friendface, part 2

Forums > 100 Days of SwiftUI

@Haibo  

In fetchUsers method, it has a guard check first, so if users is not empty, aka Core Data has users data, the code will return and it will never do the network fetch request again.

My problem is that if JSON data from server have some update in the future, our code would not get it because the users would never be empty after it get the value from the first network call.

    func fetchUsers() async {
        guard users.isEmpty else { return }

        do {
            let url = URL(string: "https://www.hackingwithswift.com/samples/friendface.json")!
            let (data, _) = try await URLSession.shared.data(from: url)

            let decoder = JSONDecoder()
            decoder.dateDecodingStrategy = .iso8601
            let users = try decoder.decode([User].self, from: data)
            await MainActor.run {
                updateCache(with: users)
            }
        } catch {
            print("Download failed.")
        }
    }

2      

Hacking with Swift is sponsored by Essential Developer.

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until February 9th.

Click to save your free spot now

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.