UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.