0

I need to load in memory a lot of different info, and I want to know which way is faster: simple tsv file, or realm database.

For that I'm trying to implement scheduled timer in AppDelegate. This timer should start as soon as possible, and it should stop when the data is loaded.

I created some small class for that, with internal counter:

class TimerSpeedCounter {
var timer: Timer!
var counter: Int = 0

init() {
    self.counterInit()
}

func counterInit() {
    self.counter = 0

    self.timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(TimerSpeedCounter.countPlusOne), userInfo: nil, repeats: true)

}

@objc func countPlusOne() {
    self.counter = self.counter + 1
}

I'm creating this TimerSpeedCounter in didFinishLaunchingWithOptions, and this is singleton. I put it at the beginning:

timerSpeed.counterInit()

Obviously I want to see the value of self.counter when my function that loads all the data stops.

The thing is, it somehow works, but self.counter is always equals zero, because this timer is fired much later than needed (after these functions load all the data).

So, I have two questions.

What am I doing wrong? Why this timer is fired much later than it should?

What is the right way to compare the speed of two different solutions?

lithium
  • 1,272
  • 1
  • 14
  • 28
  • Don't use a timer. Simply take a `Date` and the start of the operation and check the elapsed time from that date at the end. A timer needs to execute but you are tying up the processor with your load, so the timer can't fire until the processor is available. – Paulw11 Mar 24 '18 at 20:16
  • Yeah, I did exactly that after posting this question, so my problem is solved, but it seems strange to me (timer was created long before my function; for me, it should work — maybe I'll have to set up very small interval for that though). – lithium Mar 25 '18 at 08:16
  • The main thread can only be doing one thing at a time; if it is loading your data then it can't be firing your timer – Paulw11 Mar 25 '18 at 08:48

0 Answers0