1

I built my application with the management of the background task in this way:

var updateTimer: NSTimer?
var backgroundTask: UIBackgroundTaskIdentifier = UIBackgroundTaskInvalid

override func viewDidLoad() {
    super.viewDidLoad()

    // listen for UIApplicationDidEnterBackgroundNotification

    NSNotificationCenter.defaultCenter().addObserver(self,
    selector: "appDidEnterBackground:",
    name: UIApplicationDidEnterBackgroundNotification, object: nil)
}

func reinstateBackgroundTask() {
    if updateTimer != nil && (backgroundTask == UIBackgroundTaskInvalid) {
        registerBackgroundTask()
    }
}

func registerBackgroundTask() {
    backgroundTask = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler {
        [unowned self] in
        self.endBackgroundTask()
    }
    assert(backgroundTask != UIBackgroundTaskInvalid)
}

func endBackgroundTask() {
    NSLog("Background task ended.")
    UIApplication.sharedApplication().endBackgroundTask(backgroundTask)
    backgroundTask = UIBackgroundTaskInvalid
}

func calculateNextNumber() {
    switch UIApplication.sharedApplication().applicationState {
    case .Active:
        break
    case .Background:
        NSLog("Background time remaining = %.1f seconds", UIApplication.sharedApplication().backgroundTimeRemaining)
    case .Inactive:
        break
    }
}

func appDidEnterBackground(notification:NSNotification) {
    updateTimer?.invalidate()
    updateTimer = nil
    if backgroundTask != UIBackgroundTaskInvalid {
        endBackgroundTask()
    }
}

but I can not block the background timer . I would like to use the three minutes not continuously but fragmenting, for example using a 5 second intervals. I do not know if you can do , let me know if this is possible and how. thank you Edit: I certainly have not been clear. I can stop and resume the remaining time of the backgroundTimeRemaining ? example

if the backgroundTimeRemaining == 168.0 {
    endBackgroundTask()
}

I have tried it, but it doesn't work !!!

Rob
  • 415,655
  • 72
  • 787
  • 1,044
Milena
  • 29
  • 1
  • 8
  • No, the fixed amount of time your app is given to complete background tasks (usually 3 minutes) is total elapsed time. You can't stretch it out by using 5 seconds at a time or anything like that. – Rob Jan 12 '16 at 01:38

0 Answers0