|
I have completed day 60 and have a functioning app, but now I don't really know how to throw CoreData into the mix. I have created a xcdatamodeld file, added entities and properties to it, manually added wrappers for the properties to my Entities, added a Persistence.swift file, and added the lines of code needed to make my managedObjectContext available in content view. So, at this point, I should be ready to start using CoreData in my app. However, I have some questions.
|
|
My biggest problem at this point is this... This was my ContentView before
But now, I need to use
instead of
so |
|
Ok, I decided that it was probably best to make my data model entities conform to codable, so I followed the instructions on this page https://www.donnywals.com/using-codable-with-core-data-and-nsmanagedobject/ It actually wasn't too hard to follow if you just take it one step at a time. I think I just got overwhelmed by it when I first looked at it, and didn't know if I should trust this random guy who was not Paul telling me what to do. But he seems to know what he is doing. After doing that, I basically just had to create my However, I am still having problems. My ContentView list only ends up having one User in it. When I tap on the user to see the UserView, most of their information is there, but the Tags and Friends list are completely empty. Even worse, when I tap the 'Back' button and go back to my list of Users, it now has two of the same user in the list. Each time I go back and forth between the UserView and ContentView it adds another copy of that one user. I don't know how this is happening, because I have added constraints to the id property of all of my entities, and added
to my PersistenceController initializer, so I thought that should stop it from adding duplicates of my entities. This is all getting very confusing, and I'm not even sure what code would be helpful for you to see at this point, but I have about 13 different files in my project now, and I don't think that sharing them all in this forum would be super helpful. This is what my content view looks like now though
This line seems like it might be a problem to me, bud I didn't know what else to do with the decoder at this point. Since it should just be storing the results into CoreData, I didn't know what else to do with the return value except store it in a constant with no name.
But I am getting the error shown just below that in my output log CoreData: warning: View context accessed for persistent container FriendFace with no stores loaded Decoding Failed: The data couldn’t be read because it is missing. |
SPONSORED Let’s face it, SwiftUI previews are limited, slow, and painful. Judo takes a different approach to building visually—think Interface Builder for SwiftUI. Build your interface in a completely visual canvas, then drag and drop into your Xcode project and wire up button clicks to custom code. Download the Mac App and start your free trial today! Sponsor Hacking with Swift and reach the world's largest Swift community! |
|
I just remembered the tip that @RoosterBoy gave me a few days ago, which is to use error rather than error.localizedDescription when catching JSON decoding errors. Now I can see this error. Decoding Failed: keyNotFound(CodingKeys(stringValue: "users", intValue: nil), Swift.DecodingError.Context(codingPath: "[_JSONKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "friends", intValue: nil), _JSONKey(stringValue: "Index 0", intValue: 0)], debugDescription: "No value associated with key CodingKeys(stringValue: \"users\", intValue: nil) (\"users\").", underlyingError: nil)) |
|
You can trust Donny Wals. And his books Practical Core Data and Practical Combine are essential reading, IMO.
This error is because the JSON you are trying to decode doesn't have a
Got a github project you can share the link to? |
|
So, that error leads me to believe that I have a problem with one or more of these three files. I wasn't sure if I was supposed to add 'friends', 'users', or 'tags' to my coding keys, because they are set up as relationships and not properties in my data model. Maybe that has something to do with this? User+CoreDataClass.swift
Friend+CoreDataClass.swift
Tag+CoreDataClass.swift
|
|
Aha! Yeah that does make sense that the JSON wouldn't have a users key. Those are just inverse relationships that I set up in my Tag and Friend entities, but they don't actually exist in the JSON so I probably don't need to include those coding keys or those lines of code in the initializers in my Friend+CoreDataClass.swift and Tag+CoreDataClass.swift files. After removing those, I end up with this error instead. Decoding Failed: typeMismatch(Swift.Dictionary<Swift.String, Any>, Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "tags", intValue: nil), _JSONKey(stringValue: "Index 0", intValue: 0)], debugDescription: "Expected to decode Dictionary<String, Any> but found a string/data instead.", underlyingError: nil)) I assume this has something to do with the fact that I created |
|
Unfortunately, I haven't taken the time to learn to use Github yet. So I don't really know how to add my projects there at this time. But maybe it's about time I learn. |
|
I changed up my data model, so now Tag is no longer an entity, tags is just a property of the User entity. I set the type of the property to "Transformable" but then I was getting a warning in XCode telling me that 'tags' had an undefined transformer. So I tried to enter NSSet\<String> as the transformer, and that got my code to compile, although, I still don't think that is the correct way to do it because it is giving this warning in my output log now. CoreData: warning: no NSValueTransformer with class name 'NSSet\<String>' was found for attribute 'tags' on entity 'User' But it looks like it is loading all of the data from the JSON now, so that is bit of a relief. But I still have other problems. For one thing, I can't save my modelObjectContext. When I try to put
into my loadData() function, it causes my app to crash with this error Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSCFSet: 0x6000004c0060> was mutated while being enumerated.' I think this is some kind of threading issue. Like, the data doesn't finish decoding before trying to save the modelObjectContext or something, but I don't know how to fix that. Also, when I try to click on a user, it takes me to the User view, and then I click on one of their friends, and it takes me to another User view for the friend that I tapped on, but every time I do that it is saying this in the output log... FriendFace[13539:2234741] [Assert] displayModeButtonItem is internally managed and not exposed for DoubleColumn style. Returning an empty, disconnected UIBarButtonItem to fulfill the non-null contract. I think I might just have to give up on this project for now, but at least I got it pretty close to working. Maybe after I learn to use GitHub I'll be able to add my project there and somebody will be able to help me with it a little better. I know that this Forum has gotten long winded and is probably hard to follow and give answers to, so sorry about that. |
|
I have added this project to My GitHub Repository now, and am asking if anybody might be able to help me figure out why it isn't working. If I remove the code from ContentView that tells my ModelObjectContext to try to save, the app seems to run alright. But, as soon as I try to save my data, the app crashes. I'm not sure if there is just a problem with threading that I don't know how to fix, which is causing the MOC to try to save before the data finishes loading from the URL, or if I might have set up my relationships or constraints in my data model incorrectly, and that is causing a problem? I know that my user interface is kind of ugly right now, but it is put together enough to see the data anyway. |
|
I've just started attempting Day 61 today. I must admit I'm feeling kind of overwhelmed in a way I've not felt in so far. Everywhere I look, I've got questions and there don't seem to be answers in what we've been taught to date. I really am not a fan of Core Data!! So far I've done the unwrapping code and (I think) set up the Core Data initialisation and moc etc. I've gone the route of creating tags as a separate entity with a relationship linking it to users with a many to many connections. Some of the questions I've got floating around in my head are:
Right now, I'm considering making another core data entity that saves the array of Users so that I don't have to worry about the names of the instances. But that seems kind of nuts? Maybe not? Once I've got the data into core data, I'm vaguely confident that I'll be able to refactor my code to access the core data instances rather than the JSON. BTW, kind of worried that you're still battling with this a month later @Fly0strich |
|
|
|
No, I've pretty much just decided to move on from it and will probably try to remake the project from scratch afteri finish the 100 days and see if i can figure it out then. |
SPONSORED Let’s face it, SwiftUI previews are limited, slow, and painful. Judo takes a different approach to building visually—think Interface Builder for SwiftUI. Build your interface in a completely visual canvas, then drag and drop into your Xcode project and wire up button clicks to custom code. Download the Mac App and start your free trial today!
Sponsor Hacking with Swift and reach the world's largest Swift community!
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.
Link copied to your pasteboard.