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

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

4      

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

6      

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.

4      

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! 😀

5      

@andresr-dev I was facing the exact same issue. I was very confused when I saw Paul's solution also had the same issue that my own project had. Your post was a life saver. Thank you!!

Just a note for anyone else trying to implement the same solution: You need to recreate your NSManagedObject Subclasses when you update the one to many relationship on the cached friend.

4      

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 April 28th.

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.