Timer does not invalidate - timer

I can not invalidate a Swift Timer.
When defining my objects and variables I declare the timer with
var scoreTimer = Timer()
I start the timer when two objects collide with this line:
scoreTimer = Timer.scheduledTimer(timeInterval: 0.05, target: self, selector: #selector(updatePassengerScore), userInfo: nil, repeats: true)
My updatePassengerScore method looks like this
passengerScore -= 1
scoreLabel.text = String(passengerScore)
When another collision is detected, I try to stop the timer with
scoreTimer.invalidate()
but the variable passengerScore is still decreasing and I do not have a clue why this is the case.
Does anyone have a clue hoe to solve this and make the Timer stop? Every Thread I read says that scoreTimer.invalidate() should actually stop the timer. I am really confused :-/

You might be starting multiple timers. If you hit the line of code that creates a timer before the current one has been invalidated, then you will have two active timers, only one of which you can invalidate.
To prevent this possibility, call invalidate on scoreTimer before creating a new timer:
scoreTimer.invalidate()
scoreTimer = Timer.scheduledTimer(timeInterval: 0.05, target: self, selector: #selector(updatePassengerScore), userInfo: nil, repeats: true)
The other way to handle it is to change scoreTimer to an optional, and then only create a timer if scoreTimer is nil.
var scoreTimer: Timer?
...
if scoreTimer == nil {
scoreTimer = Timer.scheduledTimer(...
}
...
scoreTimer?.invalidate()
scoreTimer = nil

Related

Executing morse code with flashlight from array

I have an array of characters "." and "-" which is a translated text to morse code. They are of course in a extacly specified order because of the morse code. Now i want to convert this array to long and short flashes from flashlight in iPhone, after button is tapped. I tried of course looping through this array with 'if' statement, with scheduledTimers just like below, but obviously that doesnt worked out, loop was executing immediately and flashlight was launching only once.
let dash: Character = "-"
let dot: Character = "."
for i in translatedArray {
if i == dash{
//two sec delay at the beggining because of the pause beetween each flash which is equal to one dot
timer1 = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(toggleFlashlightOn), userInfo: nil, repeats: false)
timer2 = Timer.scheduledTimer(timeInterval: 6, target: self, selector: #selector(toggleFlashlightOff), userInfo: nil, repeats: false)
print("long flash activated")
}else if i == dot{
timer1 = Timer.scheduledTimer(timeInterval: 2, target: self, selector: #selector(toggleFlashlightOn), userInfo: nil, repeats: false)
timer2 = Timer.scheduledTimer(timeInterval: 4, target: self, selector: #selector(toggleFlashlightOff), userInfo: nil, repeats: false)
print("short flash activated")
} else { return }
}
I also tried to delay separate 'if' iteration with delay() function, but execution of this loop with morse code should have irregular time interval beetween each iteration depending on previous character aka previous lenght of flash, and delay() have only static value of seconds, so this didn't worked either.
Finally i found this thread here. And except updating it to swift4, and changing array of morse code characters to array of time intervals, which i did succesfully this would be a perfect solution for my problem... but it doesn't work. It turns on flashlight in only one mode which is ON, and refresh endlesly printing "ON" regularly with first time interval value in the array, for example every 2 seconds or so. So it never turns off the flashlight.
let shortFlash: Double = 2
let longFlash: Double = 4
let pause: Double = 2
var sequenceOfFlashes: [Double] = []
//this works ok, prints correct values in correct order from sequenceOfFlashes array
func setupMorseFlashesSequence(){
let dot: Character = "•"
let line: Character = "—"
for i in translatedArray {
switch i {
case dot:
sequenceOfFlashes.append(shortFlash)
sequenceOfFlashes.append(pause)
case line:
sequenceOfFlashes.append(longFlash)
sequenceOfFlashes.append(pause)
default:
return
}
}
print(sequenceOfFlashes)
}
var index: Int = 0
weak var timer: Timer?
func scheduleTimer(){
timer = Timer.scheduledTimer(timeInterval: sequenceOfFlashes[index], target: self, selector: #selector(timerTick), userInfo: nil, repeats: false)
}
#objc func timerTick(){
if index == sequenceOfFlashes.count {
stop()
}
turnFlashlight(on: index % 2 == 0)
scheduleTimer()
}
func start(){
index = 0
turnFlashlight(on: true)
scheduleTimer()
}
func stop(){
timer?.invalidate()
turnFlashlight(on: false)
}
deinit{
timer?.invalidate()
}
how can i achieve executing this flashes one by one with different times?
You forgot to increment index in timerTick():
#objc func timerTick(){
index += 1
if index == sequenceOfFlashes.count {
stop()
}
turnFlashlight(on: index % 2 == 0)
scheduleTimer()
}

Preventing completion handler to be executed synchronously

I'm writing some networking code in Swift that prevents initiating a download that is already in progress. I do this by keeping tracking of the identity of the network request along with the associated completion handlers in an (synchronized) array A. When a network call finishes it calls the completion handlers that are associated with that resource and it subsequently removes those handlers from the array A.
I want to make sure there is no way for threads to access the array in certain cases. For example, consider the following scenario:
A request to download resource X is started.
Verify whether the request has already been made.
Add the completion handler to the array A.
If request has not been made, start the download.
What if resource X was already downloading, and the completion handler for this download interrupts the thread between steps 2 and 3? It has been verified that the request has been made so the download will not be started, but the new completion handler will be added to array A which will now never be called.
How would I block this from happening? Can I lock the array for writing while I do steps 2 and 3?
The simple solution is to run everything on the main thread except the actual downloading. All you need to do is make the completion handler a stub that places a block on the main queue to do all the work.
The pseudo code for what you want is something like
assert(Thread.current == Thread.main)
handlerArray.append(myHandler)
if !requestAlreadyRunning)
{
requestAlreadyRunning = true
startDownloadRequest(completionHandelr: {
whatever in
Dispatch.main.async // This is the only line of code that does not run on the main thread
{
for handler in handlerArray
{
handler()
}
handlerArray = []
requestAlreadyRunning = false
}
})
}
This works because all the work that might result in race conditions and synchronisation conflicts runs on one thread - the main thread and so the completion handler can't possibly be running when you are adding new completion handlers to the queue and vice versa.
Note that, for the above solution to work, your application needs to be in a run loop. This will be true for any Cocoa based application on Mac OS or iOS but not necessarily true for a command line tool. If that is the case or if you don't want any of the work to happen on the main thread, set up a serial queue and run the connection initiation and the completion handler on it instead of the main queue.
I'm working on the assumption that you want to be able to add multiple callbacks that will all be run when the latest request completes, whether it was already in-flight or not.
Here's a sketch of a solution. The basic point is to take a lock before touching the array(s) of handlers, whether to add one or to invoke them after the request has completed. You must also synchronize the determination of whether to start a new request, with the exact same lock.
If the lock is already held in the public method where the handlers are added, and the request's own completion runs, then the latter must wait for the former, and you will have deterministic behavior (the new handler will be invoked).
class WhateverRequester
{
typealias SuccessHandler = (Whatever) -> Void
typealias FailureHandler = (Error) -> Void
private var successHandlers: [SuccessHandler] = []
private var failureHandlers: [FailureHandler] = []
private let mutex = // Your favorite locking mechanism here.
/** Flag indicating whether there's something in flight */
private var isIdle: Bool = true
func requestWhatever(succeed: #escaping SuccessHandler,
fail: #escaping FailureHandler)
{
self.mutex.lock()
defer { self.mutex.unlock() }
self.successHandlers.append(succeed)
self.failureHandlers.append(fail)
// Nothing to do, unlock and wait for request to finish
guard self.isIdle else { return }
self.isIdle = false
self.enqueueRequest()
}
private func enqueueRequest()
{
// Make a request however you do, with callbacks to the methods below
}
private func requestDidSucceed(whatever: Whatever)
{
// Synchronize again before touching the list of handlers and the flag
self.mutex.lock()
defer { self.mutex.unlock() }
for handler in self.successHandlers {
handler(whatever)
}
self.successHandlers = []
self.failureHandlers = []
self.isIdle = true
}
private func requestDidFail(error: Error)
{
// As the "did succeed" method, but call failure handlers
// Again, lock before touching the arrays and idle flag.
}
}
This is so broadly applicable that you can actually extract the callback storage, locking, and invocation into its own generic component, which a "Requester" type can create, own, and use.
Based on Josh's answer I created a generic Request & Requester below. It had a few more specific needs than I described in the question above. I want the Request instance to manage only requests with a certain ID (which I made into a String for now, but I guess this could also made more generic). Different ID's require a different Request instance. I created the Requester class for this purpose.
The requester class manages an array of Requests. For example, one could choose T = UIImage, and ID = image URL. This would give us an image downloader. Or one could choose T = User, and ID = user id. This would get a user object only once, even when requested several times.
I also wanted to be able to cancel requests from individual callers. It tags the completion handler with a unique ID that is passed back to the caller. It can use this to cancel the request. If all callers cancel, the request is removed from the Requester.
(The code below has not been tested so I cannot guarantee it to be bug free. Use at your own risk.)
import Foundation
typealias RequestWork<T> = (Request<T>) -> ()
typealias RequestCompletionHandler<T> = (Result<T>) -> ()
typealias RequestCompletedCallback<T> = (Request<T>) -> ()
struct UniqueID {
private static var ID: Int = 0
static func getID() -> Int {
ID = ID + 1
return ID
}
}
enum RequestError: Error {
case canceled
}
enum Result<T> {
case success(T)
case failure(Error)
}
protocol CancelableOperation: class {
func cancel()
}
final class Request<T> {
private lazy var completionHandlers = [(invokerID: Int,
completion: RequestCompletionHandler<T>)]()
private let mutex = NSLock()
// To inform requester the request has finished
private let completedCallback: RequestCompletedCallback<T>!
private var isIdle = true
// After work is executed, operation should be set so the request can be
// canceled if possible
var operation: CancelableOperation?
let ID: String!
init(ID: String,
completedCallback: #escaping RequestCompletedCallback<T>) {
self.ID = ID
self.completedCallback = completedCallback
}
// Cancel the request for a single invoker and it invokes the competion
// handler with a cancel error. If the only remaining invoker cancels, the
// request will attempt to cancel
// the associated operation.
func cancel(invokerID: Int) {
self.mutex.lock()
defer { self.mutex.unlock() }
if let index = self.completionHandlers.index(where: { $0.invokerID == invokerID }) {
self.completionHandlers[index].completion(Result.failure(RequestError.canceled))
self.completionHandlers.remove(at: index)
if self.completionHandlers.isEmpty {
self.isIdle = true
operation?.cancel()
self.completedCallback(self)
}
}
}
// Request work to be done. It will only be done if it hasn't been done yet.
// The work block should set the operation on this request if possible. The
// work block should call requestFinished(result:) if the work has finished.
func request(work: #escaping RequestWork<T>,
completion: #escaping RequestCompletionHandler<T>) -> Int {
self.mutex.lock()
defer { self.mutex.unlock() }
let ID = UniqueID.getID()
self.completionHandlers.append((invokerID: ID, completion: completion))
guard self.isIdle else { return ID }
work(self)
self.isIdle = false
return ID
}
// This method should be called from the work block when the work has
// completed. It will pass the result to all completion handlers and call
// the Requester class to inform that this request has finished.
func requestFinished(result: Result<T>) {
self.mutex.lock()
defer { self.mutex.unlock() }
completionHandlers.forEach { $0.completion(result) }
completionHandlers = []
self.completedCallback(self)
self.isIdle = true
}
}
final class Requester<T> {
private lazy var requests = [Request<T>]()
private let mutex = NSLock()
init() { }
// reuqestFinished(request:) should be called after a single Request has
// finished its work. It removes the requests from the array of requests.
func requestFinished(request: Request<T>) {
self.mutex.lock()
defer { self.mutex.unlock() }
if let index = requests.index(where: { $0.ID == request.ID }) {
requests.remove(at: index)
}
}
// request(ID:, work:) will create a request or add a completion handler to
// an existing request if a request with the supplied ID already exists.
// When a request is created, it passes a closure that removes the request.
// It returns the invoker ID to the invoker for cancelation purposes.
func request(ID: String,
work: #escaping RequestWork<T>,
completion: #escaping RequestCompletionHandler<T>) ->
(Int, Request<T>) {
self.mutex.lock()
defer { self.mutex.unlock() }
if let existingRequest = requests.first(where: { $0.ID == ID }) {
let invokerID = existingRequest.request(work: work, completion: completion)
return (invokerID, existingRequest)
} else {
let request = Request<T>(ID: ID) { [weak self] (request) in
self?.requestFinished(request: request)
}
let invokerID = request.request(work: work, completion: completion)
return (invokerID, request)
}
}
}

Multiple timers in a loop

I have an array of characters, eg.
["A","E","I","O","U","Y"]
I should loop this array and, for every character, start a timer. That is: if the current letter is A, A must be printed and last 2 seconds. After A has been printed, we need to print "E" and it should last 3 seconds, "I" one second, "O" four seconds, etc.
I think I should create a new timer for every character, this way:
Timer.scheduledTimer(timeInterval: timeInterval, target: self, selector:
#selector(printCharacter), userInfo: nil, repeats: false)
but I can't do it inside the loop.
I hoped there was a way to build an array of timers and then fire them sequentially, but this is not possible. Any idea?
You can achieve this using recursion and DispatchQueue only. If you are not familiar with recursion a simple way to understand it's by googling it
A sample code if print duration isn't linear would look like following :
var currentIndex = 0
let charactersArray = ["A","B","C","D"]
let durations : [TimeInterval] = [1.0,2.0,3.0,4.0]
func printCharacter(){
guard currentIndex < charactersArray.count else { return }
print(charactersArray[currentIndex])
DispatchQueue.main.asyncAfter(deadline: .now() + durations[currentIndex], execute: {
currentIndex = currentIndex + 1
printCharacter()
})
}
printCharacter()
where durations array represent time shown for each character

Rx Swift simple timer not working

I have this RxSwift code in swift 3
let bag:DisposeBag = DisposeBag()
var sig:Observable<Int>!
sig = Observable<Int>.interval(1.0, scheduler: MainScheduler.instance)
sig.subscribe(onNext: { (milsec) in
print("Mil: \(milsec)")
}).addDisposableTo(bag)
i run this code when button tapped, but its not print anything on console.
DisposeBag will dispose of your subscription once it goes out of scope. In this instance, it'll be right after the call to subscribe, and it explains why you don't see anything printed to the console.
Move the definition of dispose bag to the class creating the subscription and everything should work fine.
class MyViewController: UIViewController {
let bag:DisposeBag = DisposeBag()
dynamic func onButtonTapped() {
var sig:Observable<Int>!
sig = Observable<Int>.interval(1.0, scheduler: MainScheduler.instance)
sig.subscribe(onNext: { (sec) in
print("Sec: \(sec)")
}).addDisposableTo(bag)
}
}
On a side note, interval expects an interval in seconds, so it will only tick every seconds as oposed to milliseconds.

Swift: test for expiration / invalidation of NSTimer seems to fail

Using Swift, I'm having difficulty testing for the expiration / invalidation of an NSTimer.
In particular, why does it appear that a test like while timer1 != nil { } or while timer1?.valid { } does not escape the null loop after it has been seemingly clearly invalidated and set to nil directly.
I have reviewed several of the questions relating to NSTimer already, and unless there is a version in Obj-C that I simply am not recognizing as the same issue due to my poor comprehension of ObjC properly, I don't believe this is covered (directly).
Please note, I'm not looking to directly "fix" the code, I'm looking for comprehension.
// ViewController.swift
// NSTimerTest
import UIKit
class ViewController: UIViewController {
var z:Int = 0
var timer1:NSTimer? = nil
#IBOutlet var lblMessage: UILabel
#IBOutlet var lbl2: UILabel
#IBAction func btnPress(sender: AnyObject) {
doBtnPress()
}
override func viewDidLoad() {
super.viewDidLoad()
z = 0
lblMessage.text = "Timer was called \(z) times."
lbl2.text = "waiting for countdown"
}
func doBtnPress(){
lblMessage.text = "doBtnPress!!"
timer1 = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "doTimedAction", userInfo: nil, repeats: true)
// All of the below infinitely loop
// while timer1?.valid { } // <=== never escapes, also depreciated in IOS 8.0 ???
// while timer1 { } // <==== never escapes
// while timer1 != nil { } // <==== never escapes
// while z >= 0 { } // <==== test counter instead, but no use
lbl2.text = "timer has been invalidated"
}
func doTimedAction(){
lblMessage.text = "Timer was called \(++z) times."
if z >= 10 {
timer1?.invalidate()
timer1 = nil
z = -1
}
}
}
As you can see, I have two labels -- one that is simply updated with the number of times the NSTimer has been invoked, and the other that hold be updated after I invalidate the timer.
With the testing lines commented out as noted above, things work as I expect. The label indicating the number of timer invocations updates each second (or so) and the immediate but factually incorrect display of the statement that "Timer has been invalidated" )
However, it my expectation that de-commenting any of the three lines that follow should allow the timer to run through 10 iterations, and then be invalidated, allowing the "Timer invalidated" message to display. Instead, however, I end up stuck infinitely in the empty loop, as if the condition(s) never come true. The UI does not update at all.
Instead of testing the timer, I also tried testing the counter z, but this does not work either, leading me to believe there is something more elemental at play that I am not understanding. For example, using while z >= 0 { println("z is \(z)")} in the location results in z is 0 -- the reason the loop is infinite. But again, if I comment out the line, z does in fact clearly increment, as the label DOES change, reflecting the cane in the counter z.
Again, I'm looking more to understand WHY things are failing (i.e., what I am failing to comprehend here). I understand, for example, I could make this "work" by having the doTimedAction() func directly update the label --- but that would not help me understand why my tests fail.
Whilst you are in the loop, you are blocking the timer from firing, as both the timer and loop are on the same thread.
Check out the documentation on Run Loops, and also this post on creating an NSTimer on a background thread.

Resources