TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Intermittent SwiftUI Redraw Issue when inserting/removing elements from array. (Reproducible in project17)

Forums > SwiftUI

Hi everyone,

I'm currently working on a project that involves a card-swiping feature in SwiftUI. I'm facing an intermittent issue where, occasionally, removing an element from the array does not trigger the expected SwiftUI redraw. Consequently, conditions like if array.isEmpty are not being activated as they should.

To delve deeper, I tested with Project 17 (downloaded from Flashzilla), which employs similar logic for handling arrays and card swiping. Interestingly, I was able to reproduce the same issue in this project. After clearing the stack of cards repeatedly, there comes a point where the screen remains empty, and the cards.isEmpty condition does not get triggered, even though the array is indeed empty.

This issue doesn't happen consistently but is noticeable with persistent interaction. It seems like SwiftUI is not detecting changes in the array's count or its isEmpty state under certain circumstances (which i cannot identify).

Has anyone else encountered similar issues with SwiftUI not detecting changes in an array's count or the isEmpty state? Any insights or solutions would be greatly appreciated.

Thank you in advance for your help!

2      

I'm experiencing a similar but slightly different issue (specifically regarding Flashzilla project). My array is not empty but the display of cards is.

I've narrowed down root cause to the ForEach loop calling CardView. After 3-6 iterations (sometimes more), the number of cards displayed is actually cards.count-1 which prevents the array from being completely emptied and therefore also prevents the reset button from displaying.

I've tried to troubleshoot the issue by putting in .onAppear modifiers around CardView, ForEach, as well as .onChange(of:) on the ZStack, but so far no luck. I also added another ForEach that doesn't call CardView to see if the array is fully populated and it is (so it's not a case of UserDefaults being inaccurate)

I'll keep digging...

2      

Hacking with Swift is sponsored by RevenueCat.

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.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.