Singleton with NSTimer not firing from AppDelegate - ios

I'm trying to get a singleton with an NSTimer to fire from within my AppDelegate so that it fires for the duration of whilst the app is running. Whilst I can make it work in Objective-C, I can't seem to make the link as to why it's not working in Swift. Every 30 seconds I want it to poll a server and download any messages that are waiting.
The code I have in my AppDelegate is:
var myGetMessages:GetMessages!
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
..lots of other things ..
myGetMessages = GetMessages.sharedInstance
myGetMessages.downloadMessages()
}
My Singleton has:
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")
}
}
It fires once from the AppDelegate and then never again.
Any clue as to what I'm missing would be great.

Change your download function;
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")
}
}

Related

Send the location to the server every n minutes in the background in swift

i want to sent the location to the server when the app is background, i tried with many way but can't get the solution
I tried with the timer function using the timer and using function
self.bgtimer = Timer.scheduledTimer(timeInterval:60, target: self, selector: #selector(AppDelegate.bgtimer(_:)), userInfo: nil, repeats: true)
RunLoop.current.add(self.bgtimer, forMode: RunLoopMode.defaultRunLoopMode)
RunLoop.current.run()
but i when i stop the timer when the app becomes active
if UIApplication.shared.applicationState == .active {
RunLoop.current.cancelPerformSelectors(withTarget: self.bgtimer)
timer.invalidate()
}
but the app not function properly collection view and back function everything is not working properly
In DidBackground:
stop normalTimer.
start New backgroundTaskTimer
In WillForeground:
stop backgroundTaskTimer.
start normalTimer
Find below sample code:
In Appdelegate:
Declaration:
var backgroundUpdateTask: UIBackgroundTaskIdentifier!
var backgroundTaskTimer:Timer! = Timer()
Implementation:
func doBackgroundTask() {
DispatchQueue.global(qos: .default).async {
self.beginBackgroundTask()
if self.backgroundTaskTimer != nil {
self.backgroundTaskTimer.invalidate()
self.backgroundTaskTimer = nil
}
//Making the app to run in background forever by calling the API
self.backgroundTaskTimer = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(self.startTracking), userInfo: nil, repeats: true)
RunLoop.current.add(self.backgroundTaskTimer, forMode: RunLoopMode.defaultRunLoopMode)
RunLoop.current.run()
// End the background task.
self.endBackgroundTask()
}
}
func beginBackgroundTask() {
self.backgroundUpdateTask = UIApplication.shared.beginBackgroundTask(withName: "Track trip", expirationHandler: {
self.endBackgroundTask()
})
}
func endBackgroundTask() {
UIApplication.shared.endBackgroundTask(self.backgroundUpdateTask)
self.backgroundUpdateTask = UIBackgroundTaskInvalid
}
In ApplicationDidEnterBackground:
func applicationDidEnterBackground(_ application: UIApplication) {
//Start the background fetch
self.doBackgroundTask()
}
In ApplicationWillEnterForeground:
func applicationWillEnterForeground(_ application: UIApplication) {
if self.backgroundTaskTimer != nil {
self.backgroundTaskTimer.invalidate()
self.backgroundTaskTimer = nil
}
}

call timeout function after duration Callkit

