ggruen / CloudKitSyncMonitor

Monitor current state of NSPersistentCloudKitContainer sync
MIT License
488 stars 44 forks source link

"syncStateSummary" not `@Published` #18

Open benguild opened 1 year ago

benguild commented 1 year ago

I was looking at this library and noticed that this property, although useful, is not @Published.... So, in order to catch changes on it, one would need to monitor changes to a half dozen or so other properties.

It seems potentially wasteful (although not terribly) to calculate this in advance for publishing if not used, but it's also a hassle to monitor so many other properties.

Just wanted to ask if there was potentially a compromise here, or if you had best practices in mind regarding this. 😅

ggruen commented 1 year ago

Hi @benguild,

The SyncMonitor class itself is an ObservableObject, so any property you include in a SwiftUI View (assuming you're using SwiftUI) will be updated when there's a change. You'll notice that the @Published properties in SyncMonitor are all private.

So, if you do:

@ObservedObject var syncMonitor: SyncMonitor = SyncMonitor.shared

then in your View include

syncmonitor.syncStateSummary

Then syncStateSummary will be updated when the sync state changes because syncMonitor itself will show that a change was published.

benguild commented 1 year ago

Sorry for the delay! So, I guess the downside there is it'd recalculate the view whenever anything changes, even outside of the visual linked to "syncStateSummary" ...?

In this case it might be OK, but in others it might be less ideal depending on what's going on.

ggruen commented 1 month ago

Ok, now I apologize for the delay... My understanding is that, because SyncMonitor is an ObservedObject, that any public properties within it will behave, for practical purposes, like they're Published - that is, your syncMonitor variable will be what's updated (and thus trigger any UI updates), not the syncStateSummary property itself.

Are you using SyncMonitor outside of SwiftUI, and/or seeing an issue with syncStateSummary not updating (assuming you're still using this since I'm responding over a year later 😮 )?

benguild commented 1 month ago

Right, so I guess the downside is that it requires observing the object rather than just that one property. It'd be nice to have that as a choice, but since the property is computed it would need to be stored, calculated initially, and then updated only when it changes.