2017-02-05 2 views
0

ist Code, die ich im Hintergrund ausgeführt werden, wenn der Benutzer die App schließen, aber es ist seltsam Verhalten, nachdem endBackgroundUpdateTask() Methode ausgeführt wird, wird DispatchQueue noch nicht beendet ...Wie man backgroundUpdateTask stoppt? Hier

I Stahl Kontinuierlich get-Benachrichtigung.

Was mache ich falsch?

können Sie versuchen, diese snipped Code zu nehmen und versuchen, für sich selbst, ist es wirklich seltsam

var backgroundUpdateTask: UIBackgroundTaskIdentifier! 

func beginBackgroundUpdateTask() { 
    print("beginBackgroundUpdateTask") 
    self.backgroundUpdateTask = UIApplication.shared.beginBackgroundTask(expirationHandler: { 
     self.endBackgroundUpdateTask() 
    }) 
} 

func endBackgroundUpdateTask() { 
    print("endBackgroundUpdateTask") 
    UIApplication.shared.endBackgroundTask(self.backgroundUpdateTask) 
    self.backgroundUpdateTask = UIBackgroundTaskInvalid 
} 

func doBackgroundTask() { 
    print("Strart") 
    DispatchQueue.global().async { 
     self.beginBackgroundUpdateTask() 

     // Do something with the result. 
     let timer = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(AppDelegate.displayAlert), userInfo: nil, repeats: true) 
     RunLoop.current.add(timer, forMode: RunLoopMode.defaultRunLoopMode) 
     RunLoop.current.run() 

     // End the background task. 
     self.endBackgroundUpdateTask() 
    } 

    print("Finish") 
} 

func displayAlert() { 
    print("displayAlert") 
    let note = UILocalNotification() 
    note.alertBody = "As a test I'm hoping this will run in the background every X number of seconds..." 
    note.soundName = UILocalNotificationDefaultSoundName 
    UIApplication.shared.scheduleLocalNotification(note) 
} 

func applicationDidEnterBackground(_ application: UIApplication) { 
    log.debug("applicationDidEnterBackground") 
    self.doBackgroundTask() 

} 

bearbeiten

var backgroundUpdateTask: UIBackgroundTaskIdentifier! 
var timerr: Timer? 

func beginBackgroundUpdateTask() { 
     appDeligate.log.debug("beginBackgroundUpdateTask") 
     self.backgroundUpdateTask = UIApplication.shared.beginBackgroundTask(expirationHandler: { 
     self.endBackgroundUpdateTask() 
    }) 
} 

func endBackgroundUpdateTask() { 
    appDeligate.log.debug("endBackgroundUpdateTask") 
    UIApplication.shared.endBackgroundTask(self.backgroundUpdateTask) 
    self.backgroundUpdateTask = UIBackgroundTaskInvalid 
    timerr = nil 
} 

func doBackgroundTask() { 
    print("Strart") 
    DispatchQueue.global().async { 
     self.beginBackgroundUpdateTask() 

     // Do something with the result. 
     self.timerr = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(TestViewController.displayAlert), userInfo: nil, repeats: true) 
     RunLoop.current.add(self.timerr!, forMode: RunLoopMode.defaultRunLoopMode) 
     RunLoop.current.run() 

     // End the background task. 
     self.endBackgroundUpdateTask() 
    } 

    print("Finish") 
} 

func displayAlert() { 
    print("displayAlert") 
    let note = UILocalNotification() 
    note.alertBody = "As a test I'm hoping this will run in the background every X number of seconds..." 
    note.soundName = UILocalNotificationDefaultSoundName 
    UIApplication.shared.scheduleLocalNotification(note) 
} 
+1

Sie nie den Timer ungültig machen, so dass es weiter zündet. Nichts mit "Hintergrundaufgaben" zu tun. – matt

+0

@matt Ich habe den bearbeiteten Code in Frage gestellt, hast du darüber gesprochen? Wenn ja, es ist Stahl funktioniert nicht (oder Sie meinen etwas anderes? –

+0

Ich sehe nicht, welchen Unterschied Ihre Bearbeitung macht. Ich wiederhole, die ganze "Hintergrundaufgaben" Sache ist hier irrelevant. Sobald ein Wiederholungstimer gestartet wird, es geht einfach weiter (im Simulator), bis du 'invalidate()' darauf nennst. Du tust es nie, also geht es weiter. – matt

Antwort

0

Wirklich Problem mit Timer war, musste ich wurde diese setzen Linie

timerr?.invalidate() 

hier

func endBackgroundUpdateTask() { 
    appDeligate.log.debug("endBackgroundUpdateTask") 
    UIApplication.shared.endBackgroundTask(self.backgroundUpdateTask) 
    self.backgroundUpdateTask = UIBackgroundTaskInvalid 
    timerr?.invalidate() 
} 

Aber trotzdem ist es etwas seltsam, weil ich dachte, wenn ich anhielt backgroundUpdate() alle Aufgaben innerhalb ungültig machen musste und reinigen automatisch, aber nein.

Dank @matt