UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Day 61: Friendface - Get an array of [CachedUser]

Forums > 100 Days of SwiftUI

On top of the original requirments, I want to compare the list of friends in user details page with the full users list so that things like greying out unregistered friends and showing the friend's details page are possible.

I've already done so before implementing Core Data, but after that I couldn't pass around the list of users to work with without first converting it from FetchedResults<CachedUser> type into [CachedUser].

To do so I imagine it to be similar as converting the user's friends NSSet to a Set<CachedFriend>, where the function will be:

  func getUsersArray(_ users: FetchedResults<CachedUser>) -> [CachedUser] {
        let set = users as? Set<CachedFriend> ?? [] // "users" here is the variable storing the original FetchedResults<CachedUser>

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

The question is, where should I run the function? I tried putting it inside a didSet like this:

var usersArray: [CachedUser]

@FetchRequest(sortDescriptors: [SortDescriptor(\.name)]) var users: FetchedResults<CachedUser> {
  didSet {
  usersArray = getUsersArray(users)
  }
}

It first said "Cannot assign to property: 'self' is immutable". So I added @State to before var usersArray: [CachedUser], then another error was shown in the upper (top) level file, next to ContentView() - "Missing arguement for parameter 'usersArray' in call"

Have I been wrong since assuming the function? On the other hand how should a computed property like this be declared in general?

I hope it all makes sense. Thanks for your help.

2      

I don't know that it's the right answer, but what I did was put a computed property in my User class that creates an NSCompoundPredicate from the ids of all a users's friends.

@objc dynamic public var friendsPredicate: NSCompoundPredicate {
        var predicates = [NSPredicate]()
        for friend in friendsArray {
            predicates.append(NSPredicate(format: "id == '\(friend.wrappedId)'"))
        }
        return NSCompoundPredicate(orPredicateWithSubpredicates: predicates)
    }

I then adapted the filtered list view from the singers example to initialize with that predicate and create Content for each fetched User.

init(predicate: NSCompoundPredicate, @ViewBuilder content: @escaping (T) -> Content) {
        fetchRequest = FetchRequest(entity: CacheUser.entity(), sortDescriptors: [], predicate: predicate)
        self.content = content
    }

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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.