What an amazing tutorial, no pay walls no promotions, code is in the description including all links, he could have put this behind a 100 dollar paywall but instead he is helping everyone learn for free hats off
Hi mr Stewart i just found out your channel. First of all thank you for your time giving the community free content and second i wish you many years of health and prosperity.
Also after my comments on the issue below, a big "Thank You" for all you videos here, Stewart. They really rock! I especially liked the part on the file and database structure of the SQLite database under the hood in this one. Always wanted to understand this. And now I do. Again, thanks you!
Clarity is the signature of your videos as is your ability to find the perfect «SwiftUI way» of doing things. The ".onDelete" is a very efficient way to handle a quick deletion, but I wonder why no one has found a way to elegantly add an alert into this process when the consequence of the deletion is more important. This is not unusual and several Apple applications include this double confirmation. Thank you for your work; There's not a single video where I don't learn something really useful.
After 2 weeks in CoreData I cant believe how streamlined much of the code is.. much more pleasant to read. I found the use of a browser app really neat (though I wish it had diagrams or some visual layout of the dataModel). Instead of following along from the start I watched the video first to see what the UI will look like so following along ll make more sense. Great start to a new series !
Great video! One thing I noticed running on the simulator was that the date display seems to switch between mm/dd/yy format and Mmm D, yyyy format. This seems to only happen for the initial value. If I edit the date it reverts to the month name format. I also notice that if I change the date, and then change it back to the original, the update button goes away. I had a good time with this! Thanks for your hard work making this video!
Five stars! Once again, such a well-presented lesson! I really appreciate you adding the fix in the date update in the edit view. SwiftData is a real game-changer, so efficient and approachable. I have a number of apps that utilize Core Data, one of these days, I need to start updating them to Core Data. Thanks again for another great lesson, I look forward to the fire additions.
In your example, the first time you run the application, the database is empty. How can I ensure that the minimum database is loaded when the application is first launched? For example, after the first installation there were already 10 books with descriptions.
On the first view that loads where it preforms the query on books, you can check the count. If it is 0, you can call a function that will insert your 10 books into the database.
@StewartLynch I agree with pretty much everyone here: amazing video, topic, pace, explanation. I only have one suggestion if I may: avoid the continuous zooming in and out in Xcode. As someone who suffers strobing lights and these kind of "movements" I'm really struggling to follow (to the point of being phisically sick). In fact, I had to stop at 20 minutes in, a few hours ago, and I've only just come back to resume (but still really suffer and I'm not sure I can finish the video and the series). I'm still learning Swift with HWS and SwiftData is something I'm really intrigued by and I try to delve deeper at every occasion. Keep up the good work and I hope you could consider my suggestion. It would make more widespread and accessible. Cheers.
I zoom in because I want people to read the code on smaller devices. I find that some content providers text is too small to read. I hear what you are saying though and I will try to be more judicious in my zoom and pans
Hi@@StewartLynch I perfectly understand, don't worry and thank you for considering it. I noticed that for example Sean Allen's "smooth zooms" don't have the same effect and that I can follow easily. Don't take this the wrong way, I men it as a suggestion to help both of us out. Cheers :)
I'm in the process of updating my UIKit app to SwiftUI. Currently it is using CoreData + Cloud sync and MVVM pattern. The latter seems to pose a problem with SwiftData and I'm curious whether migrating to SwiftData is possible for me. So I'll follow along this series to find out if I can ... Thanks!
Thanks for a great Intro lesson into SwiftData and CRUD. Everything's great but there's one little bug: when we click on the book from the list for a brief moment we DON'T see the NavigationTitle and we SEE that "Update" button from the Toolbar which we shouldn't suppose to see at all if any data about the book has been edited. Although we see it. It's not a disaster but I'm sure you will agree that it's annoying and not acceptable in professional production level App. Did you notice it too ? What are the possible solutions to get rid of it? Thank you !
@@StewartLynch Of course. That's what I'm saying. For educational purposes it's fine though as a meticulous person I'd be glad to know and understand why that lag occurs and how to fix it for Production App. It's absolutely not about you doing a bad job as a tutor, please don't get me wrong.
I’ve noticed that when I create a book and do as you do. Set it’s added date to 2 months prior, then change status to in progress the date started gets set to the date added. In your running the simulator yours didn’t do that. I’ve copied the code directly from your repo. Not sure why, I guess that’s the way it’s suppose to work. Can’t figure out if I’ve done something wrong. As always love the content. Very informative and very well explained. You are one of a few people I go to to learn something new. Thanks for all you do for then Swift, SwiftUI community my friend.
@johndoe276. It's even more complicated ... if the book.status is .onShelf, the .onChange method is never called during opening of the view, thus firstView is again not set to false. Manually setting firstView to false fixed this for me in the .onAppear method: if status == book.status { firstView = false } else { status = book.status } Pheeewww .. complicated one. Maybe making the @State variables optional would have avoided this. But would have introduced a lot of code dealing with the optionals then ... Stewart, what do you think? Thanks! Reply @tazman7689 @StewartLynch I experienced exactly the same behavior. First, I did my own code just following the video and I had that same strange behavior. Then I copied all the code given by Stewart as well, considering that maybe I had had some issue in my code, but the result was exactly the same even using the Stewart's code. To try to find a posible cause, I'm using a MacBookPro 16-inch, 2021, Apple M1 Pro, 16 GB, Sonoma14.0. What are yours?
I am loving the tutorial so far, and learning not only the topic, but lots of little extras as well. One problem I have had so far is that when I relied on the init in the Book class description to populate the optionals, the newBook call in the create button was unjhappy with the optional parameters so I had to define them there in the create code. Is there something wrong with the way I set up the Book init?
Thanks for the Videos, much appreciated. I do have a problem with the .onDelete in the first video: my book is not deleting from the database unless i save it after the delete code. Maybe I've missed a step somewhere, and insight would be appreciated. Judy
Things have changed in Xcode 16 in that the save does not appear to be happening immediately. Seems to wait until app goes to the background. I suggest you issue a try? context.save() to force it.
Great video Stewart! Thanks for the SwiftData introduction! As you will include CloudKit integration in the coming episodes, any chance SwiftData supports CloudKit sharing among different users?
Great video as always, Stewart 👍 I guess I found the cause for the misbehavior also others were reporting: Since the toggling of firstView to false is done within the [ if !firstView { ... } ] code block, it is never set to false and thus the block is never executed. I believe the [ firstView = false ] row needs to be moved down one line, i.e. outside of the [ if !firstView { ... } ] code block. For me this fixed the issue, that the dates always show up on .distantPast. Could you check, please? Thanks!
It's even more complicated ... if the book.status is .onShelf, the .onChange method is never called during opening of the view, thus firstView is again not set to false. Manually setting firstView to false fixed this for me in the .onAppear method: if status == book.status { firstView = false } else { status = book.status } Pheeewww .. complicated one. Maybe making the @State variables optional would have avoided this. But would have introduced a lot of code dealing with the optionals then ... Stewart, what do you think? Thanks!
Hi, Stewart; thank you for your video. It helped me set up my dataset in SwiftData!!! I only have one issue with putting in Int data by the form. Do you have any ideas on how to deal with that?
Sorry, but I do not understand the question, Feel free to email me or DM me on Mastodon/X with your model so I can see what you are referring to along with some more explanation as to what difficulties you are having
Great video! In your video, it should be in Book List View() .model Container(for: Book.self) at 5.54. I was getting an error in preview. Apple says it should be in its library. I'm looking forward to the rest of your video
You can either store the photo locally in your documents directory by giving it an id and then store the id in the model, or you can store the image as a Data property. I do this in my My Images application series on Core Data so the process is the same.
Great tutorial 👏🏼👏🏼👏🏼 I have a question. What to do or how to pass the book that is Clicked on in the Listrow to the Editview when you show the Editview in a Sheet? The sheet modifier is always outside the ForEach loop So you can’t pass the current or active (clickedOn) book in the sheet. How can I pass the ckicked book to that Sheet, that displays the EditView instead of a NavigationLink.
Nit sure I understand your question in relation to this project. Feel free to follow up with me by email. My email is in all of my source code as the file heaer
@@StewartLynch Hi Stewart, excuse me for not being clear enough. It is more a What If question. What If you Edit the book in the Editview, but the Editview is not presented through a NavigationLink but a Sheet that pops up. How do you (then) pass the Book (object) that you clicked on in the List, to the Sheet (that presents the EditView? Hopefully you understand my question now?
The list down can't be a NavigationLink then because that will always push on to a navigation stack. If you want to use a sheet to edit, then if your sheet iterates through an array of those books then you can capture it in the button that you use to trigger the presentation of the view. I like to use the .sheet(item...) presentation where the item is some Optional identifiable object rather than based on a Boolean value. Then, I create an enum to use as the object and make sure it is identifiable and each case has an associated values so for example: (Warning: I am typing this freehand without code checking so there could be some typos here) enum SheetPresenter: Identifiable { case selectedBook(Book) var id: String { String(describing: self) } } Then in your view, you can have @State private var selectedBook: SelectedBook? When you tap on the book, if your iterator for the list is book, you can set selectedBook = .selectedBook(book() The sheet will be presented with .sheet(item: $selectedBook) { book in EditView(book: Book) }
@@StewartLynch Awesome Stuart, it works! Thank you 🙌🏼 (I didn’t use the enum) But I have one problem; When I pass the book to the EditView (via the sheet) I Cannot use the Book-property ‘Name’ in a TextField. It gives me the error that Name is of type String and it needs a Binding. How can I make that work?
Very nice video and a great introduction to SwiftDate. Getting started seem too easy easy, but then the going gets tougher. DB Browser is a very nice app and a great addition to my helper apps. It seems that visualization is done on a db copy obtained by a query? Is that correct, or did I misunderstand? At least the modification is done on individual data then copied onto the table last minute after verification. Because SwiftData updates at once? Did I understand correctly? A question: Would it have been possible to create a book unconnected to the database and working copy until added. to the database in the last section of the video? The ratings view is a nice reusable I hurry into the following videos. Thanks a lot
Hi Stewart. Your content is great. I only have one small request. Can you rename these video titles to specify, Part 1, Part 2, Part 3 etc. as it is confusing on which video to watch first in the series. Thank you.
Great video as always. One issue I seem to be having is that after I add a book and update its dateStarted value, then update, then edit the book again to change the status to completed the dateCompleted is initially set to Dec 31, 1 (i.e., in the very distant past). This happens even though I did make the change to add a range as in the video (which worked in the case of dateStarted). When I changed the initial value of dateCompleted to Date.now from Date.distantPast the problem went away and the picker starts at dateStarted as it is supposed to. Have you seen this issue?
Thank you very much for the great video! Just a quick question when setting the @State attributes of your Book-class: Why did you prefer setting the values in an .onAppear rather than in a custom init? Wouldn’t an init make the use of the firstView-Bool obsolete? Thanks in advance!
Hi, I’m continuing to study SwiftData by repeating all the tutorials but I can’t understand one thing: I have to create an app with a default list (which therefore no data needs to be added) and this list is contained in a toggle. How do I make only the toggle state persistent?
Hi everyone! I really like this set of tutorials. Much needed for my developement goals. I am having one issue in the Update Section. I am using xCode 15.3 and IOS version 17.4 for my project. In the update section I have followed the coding but I am not getting a preview, having a compile error where it seems that my @State private var ... are causing initializer is inaccessible due to 'private' protection level. Could this be caused by me using the bleeding edge of xCode and IOS ? Am I missing something simple. Thanks
Going back over my code I could not find an issue in the code I typed. What I did to resolve this issue is I closed many Chrome Browser Windows and retype the code carefully. I suspect that some kind of issue occurred in XCode and it corrupted the Project File somehow. I used Beyond Compare and I could not find the issue that way, so I just had to recreate the project afresh. Just posted my findings, so that others might better understand my resolution to my issues and may comment as they like.
Nice video, thankyou Once again Mr Lynch it would be more helpful for developers who are more 'hands on' defining database tables directly &setting up foreign key relationships directly using the Xcode(coredata data modeler)& using it in SwiftUI , BTW,I happened to watch 'Karin Prater' video, 'I am lost' !, , she has so much materials online, learned few things from her videos (not complaining, perhaps the way I learn is different!), so I would truly appreciate if you could create one (as you know especially in real productivity apps, you will have many tables & FK relationships , so defining tables with UI interface is better)
This video is about SwiftData, not CoreData so you don’t do it the same way as CoreData. There is much more to this as this is the first in an entire series
can this be done as easily on the Apple Watch? I am unable to access the folder to read the file. I wondering if it is because I am not doing it on a phone
Hi Stewart ! Quick help please. I’m a cs student and I am now starting to plan my first app using SwiftUI. I never used Swift Data, Core Data, nor Realm, but now I need to know which one to use for my project. It will be a war card game, each card will have its own name and image, plus 5 attributes… I heard that Core Data is a bit more difficult option. Do you think I could use Swift Data or better go with Core Data or realm, or firebase.Thanks very much!!
Hey great video as always!!! I tried to add a Category Model and implement a one to many relationship. But I always run into the same error: Thread 1: Fatal error: failed to find a currently active container for Category. Eventhough I changed the modelContainer in the Main file. Do you have any tips regarding that. I would be very grateful.
I’m working on an app that has two main views in a tab view. One is sort of an “albums owned” and the other is a wishlist for albums the user wants to buy. I’ve just made the app with swift data with the crud functions and all; but how do I manage more than 1 model (ie. An album list for owned/want)?
I would use a single model and just add a property to the Album class that is a boolean called isOwned and make it true for those owned and false for the others. Then you can do a different query for each tab. One where isOwned is true and the other where isOwned is false.
@@StewartLynchjust wanted to update in case anyone else had a similar issue as myself: I used your advice on adding a Boolean value to my model and with some help from hacking with swift, I learned how to use a filter with #predicate and display “albums” by owned vs not owned and added a toggle to my form for the user to turn on or off! I can’t say I’m well versed enough with swift data, but I was able to get my app fully functional with this help. Thanks a ton!
Maybe a dumb question, but how can i go from Firebase to SwiftData? Right now i am working on a recipe app, in Firebase all the recipes are stored and fetched when needed, i also use firebase for user verification. I want to only use Firebase for User verification. So how can i use SwiftData to get the same functionality? Users should access the recipes i provide for them, there is no way to add their own, they can just leave a rating and a comment. Or is Firebase still the better option for my use case?
Hello Stewart, Thank you for your video. In the minute 13:48, I see the next error: "This application, or a library it uses, has passed an invalid numeric value (NaN, or not-a-number) to CoreGraphics API and this value is being ignored. Please fix this problem." Later this error just disappeared, and I was not able to catch how did you fix it. Can you tell us about that kind of Error and what actions must be taken. Thanks.
Hey Stuart, this is awesome video, but I have a urgent question: I have a game object that has a location that is set up as optional. Can/should I make that non-optional? Do I have to make an explicit relationship or can I use the implicit version. If you’d like me to make a relationship, which object, the game, or the location would I choose to place the @relationship? (it would seem impossible to add it to the location, because there’s no ‘game’ listed there. in other words, locations don’t know about games, but games know about locations. (The game must have exactly one location no more. So there’s no arrays.) I’m setting up a new object with pickers from all the locations and then the use hits a CREATE button, and it inserts to the database. How can I avoid having nil assignments for location? I’m having a problem with records being inserted in the database with nil. Thanks so much. I love the channel. And if any of your readers can help me out. That would be amazing.
Where are the pickers getting the locations from? A locations table or are they hard coded? Instead of nil, you could have a ‘fake’ location like I do with date and make this the initial default. Your view could check to see if your object has fake location property and if so, present the picker where the first item in the picker is the fake location tag but the view says “Pick a Location”
OK, I just found a bug. That I think solve my problem. I was trying to use a location from the last used game as the default location for the new game, but I ran into an issue when I’m starting with no records, it doesn’t have anything to pick from, and it defaulted to nil. So I just assigned to a random location to begin with, and if it finds a previous game, then it can use that. Thanks!
General question with SwiftData: Is this set up correct? ----------------------------------------------------------- Game needs ONE location Locations can be assigned to many different games. @Model final class location { var id: UUID var text: String } @Model final class game { @Attribute(.unique) var id: UUID … var location: location? }
If locations can be assigned to many games, you have a one to many relationship from location to games so in location you will need a var games: [Game]?
@@StewartLynch Thanks! I'm still confused though. By having var location: location? (inside game class), doesn't this create an implicit relationship both ways? My app runs w/o errors. What am I missing?
@@chezchezchezchez I am confused by what you are saying, Probably best to continue this conversation via email as it is off topic for this video. I cover relationships in subsequent videos in this series
1. Enable Code Folding Ribbon: • Open Xcode. • Go to Xcode in the menu bar, then select Preferences (or press Cmd + ,). • In the Preferences window, go to the Text Editing tab. • Check the box next to Code folding ribbon under the Editing section. 2. Using Code Folding: • Once the code folding ribbon is enabled, you will see small triangles next to the line numbers in the editor. • Click on a triangle next to a function, class, or other code block to fold (collapse) that block of code. • Click on the triangle again to unfold (expand) the block of code. 3. Keyboard Shortcuts: • You can also use keyboard shortcuts to fold and unfold code blocks: • Cmd + Option + ← to fold the current block of code. • Cmd + Option + → to unfold the current block of code. • Cmd + Option + Shift + ← to fold all code blocks. • Cmd + Option + Shift + → to unfold all code blocks. 4. Fold and Unfold Specific Sections: • Place the cursor at the start or end of a code block. • Use the keyboard shortcuts mentioned above to fold or unfold the specific section. 5. Code Folding Actions: • Right-click in the editor and select Fold or Unfold to fold or unfold the current block. • Select Fold All or Unfold All from the right-click context menu to fold or unfold all code blocks in the file. By enabling and using code folding, you can keep your code more organized and focus on the parts you’re currently working on.
Hi Great video and I'll be watching the series. Just one little thing not sure if its a bug but when you change the status of a book to completed the date picker stays in the distant past and it never updates to the current date unless you manually change it in the picker itself. It does the same behaviour in the competed app from GitHub as well. the date stays at Dec 31, 1 like the year 2001 I think. Is anybody else getting this or am I just thinking about this wrong?
What if you go from onShelf to inProgress then Completed? My code may not be perfect, but the SwiftData stuff is still good. See if you can improve the code and share.
@@StewartLynch Yep it does the same thing, just for some reason it refuses to update the dateCompleted property until you use the picker. Ive tried rewriting it as a switch rather then If then else statements (no luck) one thing that does make it behave a little bit more constantly is changing the Datepicker for dateCompleted to this: DatePicker("", selection: $dateCompleted, in: dateAdded...Date.now, displayedComponents: .date) that at least should give a date that's closer to what somebody would want and not constantly setting itself to the distant past that's the best solution I have for now.
hi can we make call recording app in ios i want to make one for me..... there are many apps on appstore but they all are paid app i want to build for free.
Hi Stweart, big thanks from beginner developer. Great free content. Can anyone explaine to me, how and when firstView change state from true to false? Because in this example never get into this block of code. It's always true .onChange(of: status) { oldValue, newValue in if !firstView { if newValue == .onShelf { dateStarted = Date.distantPast dateComplited = Date.distantPast } else if newValue == .inProgress && oldValue == .completed { // from completed to inProgress dateComplited = Date.distantPast } else if newValue == .inProgress && oldValue == .onShelf { // book has been started dateStarted = Date.now } else if newValue == .completed && oldValue == .onShelf { // forgot to start book dateComplited = Date.now dateStarted = dateAdded } else { // completed dateComplited = Date.now } firstView = false } }
HI Stewart. On the Edit/Update I'm trying to pass an Int? into a Picker as below. If I put gidata.growthYears in a Textfield it shows up fine but when I try to add it to my Picker the Picker show up but nothing is selected as default nor can I select anything. Any tips or suggestions let gidata: myModel @State private var yrsSelectedArray: [Int] = [1, 5, 10] Picker("Years for Simulation:", selection: .constant(gidata.growthYears)) { ForEach(yrsSelectedArray, id: \.self) { Text(String($0)) }
What an amazing tutorial, no pay walls no promotions, code is in the description including all links, he could have put this behind a 100 dollar paywall but instead he is helping everyone learn for free hats off
Hi mr Stewart i just found out your channel. First of all thank you for your time giving the community free content and second i wish you many years of health and prosperity.
Great training video. I liked Stewart's pacing, not too fast, and showed how to check the SwiftData backend data with Xcode.
Also after my comments on the issue below, a big "Thank You" for all you videos here, Stewart. They really rock!
I especially liked the part on the file and database structure of the SQLite database under the hood in this one. Always wanted to understand this. And now I do. Again, thanks you!
Thanks for posting your solution. I wish RUclips allowed me to edit a video without having to upload an entirely new one with a new link.
The best video about swiftdata crud operations. By a large margin. Thank you!
Wow, thank you!
我才看完一小段,但已经忍不住要为您点赞!👍
Clarity is the signature of your videos as is your ability to find the perfect «SwiftUI way» of doing things. The ".onDelete" is a very efficient way to handle a quick deletion, but I wonder why no one has found a way to elegantly add an alert into this process when the consequence of the deletion is more important. This is not unusual and several Apple applications include this double confirmation. Thank you for your work; There's not a single video where I don't learn something really useful.
Best SwiftData video I have watched. The instructions are precisely clear and informative. Thanks Stewart.
I am happy that you think so
After 2 weeks in CoreData I cant believe how streamlined much of the code is.. much more pleasant to read. I found the use of a browser app really neat (though I wish it had diagrams or some visual layout of the dataModel). Instead of following along from the start I watched the video first to see what the UI will look like so following along ll make more sense. Great start to a new series !
Thanks Stewart! Love this introduction to SwiftData. Looking forward to getting to the rest in the series. 🙂
teaching the next generation of mobile devs. thank you, sir 🤝🏾
That's the plan!
Great pace and super clear explanations! The trick for getting the path to the db file was :chef-kiss:
Nice intro to SwiftData. Really clear explanation. I'm sure there is a lot more too it than this first intro covers... so on to the next session.
Great video! One thing I noticed running on the simulator was that the date display seems to switch between mm/dd/yy format and Mmm D, yyyy format. This seems to only happen for the initial value. If I edit the date it reverts to the month name format. I also notice that if I change the date, and then change it back to the original, the update button goes away. I had a good time with this! Thanks for your hard work making this video!
I think this is a SwiftUI bug. I noticed that too.
What a video man! Clear explanation and very good examples.
best tutorial video i have ever seen, thanks Stewart
Wow, thanks!
Five stars! Once again, such a well-presented lesson! I really appreciate you adding the fix in the date update in the edit view. SwiftData is a real game-changer, so efficient and approachable.
I have a number of apps that utilize Core Data, one of these days, I need to start updating them to Core Data.
Thanks again for another great lesson, I look forward to the fire additions.
Hey Stewart ...just wanna let you know...that in love your work...
Thanks a million for your amazing content! I just wanted to let you know you're the best at what you do.
Thank you. There are a lot of people providing great Swift/SwiftUI content. I am pleased that my style resonates with you.
I want to also give you a shout out for showing me a very simple way to find the raw file that SwiftData is stored in :)
In your example, the first time you run the application, the database is empty. How can I ensure that the minimum database is loaded when the application is first launched? For example, after the first installation there were already 10 books with descriptions.
On the first view that loads where it preforms the query on books, you can check the count. If it is 0, you can call a function that will insert your 10 books into the database.
Thank you so much for the amazing tutorial! For whatever reason, data doesn't seem to persist in iOS 18. Not sure what would be causing it.
It will take about 30 seconds. You can force it with a try? context .save()
@StewartLynch I agree with pretty much everyone here: amazing video, topic, pace, explanation. I only have one suggestion if I may: avoid the continuous zooming in and out in Xcode. As someone who suffers strobing lights and these kind of "movements" I'm really struggling to follow (to the point of being phisically sick). In fact, I had to stop at 20 minutes in, a few hours ago, and I've only just come back to resume (but still really suffer and I'm not sure I can finish the video and the series). I'm still learning Swift with HWS and SwiftData is something I'm really intrigued by and I try to delve deeper at every occasion. Keep up the good work and I hope you could consider my suggestion. It would make more widespread and accessible. Cheers.
I zoom in because I want people to read the code on smaller devices. I find that some content providers text is too small to read. I hear what you are saying though and I will try to be more judicious in my zoom and pans
Hi@@StewartLynch I perfectly understand, don't worry and thank you for considering it. I noticed that for example Sean Allen's "smooth zooms" don't have the same effect and that I can follow easily. Don't take this the wrong way, I men it as a suggestion to help both of us out. Cheers :)
Can't believe this is available for free! thanks mucho :)
I'm in the process of updating my UIKit app to SwiftUI. Currently it is using CoreData + Cloud sync and MVVM pattern. The latter seems to pose a problem with SwiftData and I'm curious whether migrating to SwiftData is possible for me. So I'll follow along this series to find out if I can ... Thanks!
Thanks for a great Intro lesson into SwiftData and CRUD. Everything's great but there's one little bug:
when we click on the book from the list for a brief moment we DON'T see the NavigationTitle and we SEE that "Update" button from the Toolbar which we shouldn't suppose to see at all if any data about the book has been edited. Although we see it. It's not a disaster but I'm sure you will agree that it's annoying and not acceptable in professional production level App.
Did you notice it too ? What are the possible solutions to get rid of it?
Thank you !
I would have to take a look. This is not meant as a professional app though. The main objective is to show SwiftData.
@@StewartLynch Of course. That's what I'm saying. For educational purposes it's fine though as a meticulous person I'd be glad to know and understand why that lag occurs and how to fix it for Production App.
It's absolutely not about you doing a bad job as a tutor, please don't get me wrong.
@yuriichynchyk1380 it could also be a SwiftUI bug
@@StewartLynch Sure. It's absolutely possible! When you have a chance please look to it, Stewart. Many thanks!
I’ve noticed that when I create a book and do as you do. Set it’s added date to 2 months prior, then change status to in progress the date started gets set to the date added. In your running the simulator yours didn’t do that. I’ve copied the code directly from your repo. Not sure why, I guess that’s the way it’s suppose to work. Can’t figure out if I’ve done something wrong.
As always love the content. Very informative and very well explained. You are one of a few people I go to to learn something new. Thanks for all you do for then Swift, SwiftUI community my friend.
@johndoe276.
It's even more complicated ... if the book.status is .onShelf, the .onChange method is never called during opening of the view, thus firstView is again not set to false. Manually setting firstView to false fixed this for me in the .onAppear method:
if status == book.status {
firstView = false
} else {
status = book.status
}
Pheeewww .. complicated one. Maybe making the @State variables optional would have avoided this. But would have introduced a lot of code dealing with the optionals then ...
Stewart, what do you think? Thanks!
Reply
@tazman7689 @StewartLynch
I experienced exactly the same behavior. First, I did my own code just following the video and I had that same strange behavior. Then I copied all the code given by Stewart as well, considering that maybe I had had some issue in my code, but the result was exactly the same even using the Stewart's code. To try to find a posible cause, I'm using a MacBookPro 16-inch, 2021, Apple M1 Pro, 16 GB, Sonoma14.0. What are yours?
@@G_Monge Thanks even I were struggling at this
I am loving the tutorial so far, and learning not only the topic, but lots of little extras as well. One problem I have had so far is that when I relied on the init in the Book class description to populate the optionals, the newBook call in the create button was unjhappy with the optional parameters so I had to define them there in the create code. Is there something wrong with the way I set up the Book init?
Not sure I understand the question James. Feel free to email me to follow up
Ok. I figured it out, no need to reply.
Fantastic video! I’ve been waiting for this one to be made. Thank you so much. Looking forward to the next episodes! 👍👍👍👍
Great video!... Curious as to why you used onDelete instead of swipeactions ? Performance or more swifty?
No particular reason here. It was the first option that came to me.
Thanks for the Videos, much appreciated. I do have a problem with the .onDelete in the first video: my book is not deleting from the database unless i save it after the delete code. Maybe I've missed a step somewhere, and insight would be appreciated. Judy
Things have changed in Xcode 16 in that the save does not appear to be happening immediately. Seems to wait until app goes to the background. I suggest you issue a try? context.save() to force it.
Great video Stewart! Thanks for the SwiftData introduction! As you will include CloudKit integration in the coming episodes, any chance SwiftData supports CloudKit sharing among different users?
No. Sharing is not yet possible.
Great video as always, Stewart 👍
I guess I found the cause for the misbehavior also others were reporting: Since the toggling of firstView to false is done within the [ if !firstView { ... } ] code block, it is never set to false and thus the block is never executed. I believe the [ firstView = false ] row needs to be moved down one line, i.e. outside of the [ if !firstView { ... } ] code block.
For me this fixed the issue, that the dates always show up on .distantPast. Could you check, please? Thanks!
It's even more complicated ... if the book.status is .onShelf, the .onChange method is never called during opening of the view, thus firstView is again not set to false. Manually setting firstView to false fixed this for me in the .onAppear method:
if status == book.status {
firstView = false
} else {
status = book.status
}
Pheeewww .. complicated one. Maybe making the @State variables optional would have avoided this. But would have introduced a lot of code dealing with the optionals then ...
Stewart, what do you think? Thanks!
Hi, Stewart; thank you for your video. It helped me set up my dataset in SwiftData!!! I only have one issue with putting in Int data by the form. Do you have any ideas on how to deal with that?
Sorry, but I do not understand the question, Feel free to email me or DM me on Mastodon/X with your model so I can see what you are referring to along with some more explanation as to what difficulties you are having
Great video, thank you very much!
Great video! In your video, it should be in Book List View() .model Container(for: Book.self) at 5.54. I was getting an error in preview. Apple says it should be in its library. I'm looking forward to the rest of your video
Thanks and five Stars Stewart ⭐⭐⭐⭐⭐ . What is needed to upload a cover picture as well in the program? Can you maybe provide a hint.
You can either store the photo locally in your documents directory by giving it an id and then store the id in the model, or you can store the image as a Data property. I do this in my My Images application series on Core Data so the process is the same.
I will try to find a solution and provide a feedback.@@StewartLynch
Great tutorial 👏🏼👏🏼👏🏼 I have a question. What to do or how to pass the book that is Clicked on in the Listrow to the Editview when you show the Editview in a Sheet?
The sheet modifier is always outside the ForEach loop So you can’t pass the current or active (clickedOn) book in the sheet. How can I pass the ckicked book to that Sheet, that displays the EditView instead of a NavigationLink.
Nit sure I understand your question in relation to this project. Feel free to follow up with me by email. My email is in all of my source code as the file heaer
@@StewartLynch Hi Stewart, excuse me for not being clear enough. It is more a What If question. What If you Edit the book in the Editview, but the Editview is not presented through a NavigationLink but a Sheet that pops up. How do you (then) pass the Book (object) that you clicked on in the List, to the Sheet (that presents the EditView? Hopefully you understand my question now?
The list down can't be a NavigationLink then because that will always push on to a navigation stack. If you want to use a sheet to edit, then if your sheet iterates through an array of those books then you can capture it in the button that you use to trigger the presentation of the view. I like to use the .sheet(item...) presentation where the item is some Optional identifiable object rather than based on a Boolean value. Then, I create an enum to use as the object and make sure it is identifiable and each case has an associated values so for example:
(Warning: I am typing this freehand without code checking so there could be some typos here)
enum SheetPresenter: Identifiable {
case selectedBook(Book)
var id: String {
String(describing: self)
}
}
Then in your view, you can have
@State private var selectedBook: SelectedBook?
When you tap on the book, if your iterator for the list is book, you can set
selectedBook = .selectedBook(book()
The sheet will be presented with
.sheet(item: $selectedBook) { book in
EditView(book: Book)
}
@@StewartLynch Thanx Stewart, I’m going to try this. I’ll let you know 🙏🏼
@@StewartLynch Awesome Stuart, it works! Thank you 🙌🏼 (I didn’t use the enum) But I have one problem; When I pass the book to the EditView (via the sheet) I Cannot use the Book-property ‘Name’ in a TextField. It gives me the error that Name is of type String and it needs a Binding. How can I make that work?
Too easy to follow, thank you so much!
Very nice video and a great introduction to SwiftDate.
Getting started seem too easy easy, but then the going gets tougher.
DB Browser is a very nice app and a great addition to my helper apps.
It seems that visualization is done on a db copy obtained by a query?
Is that correct, or did I misunderstand?
At least the modification is done on individual data then copied onto the table last minute after verification.
Because SwiftData updates at once? Did I understand correctly?
A question: Would it have been possible to create a book unconnected to the database and working copy until added. to the database in the last section of the video?
The ratings view is a nice reusable
I hurry into the following videos.
Thanks a lot
I chose to do updates after editing, but as you will see with other views later on, I bind an object so updating is done immediately
Hi Stewart. Your content is great. I only have one small request. Can you rename these video titles to specify, Part 1, Part 2, Part 3 etc. as it is confusing on which video to watch first in the series. Thank you.
Done. Thanks or pointing that out. You can also check out the playlist and go by the playlist.
@@StewartLynch Yes correct. I just saved the entire playlist. I just saw the videos numbered. Thank you so much Sir.
Great video as always. One issue I seem to be having is that after I add a book and update its dateStarted value, then update, then edit the book again to change the status to completed the dateCompleted is initially set to Dec 31, 1 (i.e., in the very distant past). This happens even though I did make the change to add a range as in the video (which worked in the case of dateStarted). When I changed the initial value of dateCompleted to Date.now from Date.distantPast the problem went away and the picker starts at dateStarted as it is supposed to. Have you seen this issue?
I think I fix this in a subsequent video
very clear .. Thank you..
Thank you very much for the great video! Just a quick question when setting the @State attributes of your Book-class: Why did you prefer setting the values in an .onAppear rather than in a custom init? Wouldn’t an init make the use of the firstView-Bool obsolete? Thanks in advance!
Possibly. I might have gone down the wrong path here. Free free to test it out and report back. Thanks for the feedback
Thank you very much for the reply! I will test it out and repot back! :-)
Somehow my datecompleted is always showing 1 jan 1. Can't figure out where that goes wrong???
Hi, I’m continuing to study SwiftData by repeating all the tutorials but I can’t understand one thing:
I have to create an app with a default list (which therefore no data needs to be added) and this list is contained in a toggle. How do I make only the toggle state persistent?
I don’t understand the question. Feel free to email me to follow up here
Hi everyone! I really like this set of tutorials. Much needed for my developement goals. I am having one issue in the Update Section. I am using xCode 15.3 and IOS version 17.4 for my project. In the update section I have followed the coding but I am not getting a preview, having a compile error where it seems that my @State private var ... are causing initializer is inaccessible due to 'private' protection level. Could this be caused by me using the bleeding edge of xCode and IOS ? Am I missing something simple. Thanks
My code runs fine on the latest builds so I suggest you recheck your code.
@@StewartLynch Thank you sir.
Going back over my code I could not find an issue in the code I typed. What I did to resolve this issue is I closed many Chrome Browser Windows and retype the code carefully. I suspect that some kind of issue occurred in XCode and it corrupted the Project File somehow. I used Beyond Compare and I could not find the issue that way, so I just had to recreate the project afresh. Just posted my findings, so that others might better understand my resolution to my issues and may comment as they like.
Nice video, thankyou Once again Mr Lynch
it would be more helpful for developers who are more 'hands on' defining database tables directly &setting up foreign key relationships directly using the Xcode(coredata data modeler)& using it in SwiftUI ,
BTW,I happened to watch 'Karin Prater' video, 'I am lost' !, , she has so much materials online, learned few things from her videos
(not complaining, perhaps the way I learn is different!),
so I would truly appreciate if you could create one (as you know especially in real productivity apps, you will have many tables & FK relationships , so defining tables with UI interface is better)
This video is about SwiftData, not CoreData so you don’t do it the same way as CoreData. There is much more to this as this is the first in an entire series
@@StewartLynchsorry I meant swift data
Amazing…. 💯 thank you
Thank you too!
can this be done as easily on the Apple Watch?
I am unable to access the folder to read the file. I wondering if it is because I am not doing it on a phone
Sorry. I do t understand your question
Hi Stewart ! Quick help please. I’m a cs student and I am now starting to plan my first app using SwiftUI. I never used Swift Data, Core Data, nor Realm, but now I need to know which one to use for my project. It will be a war card game, each card will have its own name and image, plus 5 attributes… I heard that Core Data is a bit more difficult option. Do you think I could use Swift Data or better go with Core Data or realm, or firebase.Thanks very much!!
Definitely SwiftData. Follow this series and you will get everything you need
@@StewartLynch Sweet!! Ty Stewart ! And thanks for such an excellent content 😊
Hey great video as always!!!
I tried to add a Category Model and implement a one to many relationship. But I always run into the same error: Thread 1: Fatal error: failed to find a currently active container for Category. Eventhough I changed the modelContainer in the Main file. Do you have any tips regarding that. I would be very grateful.
The next lesson in this series coming Nov 26 will show you this. My Ko-Fi patrons have it already.
Thanks for the quick answer. I'm looking forward for the video @@StewartLynch
Excellent tutorial!
Glad you liked it!
I’m working on an app that has two main views in a tab view. One is sort of an “albums owned” and the other is a wishlist for albums the user wants to buy. I’ve just made the app with swift data with the crud functions and all; but how do I manage more than 1 model (ie. An album list for owned/want)?
I would use a single model and just add a property to the Album class that is a boolean called isOwned and make it true for those owned and false for the others. Then you can do a different query for each tab. One where isOwned is true and the other where isOwned is false.
@@StewartLynchthanks, I really appreciate the reply. I will try that out later tonight! Great series by the way.
@@StewartLynchjust wanted to update in case anyone else had a similar issue as myself:
I used your advice on adding a Boolean value to my model and with some help from hacking with swift, I learned how to use a filter with #predicate and display “albums” by owned vs not owned and added a toggle to my form for the user to turn on or off!
I can’t say I’m well versed enough with swift data, but I was able to get my app fully functional with this help. Thanks a ton!
Maybe a dumb question, but how can i go from Firebase to SwiftData?
Right now i am working on a recipe app, in Firebase all the recipes are stored and fetched when needed, i also use firebase for user verification.
I want to only use Firebase for User verification. So how can i use SwiftData to get the same functionality?
Users should access the recipes i provide for them, there is no way to add their own, they can just leave a rating and a comment.
Or is Firebase still the better option for my use case?
Firebase is still the best option in this case
@@StewartLynch Thank you so much for the fast answer!
Hello Stewart, Thank you for your video. In the minute 13:48, I see the next error: "This application, or a library it uses, has passed an invalid numeric value (NaN, or not-a-number) to CoreGraphics API and this value is being ignored. Please fix this problem." Later this error just disappeared, and I was not able to catch how did you fix it. Can you tell us about that kind of Error and what actions must be taken. Thanks.
I didn’t do anything. I believe that this is just an Xcode bug and you can ignore it.
@@StewartLynch Thank you for your fast response. Excellent tutorial !!
Hey Stuart, this is awesome video, but I have a urgent question:
I have a game object that has a location that is set up as optional.
Can/should I make that non-optional?
Do I have to make an explicit relationship or can I use the implicit version. If you’d like me to make a relationship, which object, the game, or the location would I choose to place the @relationship? (it would seem impossible to add it to the location, because there’s no ‘game’ listed there. in other words, locations don’t know about games, but games know about locations.
(The game must have exactly one location no more. So there’s no arrays.)
I’m setting up a new object with pickers from all the locations and then the use hits a CREATE button, and it inserts to the database. How can I avoid having nil assignments for location?
I’m having a problem with records being inserted in the database with nil.
Thanks so much. I love the channel. And if any of your readers can help me out. That would be amazing.
Where are the pickers getting the locations from? A locations table or are they hard coded? Instead of nil, you could have a ‘fake’ location like I do with date and make this the initial default. Your view could check to see if your object has fake location property and if so, present the picker where the first item in the picker is the fake location tag but the view says “Pick a Location”
The pickers are getting the locations from the @Query of location table.
OK, I just found a bug. That I think solve my problem. I was trying to use a location from the last used game as the default location for the new game, but I ran into an issue when I’m starting with no records, it doesn’t have anything to pick from, and it defaulted to nil.
So I just assigned to a random location to begin with, and if it finds a previous game, then it can use that. Thanks!
Merci.
Thanks!
Thank you
General question with SwiftData:
Is this set up correct?
-----------------------------------------------------------
Game needs ONE location
Locations can be assigned to many different games.
@Model
final class location {
var id: UUID
var text: String
}
@Model
final class game {
@Attribute(.unique) var id: UUID
…
var location: location?
}
If locations can be assigned to many games, you have a one to many relationship from location to games so in location you will need a
var games: [Game]?
@@StewartLynch Thanks! I'm still confused though.
By having var location: location? (inside game class), doesn't this create an implicit relationship both ways?
My app runs w/o errors. What am I missing?
@@chezchezchezchez I am confused by what you are saying, Probably best to continue this conversation via email as it is off topic for this video. I cover relationships in subsequent videos in this series
Hey Stewart, how about to add a book's photo
I don't do that in this series, but I cover this with SwiftData in this series. ruclips.net/p/PLBn01m5Vbs4DLU9Yiff2V8oyslCdB-pnj&si=gst9JjeSy5f9oQXr
@@StewartLynch Aww thank you so much, i appreciate it StewartLynch
Danke!
Thank you very much. I am happy to give you access to all unreleased videos for this donation so send me an email and I will send you the links.
I don’t understand why passing book as a let you can updated and update model container
The context handles it for you.
how did you enable collapse feature in xcode 21:48....
1. Enable Code Folding Ribbon:
• Open Xcode.
• Go to Xcode in the menu bar, then select Preferences (or press Cmd + ,).
• In the Preferences window, go to the Text Editing tab.
• Check the box next to Code folding ribbon under the Editing section.
2. Using Code Folding:
• Once the code folding ribbon is enabled, you will see small triangles next to the line numbers in the editor.
• Click on a triangle next to a function, class, or other code block to fold (collapse) that block of code.
• Click on the triangle again to unfold (expand) the block of code.
3. Keyboard Shortcuts:
• You can also use keyboard shortcuts to fold and unfold code blocks:
• Cmd + Option + ← to fold the current block of code.
• Cmd + Option + → to unfold the current block of code.
• Cmd + Option + Shift + ← to fold all code blocks.
• Cmd + Option + Shift + → to unfold all code blocks.
4. Fold and Unfold Specific Sections:
• Place the cursor at the start or end of a code block.
• Use the keyboard shortcuts mentioned above to fold or unfold the specific section.
5. Code Folding Actions:
• Right-click in the editor and select Fold or Unfold to fold or unfold the current block.
• Select Fold All or Unfold All from the right-click context menu to fold or unfold all code blocks in the file.
By enabling and using code folding, you can keep your code more organized and focus on the parts you’re currently working on.
@@StewartLynch Thank you sir you just explain complete way to that i appreciate your kindness..
Nice tutorial thx
Hi Great video and I'll be watching the series. Just one little thing not sure if its a bug but when you change the status of a book to completed the date picker stays in the distant past and it never updates to the current date unless you manually change it in the picker itself. It does the same behaviour in the competed app from GitHub as well. the date stays at Dec 31, 1 like the year 2001 I think.
Is anybody else getting this or am I just thinking about this wrong?
What if you go from onShelf to inProgress then Completed? My code may not be perfect, but the SwiftData stuff is still good. See if you can improve the code and share.
@@StewartLynch Yep it does the same thing, just for some reason it refuses to update the dateCompleted property until you use the picker. Ive tried rewriting it as a switch rather then If then else statements (no luck) one thing that does make it behave a little bit more constantly is changing the Datepicker for dateCompleted to this:
DatePicker("", selection: $dateCompleted, in: dateAdded...Date.now, displayedComponents: .date) that at least should give a date that's closer to what somebody would want and not constantly setting itself to the distant past that's the best solution I have for now.
amazing content! =)
Glad you think so!
hi can we make call recording app in ios i want to make one for me..... there are many apps on appstore but they all are paid app i want to build for free.
I have no experience in that area
@@StewartLynch no problem thank you for the reply..
Hi Stweart, big thanks from beginner developer. Great free content.
Can anyone explaine to me, how and when firstView change state from true to false? Because in this example never get into this block of code. It's always true
.onChange(of: status) { oldValue, newValue in
if !firstView {
if newValue == .onShelf {
dateStarted = Date.distantPast
dateComplited = Date.distantPast
} else if newValue == .inProgress && oldValue == .completed {
// from completed to inProgress
dateComplited = Date.distantPast
} else if newValue == .inProgress && oldValue == .onShelf {
// book has been started
dateStarted = Date.now
} else if newValue == .completed && oldValue == .onShelf {
// forgot to start book
dateComplited = Date.now
dateStarted = dateAdded
} else {
// completed
dateComplited = Date.now
}
firstView = false
}
}
HI Stewart. On the Edit/Update I'm trying to pass an Int? into a Picker as below. If I put gidata.growthYears in a Textfield it shows up fine but when I try to add it to my Picker the Picker show up but nothing is selected as default nor can I select anything. Any tips or suggestions
let gidata: myModel
@State private var yrsSelectedArray: [Int] = [1, 5, 10]
Picker("Years for Simulation:", selection: .constant(gidata.growthYears)) {
ForEach(yrsSelectedArray, id: \.self) {
Text(String($0))
}
Looks like you are missing a .tag for your Text in the picker. So Text(Sttring($0)).tag($0)
Merci.
Thanks!
Thank you
Danke!
Bitte
Found no comparable tutorial about Swift Data with the possibility of editing the data after storage. I am excited.@@StewartLynch