带有 NSTimer 的单例不从 AppDelegate 触发
Singleton with NSTimer not firing from AppDelegate
我正在尝试让一个带有 NSTimer
的单例从我的 AppDelegate
中触发,以便它在应用程序 运行 期间触发。虽然我可以让它在 Objective-C 中工作,但我似乎无法 link 为什么它在 Swift 中不起作用。我希望它每 30 秒轮询一次服务器并下载所有正在等待的消息。
我的 AppDelegate 中的代码是:
var myGetMessages:GetMessages!
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
..lots of other things ..
myGetMessages = GetMessages.sharedInstance
myGetMessages.downloadMessages()
}
我的单例有:
class GetMessages {
static let sharedInstance = GetMessages()
var messageDownloadTimer: NSTimer?
.. other init code ....
func downloadMessages() {
if myMainUser != nil {
if messageDownloadTimer == nil {
self.getMessagesFromServer(false, completionHandler: nil)
messageDownloadTimer = NSTimer(timeInterval: 30, target: self, selector: "downloadNewMessages", userInfo: nil, repeats: true)
}
} else {
println("There is no MainUser set up, so can't download Messages")
}
}
它从 AppDelegate 触发一次,然后再也不会。
关于我遗漏了什么的任何线索都很好。
更改下载功能;
func downloadMessages() {
if messageDownloadTimer == nil {
//?? messageDownloadTimer = NSTimer(timeInterval: 30, target: self, selector: "downloadNewMessages", userInfo: nil, repeats: true)
messageDownloadTimer = NSTimer.scheduledTimerWithTimeInterval(30, target: self, selector: "downloadNewMessages", userInfo: nil, repeats: true)
}
//--
if myMainUser != nil {
self.getMessagesFromServer(false, completionHandler: nil)
} else {
println("There is no MainUser set up, so can't download Messages")
}
}
我正在尝试让一个带有 NSTimer
的单例从我的 AppDelegate
中触发,以便它在应用程序 运行 期间触发。虽然我可以让它在 Objective-C 中工作,但我似乎无法 link 为什么它在 Swift 中不起作用。我希望它每 30 秒轮询一次服务器并下载所有正在等待的消息。
我的 AppDelegate 中的代码是:
var myGetMessages:GetMessages!
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
..lots of other things ..
myGetMessages = GetMessages.sharedInstance
myGetMessages.downloadMessages()
}
我的单例有:
class GetMessages {
static let sharedInstance = GetMessages()
var messageDownloadTimer: NSTimer?
.. other init code ....
func downloadMessages() {
if myMainUser != nil {
if messageDownloadTimer == nil {
self.getMessagesFromServer(false, completionHandler: nil)
messageDownloadTimer = NSTimer(timeInterval: 30, target: self, selector: "downloadNewMessages", userInfo: nil, repeats: true)
}
} else {
println("There is no MainUser set up, so can't download Messages")
}
}
它从 AppDelegate 触发一次,然后再也不会。
关于我遗漏了什么的任何线索都很好。
更改下载功能;
func downloadMessages() {
if messageDownloadTimer == nil {
//?? messageDownloadTimer = NSTimer(timeInterval: 30, target: self, selector: "downloadNewMessages", userInfo: nil, repeats: true)
messageDownloadTimer = NSTimer.scheduledTimerWithTimeInterval(30, target: self, selector: "downloadNewMessages", userInfo: nil, repeats: true)
}
//--
if myMainUser != nil {
self.getMessagesFromServer(false, completionHandler: nil)
} else {
println("There is no MainUser set up, so can't download Messages")
}
}