Best strategy to detect a modified (unmanaged) object


Bit of background:
I have a pretty hefty model which represents a form, so most fields apart from creation date etc are editable by the user in a special UI where all the fields are visible (aka collection view). I used to have live updates to this - as in the user was modifying the realm object directly and every modification was picked up by Sync. It was a bit cumbersome with the writes on a background thread and then reads on main thread, plus the extra main thread realm refresh. Even more so for the free text fields where every keystroke was triggering sync
So I switched to a pattern where the user edits the form and has to press Save to save the form at the end

I use an unmanaged object to do this. Basically the form starts with an unmanaged copy of the original object and edits that. They press Save, I create a new object with the current value and write a realm.add(newObject, update: .modified). Since the primary key is unique and the same, the original object gets updated. So far so good

When the user exits the editor I want to present them with a “You have some unsaved changes” kind of dialog
What is the best way to detect that there were some changes made? What I’m doing at the moment is to convert both objects to dictionaries, cast them to NSDictionary which has a method to deep-compare 2 NSDictionaries (NSDictionary.isEqual(to: ))

Is there a better way to compare 2 realm objects, one managed, one unmanaged, other than the one I described or to override isEqual and compare each property? I’m definitely not in favour of overriding isEqual because it’s prone to forgetting integrating future additions to the object’s schema and therefore hidden/hard to spot bugs. Plus for any other purpose equality is determined by primary key, not a deep, every property is equal kind of equality so it would add overhead, even if small. I only need deep comparison in one place. The object has few list properties too

I use this code to convert the Realm Objects to a dictionary:

extension Object {
    func toDictionary() -> [String: Any] {
        let properties = { $ }
        var mutabledic = dictionaryWithValues(forKeys: properties)
        for prop in as [Property] {
            // find lists
            if let nestedObject = self[] as? Object {
                mutabledic[] = nestedObject.toDictionary()
            } else if let nestedListObject = self[] as? RLMSwiftCollectionBase {
                var objects = [[String: Any]]()
                for index in 0 ..< nestedListObject._rlmCollection.count {
                    let object = nestedListObject._rlmCollection[index] as! Object
                mutabledic[] = objects
        return mutabledic