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

How to detect ANY change of a class property

Forums > Swift

What is the best method for detecting a change in ANY property of a reference object so I can mark it as dirty for processing at a later time? I am using Backendless as the backend so I'm committed to using classes at this point so going with structs aren't an option.

Here are two approaches I've been trying...

  1. Use some combination of .objectWillChange and/or .sink, but I can only get those to work on individual properties of an object. If I attach to the object it only notifies when I assign a new obj to the var.

  2. Creating a copy of the object as a reference to check against for changes requires that I create some version of a deep copy which seems like the wrong way to go and very hacky.

The point for this is that I don't want to "save" the object every time a single property changes. I'd rather wait and save the whole object on dismiss or something similar, but I can't find the best way to mark an object as "changed" without writing code for every single property...which seems weird when there are already Publishers, but I can't find a method that watches the object for ANY Published events to emit. Or maybe this just isn't the right approach at all?

2      

hi david,

would it be enough for you to know if any @Published property of an ObservableObject has changed (without having to watch each one individually)?

if so, consider having the object subscribe to its own objectWillChange publisher. this seems to work:

import Combine

class SelfListener: ObservableObject {

    @Published var property1: Int = 0
    @Published var property2: Int = 0
    var hasChanged: Bool = false
    var cancellables = Set<AnyCancellable>()
    init() {
        objectWillChange.sink { _ in 
        self.hasChanged = true 
        print("received objectWillChange")
      }
            .store(in: &cancellables)
    }
}

you could test it this way (i do this in a basic command-line MacOS; i did not test in a Playground).

let selfListener = SelfListener()
print(selfListener.hasChanged) // prints false
selfListener.property1 = 3 // prints received objectWillChange
print(selfListener.hasChanged) // prints true
selfListener.property2 = 8  // prints received objectWillChange
print(selfListener.hasChanged)  // prints true

as to my phrase this seems to work above, it could be a little concerning that my selfListener object has a publisher to which the selfListener object itself is subscribed. this could bring on nightmares of possible memory cycles; and if so, a deinit method on the object might address the problem:

    deinit {
        cancellables.forEach({ $0.cancel() })
    }

finally, if you have a main-level object with properties that are themselves classes that conform to ObservableObject and you want to know whether they have been edited, then i suppose you could subscribe at the main level to each of their objectWillChange publishers as well. i did not test this

this was sort of fun to experiment with (if it even makes sense to do it). but in fact, it might just be enough to write your own property wrapper for use with properties you want to watch (in place of using @Published) that handles any set of the property by setting a named boolean such as hasChanged to true (and, if necessary, executing objectWillChange.send()).

hope that helps,

DMG

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!

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.