Creating NSManagedObject subclasses – Core Data SwiftUI Tutorial 2/7

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] when we create a new core data entity xcode automatically generates a managed object class we can use in our code we can then use a swift ui fetch request to pull data into our ui but as you've seen it's quite painful we've got optionals everywhere which requires scattering nil coalescing around your code just to make it build there are two ways around this one is fast and easy but likely to end up being problematic and one is a lot slower but will work better in the long term first let's make an entry to work with in our code here in your later model make a new entity here and rename it to be movie double click movie and then we'll add some attributes we'll add one here called the title movie title that'll be a string then we'll have another one called director that'll be a string there we go and one called year and make that an integer 16. don't leave the model editor yet because while we're here i want to go across the view menu over here then look for inspectors and you'll see a data model inspector down here press that one now and this box on the right will appear full of interesting options to control our information now when you can select the attributes you see options of the attributes but i want you to choose the entity itself movie we're now seeing the options to control the way movies used in core data in particular i'd like to look at the way it says code gen this is uh the way xcode generates code code gen for the model we have here the movie entity and you see it set to by default to be class definition i'd like to change that to be manual slash none which gives us full control over how that chordata class is made so with that change xcodes are now no longer generating a movie class for us to use in code so we can't use it in code unless we actually make the class with some real swift code to do that go to the editor menu up here and then choose create ns manage object subclass make sure your project here is checked your model is checked then press next and then check movie and then press next and then press create here ideally inside the group like this more organized get it in here there we go and these new files appear so what we've done is we've asked xcode to convert it generated code what it would have done for us anyway into actual swift files we can see and change but keep in mind if you change these files and then regenerate that set we did a minute ago the same object subclass again your changes will be lost they'll be thrown away so change these very very carefully um you'll see two files made for us here the classes here and properties here inside properties you'll see a bunch of things and particularly these three lines of code here are title director and year and you'll see now this is where our optionality problem comes from it makes title and director both optional it doesn't make year optional it'll assume a default value for us with that but title and director are optional and also notice ns managed his here at ns managed i know it looks like a property wrapper it's not a property wrapper this stuff pre-dates property rappers by a long long way this is much much older instead this reveals a little bit of how core data works internally rather than these three values here actually existing as properties on the class they're really just there to read and write from a dictionary that core data will use behind the scene to store its information that'll wrap us around that effectively and what this means is uh when we read or write title or director or a year anything is managed caller basically catches that request give me title no no i'll intercede that and handles it internally in its magical correlator ways it is far from a simple swift string now you might think you know i don't want optionals here i'm going to go ahead and just like delete the optionals and you know you can totally do that that will work right that will absolutely work you can make movie objects with the same code as before uh fetch requests yadda yadda all works great and have no optionality here and it will still work fine however you might notice something strange which is that even though our properties aren't optional anymore we're saying must be a title must be a director must be a year you'll find it's still possible to make a movie instance without those values and that ought to be impossible they are not optional they must have values all the time but we can still create one without values being there and again it's just a little bit of how ns managed to work this kind of the magic leaking out from correlator a little bit these are not real properties they're not really there as a result ns manages that is do things that shouldn't be possible that should that's just not should be allowed to work bluntly the fact that it does work is neat you can hack away at that and good luck to you and i think for small projects and for learner projects you know fill your boots go for it but there's a deeper problem which is that core data is lazy and lazy in a good way not lazy in a bad way uh you know swift has a lazy keyword built in for doing lazy loading of data don't use this until i actually have to use it the first time and then do the real work of creating it that's what it's there for corey does exactly the same thing it understands um i yeah i've totally got a title and a director in a year there's absolutely 100 there promise and it isn't really when you ask for it then it'll be loaded so lazy load as much as it can and this is really really helpful for things like if you have a whole school of data and a school has many classrooms and classrooms with many students if you load the whole school at once you might have to load 50 classrooms and a thousand students depending on the size of your school that's a lot of information being loaded for one school so call it and say yeah i've got classrooms trust me it's totally there it's not there you read a classroom then it finally loads a classroom and you read the students that find loads of students but does so lazily on demand as it needed and it calls these things false in the sense of like a a fault line like a line between where something exists and something that's really just a placeholder fault anyway we don't have to do any special work to handle these faults because as soon as we try and read students or title or classrooms whatever it's trying to read so let's try and read it coins will transparently fetch those for us it'll just do it silently it's another benefit ns managed keyword right here is lots of work for us however if we start futzing around with the types of core data's properties we get a risk of exposing that sort of magical underbelly a little bit and this thing specifically and intentionally does not work the way the rest of swift does the rest of works differently to this intentionally if we try and circumvent that to make our life easier we're just inviting problems into our code and so you know values said definitely won't be nil might suddenly manill at any point in the future so just don't do it it's not really worth the risk instead a better option i would say is to add computed properties around your values they'll read the optional values out there while also storing your nil coalescing in one place for example we have title here on our movie you could say i want to have a public var wrapped title string and a return title or nil co-letting down to unknown title and so the whole rest of your code will read rap title everywhere it wants to without having to worry about no coalescing because it's all done in one central place so the horizon occurred isn't going to worry about the way according to handled optionality that's done for you but if you want to make changes to the default values you can do so in one single file
Info
Channel: Paul Hudson
Views: 374
Rating: undefined out of 5
Keywords: xcode, swift, swiftui, ui, ios, programming, tutorial, example, project, code, uikit
Id: pIAH3RVVXZE
Channel Id: undefined
Length: 9min 7sec (547 seconds)
Published: Fri Nov 26 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.