Creating, editing, and deleting model objects - SwiftData Starter Project 4/8
HTML-код
- Опубликовано: 13 окт 2024
- Add swipe to delete and @Bindable to enable adding, editing, and deleting SwiftData model objects. You can find the completed source code here: github.com/two...
This is an 8-part project that walks you through building a SwiftData project from scratch using SwiftUI. It's designed to work alongside my book SwiftData by Example, available at www.hackingwit...
This project is aimed at programmers who have some experience with Swift and SwiftUI already, and want to get up to speed with SwiftData as quickly as possible.
On everyday job I'll use it in 10 years, but... in my own personal apps, I'll use it as soon as possible. Amazing video as usually! ... even because you are paying homage to my country :)
Thank you very much!
I hope they upgrade before that! How old are the oldest phones that people use as their primary phone? Hopefully not 10 years old. That would be the iphone 5s. My wife stopped using that in 2019.
@@markaurelius61 I totally agree with you... I work as consultant in one of the biggest banking apps here in Italy. 85% of the users have iOS 16+, and less than 0.1% uses iOS12. During the beta phase of iOS17 we had more users with 17 than 12. We asked many many times to drop 12 but they always refused without giving a real reason.
@@asanokatana Yes, I expect CoreData to keep going for a good number of years
I did your 100 days with Swift, it is so good to learn with you like in the old days and with the most recent content 🚀
I don't understand the implementation of addDestination with regards to the path. It works as expected, but why does it work? 🤪
The Add new desstination is not correct in my opinion. What if the user clicks on add new, and then clicks back button? we will have empty Destination item in SwiftData and in our list.
This is just a learning demo. That can easily be fixed by moving the modelContext.insert(destination) functionality into the EditDestinationView.
Thank you so much Paul, you're a legend! This makes managing data so much easier! 😀
I dont understand this path thing. what and how does it do this "magic"?
and with navigation, why dont we use:
NavigationLink(destination: EditDestinationView(destination: destination)) {
VStack(alignment: .leading) {
Text(destination.name)
.font(.headline)
Text(destination.date.formatted(date: .long, time: .shortened))
}
If i try to add a new destination, i get an yellow triangle displayed (error maybe?) on the editDestination view
Why do we use a For-in loop in the deleteDestinations function, when we can delete only one item sometimes?
In don't understand addDestination() magic? what is happening here? how Destination() getting values? let destination = Destination(). we are not passing any values here...what is happening?
Question: if you have an array of 10 items, and you want delete items 1, 4, 7...... when you delete item 1 from the list, won't that change the indices for the remaining items and so 4 and 7 will no longer be the correct indices? If you delete them in reverse order, ok, but.....
How do we prevent blanks from being inserted? For example, if I add a new Destination then hit back right away without inputting any text. I don’t want a blank showing up there.
same question
Same question, please respond
Sorry for this being late. This is how I fixed it.
.onAppear
{
for destination in destinations {
if destination.name == ""
{
modelContext.delete(destination)
}
}
}
I attach this to the ListView. Sorry, for the formatting; I am on my iPad.
@@MirrorsEdgeGamer01 no prblm thanks for you respons
@@MirrorsEdgeGamer01 Where exactly do you put this code? where is the ListView?
Hello Paul, great tutorial! For some reason my Xcode preview is crashing. Did you have that issue?
Mine is crashing as well. Did you find a solution?
@@morgancooledge3557 hi, you should add #Preview in this video to the ContentView preview. You may remove example line and instead of return Edit…. you should add ContentView.
Try:
#Preview {
ContentView()
.modelContainer(for:Destination.self)
}
I have an issue, I'm not sure if it's a bug with Swift Data, where in the simulator my list of names is always the name of the first model added to the context. Each time I delete one, it goes to the second oldest one (like going through a queue). This bug(?) doesn't happen in the preview.
Eg. If my modelContext has dest1, dest2, dest3 added in that order, it will look like
dest1
dest1
dest1
Then if I delete any of them it'll look like
dest2
dest2
Then if I delete either of those it'll be
dest3
I get an error on the .modelContainer(container) line:" Cannot convert value of type 'some View' to closure result type 'UIView'" with the code:
do {
let config = ModelConfiguration(isStoredInMemoryOnly: true)
let container = try ModelContainer(for: Destination.self, configurations: config)
let example = Destination(name: "Example Destination", details: "Example details go here and will automatically expand vertically as they are edited.")
return EditDestinationView(destination: example)
.modelContainer(container)
} catch {
fatalError(error)
}
Question about your delete action and it's placement. You use .onDelete on the ForEach loop and hence need the Index. I used a .swipeAction with a button to delete the destination on the VStack. Both delete the correct Destination. Is there any advantage/disadvantage of one over the other?
I prefer onDelete() because it enables list editing using EditButton, but as we aren't using it here there's no real difference.
I have a basic answer (sorry if its too basic). I've trying to use Swift Charts to build data visualisations. I have always used structs to model data for the purpose of charts. Here we are defining data models using Classes in SwiftData. Will there be any difference in how I use swift Charts with this? Thanks in advance!
Classes can be operated remotely (such as:
var example = example(value: 0)
example.value += 1 (the value of Example here is increased by 1)
example().value += 1 (the value of Example here is 1 more, and it operates on the same object
print(example.value)
⬆will print out 2
)
Each time struct is used, a new object is added.
For example:
var structExample = StructExample(value: 0)
var structExample2 = StructExample(value: 0)
⬆Add new object of structExample(). The changes here are changes to the variable and will not affect other objects.
structExample.value += 1 (changing this variable will not affect other objects using StructExample)
strcutExample2.value += 1
print(structExample.value)
⬆will print out 1
print(structExample2.value)
⬆will print out 1
Different methods (classes or struct) will be used to create models for different usage environments, such as:
When creating chartData, because adjustments need to be made to individual data, it is better to use struct to build the model.
The class can be used to build a viewModel because of its remote controllability.
這是我用google翻譯翻譯來的,如果用詞上有不精準的部分請見諒:D
Question 2: even though you can pass an IndexSet, I've never seen an example where more than one item is deleted at the same time. i've never seen a screen where you select multiple times and then click delete once and have it delete all items. usually it's just swipe left to delete, but one at a time.
can you make a video to show a multiple delete situation? or point me to one?
@@DavidNitzscheBell It could be useful for a photo grid view.
@Bindable doesn't work for me when placing the Model inside of an array
Hard to distinguish between the destination of a navigation link and the destination of a trip.
I really don't understand why it is
return EditDestinationView(destination: .example)
.modelContainer(container)
instead of
return EditDestinationView(destination: .example)
.modelContainer(container)
Those are identical
Awesome🎉