|
I'm having a problem with the second challenge:
I created a settings screen and with a Toggle that changes the setting. This was no problem. But whenever I try to add a card back into the cards array, I get strange behavior, such as the wrong card displaying, and then the cards becoming untappable. For testing purposes, I'm just trying to add every card that gets removed in my removeCard function:
I've been able to successfully add new cards to the [cards] array by using the .insert function with a button anywhere inside ContentView like so:
However, every attempt I make to add a card back into the array as a part of removeCard causes issues. What am I missing here? |
|
I found a solution which seems to indicate that there is an issue with removing/adding things to the cards array back-to-back. The solution (credit to Petro Onishchuk - YouTube) was to use DispatchQueue in order to delay the addition of the new card until after the the removal has had time to complete. 0.5 seconds seems to work.
However, this still has some major issues.
Any recommendations for how to truly solve this? |
|
I was bored so I decided to make a simple test with another array that removes an element and and re-inserts it immediately. This works fine, so there must be something specific to the the CardView that is causing problems.
|
|
@ZoydWheeler Thank you very much for sharing this. Faced a similar problem a spent hours until found this article. |
|
Try passing the cards array directly into ForEach rather than passing a range.
That does require making other changes, of course, like making the Card struct implement Hashable. I'm perfectly willing to believe there's a superior approach, but it's working well enough for me to move on. |
|
Hi @stuartisaac , I had the same problem, so I refactored my code to use
but the problem remains: looks like SwiftUI is not aware of the change of the
when removal and insert are done near each other... but I can't figure out why... The only approach that works for me is the one from @ZoydWheeler, delaying adding back the card to the stack with
However... testing the two acessibility buttons, I noticed that they work correctly: when pressing the "xmark.circle" button for wrong answer the card is added back to the stack AND shown correctly without the need for delay. Also: when no more cards are shown on screen the timer is continuing and if I tap the "checkmark.circle" button enough times to remove the recycled cards, the games stops correctly! Looks like the reused cards are there, but somehow not shown... could be something wrong with the layout? (e.g. why am I seeing the accessibility button after clicking something even if no accessibility features enabed?) |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
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.