I tried to add a timeout Timer 12 secs after receiving a call with CallKit but it doesn't get fired in Appdelegate when the app is in background. My code:
self.callBackgroundHandlerIdentifier = UIApplication.shared.beginBackgroundTask(expirationHandler: {
UIApplication.shared.endBackgroundTask(self.callBackgroundHandlerIdentifier!)
self.callBackgroundHandlerIdentifier = UIBackgroundTaskInvalid
})
DispatchQueue.global(qos: .userInitiated).async {
AppDelegate.callAnswerTimer = Timer.scheduledTimer(timeInterval: 12, target: self, selector: #selector(AppDelegate.hangUpOnCallTimeOut), userInfo: nil, repeats: false)
self.callBackgroundHandlerIdentifier = UIBackgroundTaskInvalid
}
func hangUpOnCallTimeOut(){
AppDelegate.timeOutTimer?.invalidate()
AppDelegate.timeOutTimer = nil
if #available(iOS 10.0, *) {
ProviderDelegate.sharedInstance.endCall(uuids: ApplicationDelegate.activeCalls!) { (uuid) in }
}
if self.callBackgroundHandlerIdentifier != UIBackgroundTaskInvalid {
UIApplication.shared.endBackgroundTask(self.callBackgroundHandlerIdentifier!)
}
}
Any ideas what I'm doing wrong?
a) dont add a timer in a block on the background. run the timer on the main queue!
b) dont reset self.callBackgroundHandlerIdentifier too early.
:) try below code
func ... {
self.callBackgroundHandlerIdentifier = UIApplication.shared.beginBackgroundTask(expirationHandler: {
print("expired!")
UIApplication.shared.endBackgroundTask(self.callBackgroundHandlerIdentifier!)
self.callBackgroundHandlerIdentifier = UIBackgroundTaskInvalid
})
AppDelegate.callAnswerTimer = Timer.scheduledTimer(timeInterval: 12, target: self, selector: #selector(AppDelegate.hangUpOnCallTimeOut), userInfo: nil, repeats: false)
}
}
func hangUpOnCallTimeOut(){
AppDelegate.timeOutTimer?.invalidate()
AppDelegate.timeOutTimer = nil
if #available(iOS 10.0, *) {
ProviderDelegate.sharedInstance.endCall(uuids: ApplicationDelegate.activeCalls!) { (uuid) in }
}
if self.callBackgroundHandlerIdentifier != UIBackgroundTaskInvalid {
UIApplication.shared.endBackgroundTask(self.callBackgroundHandlerIdentifier!)
self.callBackgroundHandlerIdentifier = UIBackgroundTaskInvalid
}
}

NSTimer invalidate not work

I can not stop the timer NSTimer created to perform secondary operations.
I have read and tried all sorts of possible Web guides but none have worked and the timer continues to run even when the Controller is destroyed. Below is the code in question:
/* Copy banner in a temp array and change order to random value */
private func getBanners(){
let realm = try! Realm()
let banners = self.synchronizer.getAllBanners()?.sorted("ordine")
if let banners = banners {
for banner in banners {
let random = Double.random(0.0, 1.0)
let currentOrderValue = banner.ordine
self.banners.append(banner)
do {
try realm.write({
self.banners.last!.ordine = currentOrderValue + random
})
} catch let error as NSError {
print(error)
}
}
}
}
/*Update index of banner to show */
func updateIndex(timer : NSTimer) {
if self.index+1 < self.banners.count {
for banner in self.banners{
print(banner.name + " \(banner.ordine)")
}
self.index+=1
} else {
self.index = 0
}
self.setImageBanner()
}
/* Set image of banner to show*/
private func setImageBanner() {
if self.banners.count > 0 {
self.bannerImage.hidden = false
let banner = self.banners[self.index]
let file = banner.images[0]
self.synchronizer.loadImageURL(file.link, imageView: self.bannerImage)
} else {
self.bannerImage.hidden = true
}
}
/* Start Timer */
func startTimerForBanners() {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.getBanners()
})
self.timer = NSTimer(timeInterval: 5, target: self, selector: #selector(self.updateIndex(_:)), userInfo: nil, repeats: true)
NSRunLoop.currentRunLoop().addTimer(self.timer!, forMode: NSRunLoopCommonModes)
}
/* Open link on banner click */
func openLink(sender : UITapGestureRecognizer){
if self.index >= 0 && self.index < self.banners.count {
let banner = self.banners[self.index]
print(banner.name)
self.synchronizer.openLink(banner.url)
}
}
func trialLesson(sender : UITapGestureRecognizer){
performSegueWithIdentifier("trial_lesson_segue", sender: nil)
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
if let timer = self.timer {
timer.invalidate()
}
}
you need to invalidate the timer before starting a new timer. Currently I believe you are calling "startTimerForBanners" many times may be, so many timer thread has been initiated. One approach is to invlidate all timer one by one or just invalidate before starting new one like this
/* Start Timer */
func startTimerForBanners() {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.getBanners()
})
**self.timer.invalidate()**
self.timer = NSTimer(timeInterval: 5, target: self, selector: #selector(self.updateIndex(_:)), userInfo: nil, repeats: true)
NSRunLoop.currentRunLoop().addTimer(self.timer!, forMode: NSRunLoopCommonModes)
}
Are you sure that your controller is really deallocated?
There is one thing you should be always aware about NSTimer: it retains it's target.
I would suggest to:
Start your timer in viewWillAppear
Invalidate and set timer to nil in viewDidDisappear
Also try replacing your start timer code with this (Swift 3):
timer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(self.updateIndex(_:)), userInfo: nil, repeats: true)

