Hi guys
I have a question about Timer objects. In the code beneath I've created a Timer object which is deleted upon the disappearance of the view. As I call for invalidate on this object the Timer object should be removed. To do so I made a timer property in my ViewController so I can refer to the Timer object to which the main run loop refers. Checking it with Instruments I came to the same conclusion though that the property isn't pointing to the same memory address as the main run loop? Can anyone explain me why this is the case? I would have expected it to be the same memory address?
Instruments delivers following output on Allocations/Statistics/Allocation summary/CFRunLoopTimer:
CFRunLoopTimer
0x60000017c080 CFRunLoopTimer Foundation -[NSCFTimer initWithFireDate:interval:target:selector:userinfo:repeats:]
0x60000017c740 CFRunLoopTimer QuartzCore CA::update_timer()
Under Allocations/Statistics/Allocation summary I also found
CFRunLoopTimer (contents described above)
awd:
rofile:TimerSpec
NSCFTimer
Clicking on awd:
rofile:TimerSpec I found the contents
0x60000026d00 awd:
rofile:TimerSpec WirelessDiagnostics awd:
rofile:
rotobuf_AddDesc_AwdProfile_2eproto()
Clicking on NSCFTimer I found the contents
0x60000012530 NSCFTimer Foundation _27+[NSFCTimer allocWithZone:]_block_invoke
I have a question about Timer objects. In the code beneath I've created a Timer object which is deleted upon the disappearance of the view. As I call for invalidate on this object the Timer object should be removed. To do so I made a timer property in my ViewController so I can refer to the Timer object to which the main run loop refers. Checking it with Instruments I came to the same conclusion though that the property isn't pointing to the same memory address as the main run loop? Can anyone explain me why this is the case? I would have expected it to be the same memory address?
Instruments delivers following output on Allocations/Statistics/Allocation summary/CFRunLoopTimer:
CFRunLoopTimer
0x60000017c080 CFRunLoopTimer Foundation -[NSCFTimer initWithFireDate:interval:target:selector:userinfo:repeats:]
0x60000017c740 CFRunLoopTimer QuartzCore CA::update_timer()
Under Allocations/Statistics/Allocation summary I also found
CFRunLoopTimer (contents described above)
awd:
NSCFTimer
Clicking on awd:
0x60000026d00 awd:
Clicking on NSCFTimer I found the contents
0x60000012530 NSCFTimer Foundation _27+[NSFCTimer allocWithZone:]_block_invoke
Code:
class ViewController: UIViewController {
@IBOutlet weak var timeLabel: UILabel!
let clock = Clock()
var timer: Timer?
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
updateTimeLabel()
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(ViewController.updateTimeLabel), userInfo: nil, repeats: true)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
timer?.invalidate()
}
func updateTimeLabel() {
let formatter = DateFormatter()
formatter.timeStyle = .medium
timeLabel.text = formatter.string(from: clock.currentTime as Date)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}