具有完美 : Add a scheduled timer with interval to the runLoop 的 Swift 3 Linux

标签 swift timer swift3 nsrunloop perfect

我正在尝试使用 Perfect library 在我的 Ubuntu(Ubuntu 15.10 wily,Swift swift-3.0.1-RELEASE)上使用 Swift 创建一个应用程序.

我希望每隔 X 秒调用一个函数。为此,我正在使用 Timer class of the Foundation module :

class MyTimer {
    init() {
        var timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(MyTimer.onTimer(timer:)), userInfo: nil, repeats: true)
    }
    @objc func onTimer(timer: Timer) {
        print("MyTimer.onTimer")
    }
}

尽管用这段代码找到了几个解决方案,但编译失败:

$> swift build
Compile Swift Module 'my-app' (7 sources)
/home/.../Sources/MyTimer.swift:8:16: error: method cannot be marked @objc because the type of the parameter cannot be represented in Objective-C
    @objc func onTimer(timer: Timer) {

如果我从 NSObject 扩展我的类,或者如果我删除了参数 timer,则会出现另一个编译错误:

$> swift build
Compile Swift Module 'my-app' (7 sources)
/home/.../Sources/MyTimer.swift:6:83: error: '#selector' can only be used with the Objective-C runtime
    var timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(MyTimer.onTimer), userInfo: nil, repeats: true)

我尝试使用其他不使用选择器的声明:

class MyTimer {
    init() {
        print("MyTimer.init")
        var timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) {
            timer in
            print("MyTimer.onTimer")
        }
    }
}

编译有效,但我的第二次打印从未被调用。我还尝试手动将我的计时器添加到当前 RunLoop:

class MyTimer {
    init() {
        print("MyTimer.init")
        var timer = Timer(timeInterval: 1, repeats: true) {
            timer in
            print("MyTimer.onTimer")
        }
        RunLoop.current.add(timer, forMode: .defaultRunLoopMode)
        // timer.fire()
    }
}

再也不会调用(timer.fire() 只调用一次我的函数)。最后:

class MyTimer {
    init() {
        print("MyTimer.init")
        let timer = Timer(timeInterval: 1, repeats: true) {
            timer in
            print("MyTimer.onTimer")
        }
        RunLoop.current.add(timer, forMode: .defaultRunLoopMode)
        RunLoop.current.run(until: Date(timeIntervalSinceNow: 4.0))
    }
}

我的消息 "MyTimer.onTimer" 打印了 5 次,但我的服务器(使用 Perfect 库)只在最后启动:

$> swift build && ./.build/debug/my-app 8400
Compile Swift Module 'my-app' (7 sources)
Linking ./.build/debug/my-app
MyTimer.init
MyTimer.onTimer
MyTimer.onTimer
MyTimer.onTimer
MyTimer.onTimer
MyTimer.onTimer
[INFO] Starting HTTP server  on 0.0.0.0:8181

我不知道该尝试什么了。 Perfect库可能有问题,但我找不到任何解决办法。我也许可以运行一个新线程,并在其中启动我的计时器,但它会变得有点复杂吗?

最佳答案

如果你认真地使用 Perfect,请不要使用 Foundation 的东西。尝试完美线程:http://www.perfect.org/docs/thread.html

import PerfectThread 
#if os(Linux)
import GlibC
#else
import Darwin
#endif

Threading.dispatch {
  sleep(10) // wait for 10 seconds
  doSomething()
}//end threading

既安全又简单
非常典型的服务器端编码

关于具有完美 : Add a scheduled timer with interval to the runLoop 的 Swift 3 Linux,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41328989/

相关文章:

visual-c++ - 使用定时器提高读/写串行端口的速度

ios - 使用硬件加速内容截取 WKWebview 的屏幕截图

ios - 通过 UIImagepickerController 从相机中选取图像并保存到 ios 中的相册后获取图像名称

string - 如何在 swift 3.0 中连接多个可选字符串?

linux - 为什么我在使用 linux hrtimer 进行一秒回调时会出现时间漂移?

objective-c - 核心数据或 NSUserDefaults 以避免操纵用户?

swift - 将sql中的数据拉取到 TableView

ios - 类 UITableViewCell 没有名为 Storyboard的成员

ios - RealmSwift 嵌套对象 getter 不返回数据

flutter - 使用 Timer 而不是 Future.delayed(...) 创建异步等待步骤