andywalt / game_goals

A goal tracker for video games.
3 stars 0 forks source link

onDelete is deleting Goals in other Games and not Deleting the goal it's supposed to #1

Closed andywalt closed 4 years ago

andywalt commented 4 years ago

I adjusted the sorting so that the Goals are sorted based on if they are complete or not (in the CoreData Class). I didn't realize before but when a goal is deleted, it sometimes deletes other goals in other games and also doesn't delete itself.

Think it has to do with calling the index of the specific goal that is being deleted or maybe even calling the sorted array in the deleteGoal function.

@sparklebeard if you're willing, would love your insight. Not trying to be a bother but feel like I'm so close to getting this but always just one or two steps off. I also added some comments because I saw you doing so and figured I could be more thorough.

andywalt commented 4 years ago

I also think that this could help but not sure how it fleshes out. https://stackoverflow.com/questions/58919379/swiftui-ondelete-doesnt-work-on-sorteddata

sparklebeard commented 4 years ago

Say my name three times and I will physically manifest in your repo. Pushed a branch with what I think is a solution and some comments. Turns out I had this same bug in my own code base too!

Do those comments help ?

andywalt commented 4 years ago

@sparklebeard so it looks like I was close but kept trying to figure out how to access the item in the array by it's index. I was so close but didn't realize that index.first! would call the only item in that list cell!

I can't thank you enough. I'm learning more everyday and honestly, I learn more from your comments than anything else.

What resources are you using to learn all of this?

sparklebeard commented 4 years ago

I've been able to spend a lot of time each day just trying thing and searching for solutions! I can be pretty obsessive over things if they interest me, so I've just been consuming so much tutorial material and creating different types of practise projects.

RayWenderlich tutorials tend to be pretty good. I've noticed that hackingwithswift has a lot o information but its typically pretty simplistic and can be a bit easy to trip up on when trying to modify the example to use in a more complex flow.

I bought the RayWenderlich "Combine: Asynchronous Programming with Swift" book, and while its a bit complex ive found that trying to pay attention to Combine has helped me understand data flow in SwiftUI (for obvious reasons).

I have also learned a lot by looking at questions on reddit or slack overflow and tried to figure out how I would solve it, and look at the answers others give for the problem. I find sometimes it's easier to debug someone else's code than your own since you have assumptions about your own code that you don't about others!

sparklebeard commented 4 years ago

And to clarify index.first! (though you probably already know this, apologies if I'm telling you what you already know!)

The reason it works is that the item passed to the closure after .onDelete is an array of indices to delete, but we know that the array will always contain one item since the ForEach only allows us to delete one item at a time (... I think?), hence it being safe to force unwrap .first! I probably should have used the "indexSet" variable rather than "index" for clarity.

In theory though the ForEach could pass multiple indices for deletion but I dont think it supports that out of the box.

andywalt commented 4 years ago

This is my first app and I probably bit off more than I can chew but it's been good to learn so much. Really can't thank you enough. I'm just finding that there aren't many resources out yet for more complex issues.

andywalt commented 4 years ago

also don't worry about assuming. I'm Jon Snow and I really know nothing. You've been a tremendous help