Timer.scheduledTimer Swift 3 pre-iOS 10 compatibility

I need to schedule a Timer for firing a function every second but I see that in Xcode 8 beta 3 the scheduledTimer is only available for iOS 10.
Is there any alternative for using the timer in iOS 9 or previous versions?
Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { (timer) in print("Hi!")})
Solved using
Timer.scheduledTimer(timeInterval: 1,
target: self,
selector: #selector(self.updateTime),
userInfo: nil,
repeats: true)
Run a timer with swift3,
var timer: Timer?
func startTimer() {
if timer == nil {
timer = Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(self.loop), userInfo: nil, repeats: true)
}
}
func stopTimer() {
if timer != nil {
timer?.invalidate()
timer = nil
}
}
func loop() {
let liveInfoUrl = URL(string: "http://192.168.1.66/api/cloud/app/liveInfo/7777")
let task = URLSession.shared.dataTask(with: liveInfoUrl! as URL) {data, response, error in
guard let data = data, error == nil else { return }
print(String(data: data, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue)) ?? "aaaa")
}
task.resume()
}
Release the timer when you not use it.
Once scheduled on a run loop, the timer fires at the specified
interval until it is invalidated. A nonrepeating timer invalidates
itself immediately after it fires. However, for a repeating timer, you
must invalidate the timer object yourself by calling its invalidate()
method.
Here is sample code workable with compatibility:
if #available(iOS 10.0, *) {
Timer.scheduledTimer(withTimeInterval: 15.0, repeats: true){_ in
// Your code is here:
self.myMethod()
}
} else {
Timer.scheduledTimer(timeInterval: 15.0, target: self, selector: #selector(self.myMethod), userInfo: nil, repeats: true)
}
//Your method or function:
// MARK: - Method
#objc func myMethod() {
print("Hi, How are you.")
}
Swift 3
func runCode(in timeInterval:TimeInterval, _ code:#escaping ()->(Void))
{
DispatchQueue.main.asyncAfter(
deadline: .now() + timeInterval,
execute: code)
}
func runCode(at date:Date, _ code:#escaping ()->(Void))
{
let timeInterval = date.timeIntervalSinceNow
runCode(in: timeInterval, code)
}
func test()
{
runCode(at: Date(timeIntervalSinceNow:2))
{
print("Hello")
}
runCode(in: 3.0)
{
print("World)")
}
}
Updated for swift 3:
If you want to use Timer for some delay or any other purpose used below lines of code in your project;
// function defination:
func usedTimerForDelay() {
Timer.scheduledTimer(timeInterval: 0.3,
target: self,
selector: #selector(self.run(_:)),
userInfo: nil,
repeats: false)
}
func run(_ timer: AnyObject) {
print("Do your remaining stuff here...")
}
// function call:
self.usedTimerForDelay()
NOTE:- you can change the time interval as you want.
//Enjoy coding..!
Timer.scheduledTimer
Put it in the main thread.
You can use the following simple shim to provide the new block-based Timers to pre-iOS 10:
class TimerShim {
private var timer: Timer?
private let block: (Timer) -> Void
private init(timeInterval interval: TimeInterval, repeats: Bool, block: #escaping (Timer) -> Void) {
self.block = block
timer = Timer.scheduledTimer(timeInterval: interval, target: self, selector: #selector(timerDidFire), userInfo: nil, repeats: repeats)
}
class func scheduledTimer(withTimeInterval interval: TimeInterval, repeats: Bool, block: #escaping (Timer) -> Void) -> Timer {
return TimerShim(timeInterval: interval, repeats: repeats, block: block).timer!
}
#objc private func timerDidFire() {
block(timer!)
}
}
Usage example:
TimerShim.scheduledTimer(withTimeInterval: 5, repeats: false) { _ in
print("boom!")
}
The correct form is:
Timer.scheduledTimer(withTimeInterval: 2, repeats: false){_ in
"Here your code method"
}

How to make NSTimer for multiple different controllers? In swift

I make app on page-based.based. I have multiple different controllers. Every controller has NSTimer which turn on when the controller viewDidAppear(). I made that NSTimer turn off when controller is viewDidDisappear(). It works when I slowly slide between controllers but if I fast slide controller some timers don't turn off. I tried several different methods. The first method I post notification to the function which turn off timer but it also works when I slowly slide between controllers. The second method I created public class timer which I used for every controller it doesn't work for me. I don't know how can I make it. Please help me. I also have NSTimer in UIView which is located on UIViewController and I need that when UIViewController is disappear to turn off two timers. How can I make it?
My examples on ViewController
var timer: NSTimer!
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(true)
notificationCenter.postNotificationName("turnOnMemoryArc", object: nil)
timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "showMemory", userInfo: nil, repeats: true)
}
override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(true)
if timer != nil {
timer.invalidate()
}
notificationCenter.postNotificationName("turnOffMemoryArc", object: nil)
}
This code in the view
override func drawRect(rect: CGRect) {
notificationCenter.addObserver(self, selector: "turnOnMemoryTimer", name: "turnOnMemoryArc", object: nil)
notificationCenter.addObserver(self, selector: "turnOffMemoryTimer", name: "turnOffMemoryArc", object: nil)
// timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "drawMemoryArc", userInfo: nil, repeats: true)
}
func turnOnMemoryTimer() {
print("memory timer is on")
timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "drawMemoryArc", userInfo: nil, repeats: true)
}
func turnOffMemoryTimer() {
if timer != nil {
timer.invalidate()
}
print("Memory timer turned")
}
The second method
import Foundation
public class GlobalTimer {
public init() { }
public var controllerTimer: NSTimer!
public var viewTimer: NSTimer!
}
ViewController
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(true)
notificationCenter.postNotificationName("turnOnBatteryArc", object: nil)
if GlobalTimer().controllerTimer != nil {
GlobalTimer().controllerTimer.invalidate()
GlobalTimer().controllerTimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "batteryStateForLabel", userInfo: nil, repeats: true)
} else {
GlobalTimer().controllerTimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "batteryStateForLabel", userInfo: nil, repeats: true)
}
}
UIView
func turnOnBatteryTimer() {
print("turnOnBatteryTimer arc")
if GlobalTimer().viewTimer != nil {
GlobalTimer().viewTimer.invalidate()
GlobalTimer().viewTimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "drawBatteryArc", userInfo: nil, repeats: true)
} else {
GlobalTimer().viewTimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "drawBatteryArc", userInfo: nil, repeats: true)
}
}
The main problem in your code is GlobalTimer(). This creates new GlobalTimer instance every single time you do write GlobalTimer().
Four different instance in the following piece of code - no shared state.
if GlobalTimer().viewTimer != nil {
GlobalTimer().viewTimer.invalidate()
GlobalTimer().viewTimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "drawBatteryArc", userInfo: nil, repeats: true)
} else {
GlobalTimer().viewTimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "drawBatteryArc", userInfo: nil, repeats: true)
}
You can fix it with singleton pattern ...
public class GlobalTimer {
public static let sharedInstance = GlobalTimer()
private init() { }
public var controllerTimer: NSTimer!
public var viewTimer: NSTimer!
}
... by replacing all GlobalTimer() occurences ...
if GlobalTimer().viewTimer != nil {
... with GlobalTimer.sharedInstace ...
if GlobalTimer.sharedInstance.viewTimer != nil {
Next step is ! removal, which is dangerous and can lead to crash if you don't know what you're doing.
public class GlobalTimer {
public static let sharedInstance = GlobalTimer()
private init() { } <-- avoid instantiation outside the file
public var controllerTimer: NSTimer? <-- was !
public var viewTimer: NSTimer? <-- was !
}
Remaining step is to replace your code using GlobalTimer ...
if GlobalTimer().viewTimer != nil {
GlobalTimer().viewTimer.invalidate()
GlobalTimer().viewTimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "drawBatteryArc", userInfo: nil, repeats: true)
} else {
GlobalTimer().viewTimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "drawBatteryArc", userInfo: nil, repeats: true)
}
... with ...
GlobalTimer.sharedInstance.viewTimer?.invalidate()
GlobalTimer.sharedInstance.viewTimer = NSTimer.scheduled...
First line says - call invalidate() function on viewTimer if viewTimer != nil otherwise nothing happens (similar to nil messaging in ObjC, ...). This allows you to remove the if condition, because it invalidates timer or does nothing.
Second line just assigns new timer.
Try putting timer.invalidate() in the view will disappear instead of the did disappear. You shouldn't need the notifications at all when doing this.

Resources