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

Me or a SwiftUI bug? Probably me, but...

Forums > 100 Days of SwiftUI

Hello all,

I finished Day 60 yesterday, described here:

https://twitter.com/rgordon/status/1342960752429248513

What I'm more interested in is why, as demonstrated in the brief example app that I wrote, the List view shown in this video, updates only when button 2 is pressed:

https://twitter.com/rgordon/status/1342907771575033858

Shouldn't button 1, which changes state in a published property on an observable object, also trigger SwiftUI to update the UI?

Here is the super brief example code:

https://github.com/russellgordon/StateChangeUpdateTest/tree/main/StateChangeUpdateTest

For whatever reason, only button 2 (which adds and then removes an element from the array the List is tied to) causes SwiftUI to update the user interface.

This seems wrong to me, but on the other hand, perhaps I am missing something.

Any thoughts would be greatly appreciated.

Thanks and take care,

Russ

3      

Hi Russ

I think it because when you push Button 1, the List is not changed (only the item inside the List) therefore the View is not redrawed, however when you remove and then add it back the List has changed and then the View is redrawed.

I maybe wrong.

Nigel

3      

Hi Nigel,

Thanks for your reply. It's curious – I need to play with this a bit more.

I'm wondering if both buttons would cause the list to update if Item were a struct and not a class – a value type then, rather than a reference type.

This came up in an extension that I made for Day 60 on the Friends app – I was downloading random placeholder user images and found that the images didn't update in the list for the first few users.

However, now that I'm using Core Data after Day 61, the issue is moot. It seems that SwiftUI picks up changes to the managed objects from Core Data, even though I'm loading the avatar images after the managed objects were themselves created (essentially changing an instance of a class, just like the example app that I made).

Take care,

Russ

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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

Archived topic

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.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.