TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Day 61: links not working

Forums > 100 Days of SwiftUI

Hello, I've tried day 61 challenge but it's not working as expected, as links are greyed out and not working (and navigation title is not displayed). I don't have any error message.

Could somebody help me? Here is my code. Thank you very much.

ContentView
struct ContentView: View {
    var body: some View {
        UserListView()
    }
}
UserListView
struct UserListView: View {
    @Environment(\.managedObjectContext) var moc
    @FetchRequest(sortDescriptors: []) var cachedUsers: FetchedResults<CachedUser>

    func requestUsers() async -> [User] {
        let urlString = "https://www.hackingwithswift.com/samples/friendface.json"

        guard let url = URL(string: urlString) else {
            print("Invalid URL")
            return []
        }

        let decoder = JSONDecoder()
        decoder.dateDecodingStrategy = .iso8601

        do {
            let (data, _) = try await URLSession.shared.data(from: url)
            let decodedResponse = try decoder.decode([User].self, from: data)
            return decodedResponse
        } catch {
            print(error)
        }

        return []
    }

    func getUsers() async {
        if cachedUsers.isEmpty {
            await loadData()
        }
    }

    func loadData() async {
        let fetchedUsers = await requestUsers()
        for fetchedUser in fetchedUsers {
            let newUser = CachedUser(context: moc)
            newUser.name = fetchedUser.name
            newUser.id = fetchedUser.id
            newUser.isActive = fetchedUser.isActive
            newUser.age = Double(fetchedUser.age)
            newUser.about = fetchedUser.about
            newUser.email = fetchedUser.email
            newUser.address = fetchedUser.address
            newUser.company = fetchedUser.company
            newUser.registered = fetchedUser.registeredString

            for fetchedFriend in fetchedUser.friends {
                let newFriend = CachedFriend(context: moc)
                newFriend.id = fetchedFriend.id
                newFriend.name = fetchedFriend.name
                newUser.addToFriends(newFriend)
            }
        }
        try? moc.save()
    }

    var body: some View {
        List {
            ForEach(cachedUsers) { user in
                NavigationLink {
                    UserDetailsView(user: user)
                } label : {
                    VStack(alignment: .leading) {
                        Text(user.wrappedName)
                        Text(user.wrappedIsActive).foregroundColor(.secondary).font(.footnote)
                    }.padding(.vertical, 1)
                }
            }
        }.navigationTitle("Users")
            .task {
            await getUsers()
        }
    }
}
UserDetailsView
struct UserDetailsView: View {
    @State var user: CachedUser

    var body: some View {
        List {
            Section("General informations") {
                Text("\(user.wrappedName)")
                Text("\(Int(user.age)) years old")
                Text("\(user.wrappedIsActive)")
            }
            Section("Friends") {
                ForEach(user.friendsArray) { friend in
                    Text("\(friend.wrappedName)")
                }
            }
        }.navigationTitle("\(user.wrappedName)")
    }
}
CachedUser+CoreDataProperties
extension CachedUser {

    @nonobjc public class func fetchRequest() -> NSFetchRequest<CachedUser> {
        return NSFetchRequest<CachedUser>(entityName: "CachedUser")
    }

    @NSManaged public var isActive: Bool
    @NSManaged public var id: String?
    @NSManaged public var name: String?
    @NSManaged public var age: Double
    @NSManaged public var company: String?
    @NSManaged public var email: String?
    @NSManaged public var address: String?
    @NSManaged public var about: String?
    @NSManaged public var registered: String?
    @NSManaged public var tags: String?
    @NSManaged public var friends: NSSet?

    var wrappedIsActive: String {
        if isActive {
            return "Active"
        } else {
            return "Not active"
        }
    }

    var wrappedName: String {
        name ?? "Unknown"
    }

    var wrappedCompany: String {
        company ?? "No job"
    }

    var wrappedAbout: String {
        about ?? "No data"
    }

    var wrappedAddress: String {
        address ?? "Homeless"
    }

