-1

This seems like a simple question, but now I've been going in circles for an embarrassingly long time so time to ask. What is the best method for detecting a change in ANY property of a reference object? 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?

Here is a specific use case:

  1. Class instance is passed into a view.
  2. Class instance MIGHT be changed in the view.
  3. On exiting the view I need to save the object IF it was changed.

Step 3 is where I’m stuck. I don’t know how to determine if the class instance has changed. I don’t want to run the Backendless save function unless I need to, but I haven’t found a way to determine if the object has changed without coding for every single property. If that’s just how it’s done in swift then it is what it is, but makes me think I’m missing something. Or am I wrong to approach the problem like this?

david dt
  • 31
  • 2
  • 1
    Where/how are the objects being updated, is it really necessary to know the state of _all_ objects and is it really necessary to know the exact state of objects that _might_ have been changed? Try not to over-engineer the solution. – Joakim Danielson Nov 21 '22 at 15:52
  • Do you need to know *which* property changed, or just that something in the object has changed. If the latter, changed since when? – Caleb Nov 21 '22 at 16:03
  • 1
    Please provide a real use case, since with the current overly simplified example, this seems like an [XY problem](https://xyproblem.info). – Dávid Pásztor Nov 21 '22 at 16:44
  • The objects are being updated in swiftui by elements that are bound to published properties of the class. I don’t need to know which property changed…just that ANY property changed. If it helps to narrow the scope I only need to know if it changed within the current view (the copy/compare approach was the thought for that). If there’s isn’t a simple way to know then I’ll just save the object at every exit of the view but that seems wrong. I can’t imagine there isn’t a way to subscribe to “all” set methods of a class object without writing code for every property. – david dt Nov 21 '22 at 21:12

1 Answers1

1

Try this

 var variableName: variableType {
    didSet {
     // It is called after value change
    }
    willSet {
     // It is called before value change
    }
 }
  • This only works for a single property. If my class has a lot of properties there will be a ton of boilerplate coding just to know if a property of the class has changed and needs “saving”. – david dt Nov 21 '22 at 21:15