Skip to content

Instantly share code, notes, and snippets.

@JARinteractive
Last active September 4, 2022 14:40

Revisions

  1. JARinteractive revised this gist Mar 7, 2018. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions WaitUntil.swift
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    // https://gist.github.com/JARinteractive/7fb33b6b0043f365ddfd
    import Foundation
    import XCTest

  2. JARinteractive revised this gist Mar 7, 2018. 1 changed file with 15 additions and 10 deletions.
    25 changes: 15 additions & 10 deletions WaitUntil.swift
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,19 @@
    import Foundation
    import XCTest

    func waitUntil(_ checkSuccess: @autoclosure ()->Bool) {
    return waitUntil(1.0, checkSuccess)
    @discardableResult
    public func AssertEventuallyTrue(file: StaticString = #file, line: UInt = #line, _ checkSuccess: @autoclosure () -> Bool) -> Bool {
        return AssertEventuallyTrue(10.0, file: file, line: line, checkSuccess)
    }

    func waitUntil(_ timeout: Double, _ checkSuccess: @autoclosure ()->Bool) {
    let startDate = NSDate()
    var success = false
    while !success && abs(startDate.timeIntervalSinceNow) < timeout {
    RunLoop.current.run(until: Date(timeIntervalSinceNow: 0.01))
    success = checkSuccess()
    }
    }
    @discardableResult
    public func AssertEventuallyTrue(_ timeout: Double, file: StaticString = #file, line: UInt = #line, _ checkSuccess: @autoclosure () -> Bool) -> Bool {
        let startDate = NSDate()
        var success = false
        while !success && abs(startDate.timeIntervalSinceNow) < timeout {
            RunLoop.current.run(until: Date(timeIntervalSinceNow: 0.01))
            success = checkSuccess()
        }
        if !success { XCTFail("Timeout occurred while waiting for condition (waitUntil)", file: file, line: line) }
        return success
    }
  3. JARinteractive revised this gist Jan 19, 2017. 1 changed file with 10 additions and 11 deletions.
    21 changes: 10 additions & 11 deletions WaitUntil.swift
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,14 @@
    import Foundation

    func waitUntil(@autoclosure checkSuccess: ()->Bool) -> Bool {
    return waitUntil(1.0, checkSuccess)
    func waitUntil(_ checkSuccess: @autoclosure ()->Bool) {
    return waitUntil(1.0, checkSuccess)
    }

    func waitUntil(timeout: Double, @autoclosure _ checkSuccess: ()->Bool) -> Bool {
    let startDate = NSDate()
    var success = false
    while !success && abs(startDate.timeIntervalSinceNow) < timeout {
    NSRunLoop.currentRunLoop().runUntilDate(NSDate(timeIntervalSinceNow: 0.01))
    success = checkSuccess()
    }
    return success
    }
    func waitUntil(_ timeout: Double, _ checkSuccess: @autoclosure ()->Bool) {
    let startDate = NSDate()
    var success = false
    while !success && abs(startDate.timeIntervalSinceNow) < timeout {
    RunLoop.current.run(until: Date(timeIntervalSinceNow: 0.01))
    success = checkSuccess()
    }
    }
  4. JARinteractive created this gist Feb 17, 2016.
    15 changes: 15 additions & 0 deletions WaitUntil.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    import Foundation

    func waitUntil(@autoclosure checkSuccess: ()->Bool) -> Bool {
    return waitUntil(1.0, checkSuccess)
    }

    func waitUntil(timeout: Double, @autoclosure _ checkSuccess: ()->Bool) -> Bool {
    let startDate = NSDate()
    var success = false
    while !success && abs(startDate.timeIntervalSinceNow) < timeout {
    NSRunLoop.currentRunLoop().runUntilDate(NSDate(timeIntervalSinceNow: 0.01))
    success = checkSuccess()
    }
    return success
    }