    var wrappedEmail: String {
        email ?? "no email"
    }

    var wrappedFormattedDate: String {
        registered ?? "N/A"
    }

    var wrappedID: String {
        id ?? "Unknown ID"
    }

    public var friendsArray: [CachedFriend] {
        let set = friends as? Set<CachedFriend> ?? []
        return set.sorted {
            $0.wrappedName < $1.wrappedName
        }
    }
}

// MARK: Generated accessors for friends
extension CachedUser {

    @objc(addFriendsObject:)
    @NSManaged public func addToFriends(_ value: CachedFriend)

    @objc(removeFriendsObject:)
    @NSManaged public func removeFromFriends(_ value: CachedFriend)

    @objc(addFriends:)
    @NSManaged public func addToFriends(_ values: NSSet)

    @objc(removeFriends:)
    @NSManaged public func removeFromFriends(_ values: NSSet)

}

extension CachedUser : Identifiable {

}
CachedFriend+CoreDataProperties
extension CachedFriend {

    @nonobjc public class func fetchRequest() -> NSFetchRequest<CachedFriend> {
        return NSFetchRequest<CachedFriend>(entityName: "CachedFriend")
    }

    @NSManaged public var id: String?
    @NSManaged public var name: String?
    @NSManaged public var users: NSSet?

    var wrappedId: String {
        return id ?? "Unknown ID"
    }

    var wrappedName: String {
        return name ?? "Unknown"
    }

    var usersArray: [CachedUser] {
        let set = users as? Set<CachedUser> ?? []

        return set.sorted {
            $0.wrappedName < $1.wrappedName
        }
    }
}

// MARK: Generated accessors for users
extension CachedFriend {

    @objc(addUsersObject:)
    @NSManaged public func addToUsers(_ value: CachedUser)

    @objc(removeUsersObject:)
    @NSManaged public func removeFromUsers(_ value: CachedUser)

    @objc(addUsers:)
    @NSManaged public func addToUsers(_ values: NSSet)

    @objc(removeUsers:)
    @NSManaged public func removeFromUsers(_ values: NSSet)

}

extension CachedFriend : Identifiable {

}

3      

Hi there! NavigationLink only works inside NavigationStack or NavigationSplitView so just put your List inside like so:

NavigationStack {
  List...

    .navigationTitle("You navigation title") // to be placed inside NavigaitonStack as well
}

PS. No need to put it for every view. Only according to your data flow. In this particular case you just create in inside UserListView.

3      

Oh my… such a beginner mistake on a hard project! Thanks! And it's working now…

If I may, I don't understand where I was supposed to put MainActor.run, I couldn't make it work in the task of UserListView… Can you or someone else tell me how it works?

3      

I think you don't really need to use MainActor in this project set up due to workflow established. If we think about how we get data there, we can see that our view receives the data from @FetchRequest

@FetchRequest(sortDescriptors: []) var cachedUsers: FetchedResults<CachedUser>

// and later we provide it to the view

List {
            ForEach(cachedUsers) { user in
            ...
}

and according to documentation it is already run on the main thread. (see @MainActor?)

@propertyWrapper @MainActor public struct FetchRequest<Result> where Result : NSFetchRequestResult

so when we use .task{}, what we actually do, we fetch our data from server if our cachedUsers is empty. If it is not empty we go to url, get data, and save it to core data, and then @FetchRequest provides that data to UI. Think of it as a pipeline which updates your cachedUsers as soon as there is new data in managed object context. So my understanding you can avoid using it here as it doesn't involve direct interaction with UI...

Basically why we need to use MainActor is to make some piece of code, which usually involves interaction or updating our UI, run on main thread. As another example, if we go to url, get data, and use it for our view, then you will be warned to run it on main thread, as try await URLSession.shared.data(from: url) will provide data directly and not through core data. And URLSession runs on background thread... so XCode will warn you about that...

4      

Thanks for the explanations! 😁 It's really clear, I got it!

3      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

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.