NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

Day 61 challenge: trouble loading [Friends]

Forums > 100 Days of SwiftUI

I've been stuck at this for hours now and I've tried everything I could think of. I've loaded the JSON data into structs and just want to make the Core Data entities from that (I'm not trying to go directly from JSON to Core Data).

The regular things show up just fine but I can't get the friends (and tags) to work properly no matter what I do.

I've set up a one to many relationship with "CDFriend" and this is my content view right now:

//  ContentView.swift
//  FriendFace

import SwiftUI

struct ContentView: View {

    @Environment(\.managedObjectContext) var moc
    @State var usersAll = [User]()

    @FetchRequest(entity: CDUser.entity(), sortDescriptors:[]) var userCDList: FetchedResults<CDUser>

    var body: some View {
//            List(usersAll, id: \.id) { user in
//                NavigationLink(destination:userView(user: user, allUsers: usersAll)){
//                    Text(
//                }
//            }.navigationBarTitle("FriendFace")
//            .onAppear(perform: loadData)

            List(userCDList, id: \.id) { user in
                            NavigationLink(destination:userView(user: user, allUsers: usersAll)){

                        .onAppear(perform: loadData)

    func buildCD(){

        for user in usersAll {

            let newUser = CDUser(context: self.moc)

            newUser.isActive = user.isActive
            newUser.age = Int16(user.age)
            newUser.address = user.address
            newUser.about = user.about
            newUser.registered = user.registered
            newUser.tags = ""

            var tempFriendsArray = Array<CDFriend>()

            for friend in user.friends{

                let newFriend = CDFriend(context: moc)



            newUser.friends = NSSet(array: tempFriendsArray)



    func loadData(){

        guard let url = URL(string: "") else {
            print ("invalid url")

        let request = URLRequest(url: url)

        URLSession.shared.dataTask(with: request) { data, response, error in

            if let data = data{

                if let decodedResponse = try? JSONDecoder().decode([User].self, from: data){

                    DispatchQueue.main.async {
                        self.usersAll = decodedResponse



            print("Fetch failed: \(error?.localizedDescription ?? "Unknown error")")




struct ContentView_Previews: PreviewProvider {
    static var previews: some View {

I also tried using this:


instead of making a temporary array but this didnt work either.

I'd really appreciate any help!

I'm really on the verge of giving up on this challenge...


I don't know wether I am right,but I think " newUser.friends = NSSet(array: tempFriendsArray)" this will not be saved correctly.Its type is arrary,which can't be saved correctly, the type of tags is also array,so there are songthing wrong.


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 October 1st.

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.