WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

SOLVED: Day 61, Missing data while working with Core Data

Forums > 100 Days of SwiftUI

I did this challenge but when I compared the list of friends for every user to the original JSON, I noticed that some friends were missing. So I decided to take help from hws+ and I noticed that paul has the same issue on his solution. For instance at minute 21:38 of his video solution, paul tap on a user called Allie Mendoza and on paul's simulator we can see that this user has just one friend called Calderon Blackwell, but if we take a look at the original JSON we can see that Allie Mendoza actually has 6 friends, so 5 of them were missing and the same thing happens to me on my simulator.

I would appreciate some clarity on this, Thank you

   

I experienced the same thing and I had a post on this but I haven't been able to locate it. If I find it I'll add the link, but it might have gotten deleted.

Make sure you have the following:

  1. You have the NSMergePolicy in your DataController.
  2. You have a many to many relationship created.
  3. You have constraints set on the CachedFriend and CachedUser Enitities.
class DataController: ObservableObject {
    let container = NSPersistentContainer(name: "FriendFace")

    init() {
        container.loadPersistentStores { descrption, error in
            if let error = error {
                print("Core Data failed to load: \(error.localizedDescription)")
                return
            }
            self.container.viewContext.mergePolicy = NSMergePolicy.mergeByPropertyObjectTrump
        }
    }
}

Relationships

Constraints

Constraints

1      

I also added some state, toolbar items and a method to calculate the number of people so I could see what was happening:

@State private var uniqueFriends = [Friend]()

.toolbar {
  ToolbarItemGroup(placement: .navigationBarLeading) {
    Text("CU: \(cachedUsers.count)")
    Text("CF: \(cachedFriends.count)")
    Button("UniqueFriends") {
      calculateUniqueFriends()
    }
  }

  ToolbarItemGroup(placement: .bottomBar) {
    Text("U: \(users.count)")
    Text("F: \(uniqueFriends.count)")
  }
}
func calculateUniqueFriends() {
  for user in users {
    for friend in user.friends {
      if uniqueFriends.contains(where: { $0.name == friend.name}) {
        continue
      } else {
        uniqueFriends.append(friend)
      }
    }
  }
}

Basically you should have a 100 CachedUsers and 100 CachedFriends, which should match 100 Users and 100 Unique Friends from the download.

1      

Wow! Thank you so much @vtabmow this has been really helpful, my problem was that I had Cached friend with a To One relationship with user and Paul has the same issue in his video solution. I think this makes a lot of sense because one user can have multiple friends, but one friend can also belong to multiple users. I spent hours trying to figure out what was going on here, Thanks again! 😀

1      

Save 50% in my Black Friday sale.

SAVE 50% To celebrate WWDC22, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.