Search code examples
arraysswifttimer

Swift: sending Data in uneven time intervals


I have a little problem and I am too inexperienced in Swift to solve it on my own. If anyone has done something similiar or has some advice for me, I'd really appreciate it. So here is my problem. I have a text file that the app is selecting. The file itself is made of a timestamp and a line of code. I seperate the two and save them in arrays. The timestamps are in uneven intervals but I need to send the codelines at the corresponding times.

    extension String {
        var integer: Int {
           return Int(self) ?? 0
        }
        // Converting the String Timestamp to a Float Value

        var secondsfromString: Float {
        var components : Array = self.components(seperatedBy: ":")
        let minutes = components[0].integer
        let seconds = components[1].integer
        let milliseconds = components[2].integer / 100
        return Float((minutes * 60) + seconds + milliseconds)
        }
     }

     if let path = Bundle.main.path(forResource: "", ofType: "cs2") {
          do {

             // Getting file content and removing empty lines
              let contents = try String(contentsofFile: path)
              let newlines = contents.split(whereSeperator: \.isNewline)

             // counting the Array
              let x = newlines.count as Int

             // Preallocating the Arrays and seperating timestamp and code
              var timestamp = [Sting]{repeating: "", count: x}
              var codelines = [Sting]{repeating: "", count: x}

              for counter in 0...x-1 {
                  timestamp[counter] = "" + newlines[counter].prefix(8)
                  codelines[counter] = "" + newlines[counter].suffix(26)
              }

             // Getting the time in seconds from timestamp
              var time = [Float](repeating: 0, count: x)
              for counter in 0...x-1 {
                   time[counter] = 0 + timestamp[counter].secondsfromString
              }

             // Getting the interval between each Timestamp
              var y = [Float](repeating: 0, count: x)
              y[0] = time[0]
              for i in 0...x-2 {
                  y[i+1] = time[i+1] - time[i]
              }

             // Here I want to send Data with the intervals I got from 
             // the Text file

             var timer = [Timer]()
             for t in 0...x-1 {
                timer[t] = Timer.scheduledTimer(
                               .withTimeInterval: TimeInterval(y[t]),
                               .repeats: false, block: { timer in
                    //code to send Data, not yet done
                 })
              }
                   

I've tried a few things with Timers already, for example when I used the timer without a loop I managed to get the first timing right, but all the others were the same interval as the first, which was not what the text file gave me. When I put the timer into a loop without the array timer, it did everything like "instantly", not even the first interval was correct. Now I wanted to try to make an array of timers in order to accomplish it, but I just can't get it to work. Is using a timer in this circumstance even correct, or is there a better way to do it?


Solution

  • Your time array appears to already have the values you want. You then compute intervals, but that's not useful here. Creating a scheduled Timer is going to execute relative to "now." Schedule these all using time minus "the first time;" you don't need intervals between them.