|
Hello everyone! I hope you are all doing great! I'm working on Day 61 "Time for Core Data" (https://www.hackingwithswift.com/100/swiftui/61) So far I've managed to:
However, I can't find where to use Paul's This is my
Any thoughts? Thanks in advance for any help! KB |
|
Hi @KeyBarreto! I think, we don't really need to use In this particular challenge we are downloading data using On the other hand, In our particular case, we are fetching data from web, then store it in core data using Your code is fine, the only thing I would point is this part: With every launch of your app this array will be empty.
so every time below part will be triggered, and starts loading data from web and save it to core data. If this is what you want that is fine.
If you want to load the data from web and then read it from core data then logic should be changed to something like this ->
And finally, finally you can throw
PS in
to that, to make sure data is saved properly.
Hopefully it will help :) |
|
Thank you so much @ygeras !! I really appreciate your input, I had the feeling I made all the changes you suggested but I'm still having problems showing all of the friends (I haven't had any purple warnings during the project) Any thoughts? I just realized I published my this question in the wrong forum. It should've been 100 Days of SwiftUI |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates. Sponsor Hacking with Swift and reach the world's largest Swift community! |
|
What kind of issues do you have? |
|
The "List of Friends" is not being displayed correctly, I believe the reason for this is that the JSON data isn't properly saved in I added a https://drive.google.com/file/d/1fkwIb9XjcZo_Ltpf5ToTttc_Dyg8Fbbb/view?usp=share_link And then, I added the https://drive.google.com/file/d/1ugGmuznGAZ0Fz9MPZ18JFLaTTdy3DYKq/view?usp=share_link P.D.: I still don't know how to add images to the messages, that's why I added the link of the screenshots Thanks again for your help @ygeras |
|
|
|
I believe you need to apply the The reason is in Paul’s instruction for the Day 61 challenge: “So, when it comes to creating and saving all your Core Data objects, that’s definitely a task for the main actor”. “Creating” means that when you instantiate the I suspect Xcode does not display purple warnings because Core Data is based on Objective C and is not integrated with Swift Concurrency. The new Swift Data framework should be more Swifty. |
|
Can you try replacing : cachedFriend.friendOfUser = userToSave with: cachedUser.addToFriends(cachedFriend) This should make your friends appear, let me know if it worked for you. |
|
Hello everyone! I'm resuming this project. @bobstern I having a hard time figuring out where to apply the @alexbrossard do you mean Thanks again for the help. |
|
Calling a function with I have not seen a concise, intuitive explanation of From the Concurrency chapter in the Swift Language Guide at swift.org:
"Because the From Paul's "Concurrency by Example": "the await keyword is as much for us as it is for the compiler – it’s a way of clearly marking which parts of the function might suspend" “if you’re attempting to...call a method on an actor, and you’re doing it from outside the actor itself, you must do so asynchronously using await.” “from outside the actor, await is required even for synchronous properties and methods.” The main thread is executed synchronously, of course. A function marked to be run on the main thread may have to await (delay) its execution while, for example, a view is being updated or is responding to user input. |
|
Incidentally, I believe there is no difference between:
and marking saveInCD as @MainActor and calling it using:
|
|
Without having viewed Paul's solution video, I belatedly thought of two problems with the relationships between the cachedUser and cachedFriend entities. It would help if you would insert a screenshot of the entities in the model editor. (1) (2) Taking this logic one step further, it does not make sense to me that Paul suggested defining cachedUser and cachedFriend as separate entities. A more realistic scenario would be that a user's friends are other users. In that case, there should be only one entity called cachedUser or cachedPerson. A user's friends would be defined in a 1-to-many relationship called "friends" that is a "self relationship", a relationship with the same entity on both sides. |
|
Considering that Swift Data is intended to eventually supersede Core Data, and considering that Paul wrote an entire book on Swift Data but only a few brief lessons on Core Data, it seems sensible to not spend any more time on this challenge or on learning Core Data at this point. It seems more productive to finish the 100 Days of SwiftUI, then learn Swift Data rather than Core Data if the subject interests you, and then return to Core Data only if you need the more advanced capabilities of Core Data that Apple has yet implemented in Swift Data. On the other hand, if you relish the challenge, please don’t let me dissuade you! |
|
Thank you so much for your time @bobstern ! I guess Relationships are't quite clear to me yet. Indeed I used One-To-Many for CachedUser (because each can have many friends) and To-One for friends, that might be the problem. And I really appreciate your advice (and your time), I was actually considering it myself when I heard about SwiftData, but I wanted to have CoreDate in my skills set too. Moving on now! Thanks againa! |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.