I'm going through this again and there are some things I don't understand. Please help me.
Problem 1
first part of this challange is about struct based DataModel
. When the task is done we can see the info about a user in another view. But I can see a difference between Friends section in struct based DataModel
and CoreData
implementation.
Somehow friends amount changes. Example:
struct Friends vs coreData Friends

First I thought it's because of Constraints
or MergePolicy
but changing constraints or deleting them entirely didn't change anything. The difference in amount of friends remains.
It's a same person but the list of friends has decreased.
Why it happens ? Can someone please explain ?
Problem 2.
During Pauls solution for CoreData implementation in second part of the challange he is making a next method:
func updateCache(with downloadedUsers: [UserDataModel]) {
for user in downloadedUsers {
let cachedUser = CachedUser(context: moc)
cachedUser.id = user.id
cachedUser.isActive = user.isActive
cachedUser.name = user.name
cachedUser.age = Int16(user.age)
cachedUser.company = user.company
cachedUser.email = user.email
cachedUser.address = user.address
cachedUser.about = user.about
cachedUser.registered = user.registered
// small trick here as we don't have an Array of Strings
cachedUser.tags = user.tags.joined(separator: ", ")
for friend in user.friends {
let cachedFriend = CachedFriend(context: moc)
cachedFriend.id = friend.id
cachedFriend.name = friend.name
cachedUser.addToFriends(cachedFriend)
}
}
try? moc.save()
}
I understand the method itself, what we're trying to achieve here.
But after he's finished he's mentioning one thing at 15:55 of the video solution:
"We could put most of this code to our User (dataModel) struct or even to our CachedUser Class (from CoreData?), no users could have a create cachedUser object returns cachedUser with all that stuff copied across.
Or add a custom init() for a CachedUser that has init() with a user + the context ("that create with" ? can't make out). I'd be happy with either of those... but of the two the init() feels more natural to me, having this inside a CachedUser initializer will make sense. "
Can someone please explain what way of writing the code is he talking about ?
Is this a first place where he's kinda touching the topic of MVVM concept he's introducing us in later days to ?
I couldn't figure this out 2 months ago and hesitated to ask people here. But even now when I decided to repeat the lessons to get the better understanding I still can't get it.
Maybe now with the help of someone I will become a little better.
Thanks everyone !