Want to keep instance update time

Asked 2 years ago, Updated 2 years ago, 34 views

I'd like to remember the time when I updated an instance of this class in a different class. Is there any good way?

class Testdata:NSObject{
   vardata1 = Data1()
   vardata2 = Data2()

   class Data1 {
     dynamic varversion —UInt8=0x10
     Dynamic ver...
   }
   class Data2 {
     dynamic var no —UInt8 = 0x100
     Dynamic ver...
   }
}


   vardate —UInt64 = 0
   vardata1 = Data1(){
       DidSet{
           self.date=UInt64 (NSDate().timeIntervalSince 1970)
       }
   }
   vardata2=Data2(){
       DidSet{
           self.date=UInt64 (NSDate().timeIntervalSince 1970)
       }
   }

I tried to see if I could get it above, but
Rewriting data1.version did not call DidSet.
Is there any good way?

swift

2022-09-30 10:28

1 Answers

I don't think there's any particular "good way."
(By the way, data1 (or data2) in your code itself will not be called each didSet unless it is rewritten (that is, another instance is substituted).

(1) Set KVO (key value monitoring) to all properties of all instances where you want to record the update date and time.

class Testdata:NSObject{
    vardata1 = Data1()
    vardata2 = Data2()

    var updateDate —NSDate=NSDate()

    classData1:NSObject{
        dynamic varversion —UInt8=0x10
        // dynamic ver...
        //...
    }
    classData2:NSObject{
        dynamic var no —UInt8 = 0x10
        // dynamic ver...
        //...
    }

    override init() {
        super.init()
        data1.addObserver(self, forKeyPath:"version", options:[], context:nil)
        // data1.addObserver(self, forKeyPath:"...", options:[], context:nil)
        //...
        data2.addObserver(self, forKeyPath:"no", options:[], context:nil)
        // data2.addObserver(self, forKeyPath:"...", options:[], context:nil)
        //...
    }

    US>deinit{
        data1.removeObserver(self, forKeyPath: "version")
        // data1.removeObserver(self, forKeyPath: "...")
        //...
        data2.removeObserver(self, forKeyPath: "no")
        // data2.removeObserver(self, forKeyPath: "...")
        //...
    }

    override funcoveValueForKeyPath(keyPath:String?, ofObject object:AnyObject?,change:[String:AnyObject]?,context:UnsafeMutablePointer<Void>){
        if object===data1||object===data2{
            self.updateDate=NSDate()
        }
    }
}

(2) Record the update time in DidSet in all instances of all classes where you want to record the update date and time.

protocol MyDataObserver:class{
    funcsetUpdateDate()
}
classTestdata2:MyDataObserver{
    vardata1 = Data1()
    vardata2 = Data2()

    var updateDate —NSDate=NSDate()

    classData1:NSObject{
        weak varobserver: MyDataObserver?
        varversion:UInt8=0x10{
            DidSet{
                observer?.setUpdateDate()
            }
        }
        //ver...{
        //  DidSet{
        //      observer?.setUpdateDate()
        //  }
        //}
        //...
    }
    classData2:NSObject{
        weak varobserver: MyDataObserver?
        var no : UInt8 = 0x10 {
            DidSet{
                observer?.setUpdateDate()
            }
        }
        //ver...{
        //  DidSet{
        //      observer?.setUpdateDate()
        //  }
        //}
        //...
    }

    init(){
        data1.observer=self
        data2.observer=self
    }

    // MyDataObserver
    funcsetUpdateDate(){
        self.updateDate=NSDate()
    }
}

I wonder if you can think of it right away.Neither seems like a "good way".

(3) It seems interesting to set the setter of all properties that you want to record updates to private, and to change the value only with the setValue(_:forKeyPath:) method.

class Testdata3:NSObject{
    dynamic var data1 = Data1()
    dynamic var data2 = Data2()

    var updateDate —NSDate=NSDate()

    classData1:NSObject{
        dynamic private(set) varversion —UInt8 = 0x10
        // dynamic private (set) ver...
        //...
    }
    classData2:NSObject{
        dynamic private(set) var no : UInt8 = 0x10
        // dynamic ver...
        //...
    }

    override func setValue(value:AnyObject?, forKeyPath keyPath:String) {
        if keyPath.hasPrefix("data1.")||keyPath.hasPrefix("data2."){
            self.updateDate=NSDate()
        }
        super.setValue(value, forKeyPath:keyPath)
    }
}

let myTestdata3 = Testdata3()
myTestdata3.setValue (0x20, forKeyPath: "data1.version")
print(myTestdata3.data1.version)//->32
The time you called print(myTestdata3.updateDate)//->setValue(_:forKeyPath:)

If I get the same requirements, what would I do to create a class that holds all the data that needs to be updated, so that only a limited method can update those data, and record the time that the method was called?

Some of you may know, "There's such a good way," but here's what comes to mind.

[Correction] Removed unnecessary dynamic from code example


2022-09-30 10:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.