call function in background mode

my app running in the background and I want to take the html data from the web server every 60 second. (with alamofire or nsurlconnection etc..) but I could not find sample code associated with it. Is it possible to do something like this. updated every minute I want to do.


applications running in the background mode, updateServer function I want to call every 60 second


note: with background fetch methot not working every minute. after 3 minutes not called function.

func application(application: UIApplication, performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void)




    func updateServer() { 
        self.requesti =  request(.POST, "http://www.myserver.com/deerer.aspx" 
            .response { request, response, data, error in 
                let dataString = NSString(data: data!, encoding:NSUTF8StringEncoding) 
         
                ....... 
         
                let alis = scanned as! String 
                let satis = scanned as! String 
                self.addDataBase(alis, satis: satis) 
        } 
    } 
    func addDataBase(alis: String, satis: String) { 
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate 
        let kur  = NSEntityDescription.insertNewObjectForEntityForName("Kur", inManagedObjectContext: appDelegate.managedObjectContext) as! Entity 
 
        kur.alis = alis 
        kur.satis = satis 
        kur.tarih = NSDate() 
 
        appDelegate.saveContext() 
 
        let notification = UILocalNotification() 
        notification.alertBody = "testtesttest" 
        notification.fireDate = NSDate(timeIntervalSinceNow: 1) 
        notification.soundName = UILocalNotificationDefaultSoundName 
        UIApplication.sharedApplication().scheduleLocalNotification(notification) 
    }

No, it's not possible. Polling a server every 60 seconds is not one of the approved background activities. Only the specific capabilities listed under background modes (location updates, playing audio, interface with external accessory, etc.) are allowed for continuous background operation. Background fetch and/or silent push (content-available) notifications are your best option (really, your only option). And those will not guarantee running on any reliable schedule.

In addition to what junkpile said, which is all correct, I want to point out that polling a web server on every user’s device is extremely wasteful of precious resources. A better approach is to implement your own server that does this polling. This server will (presumably) have a fast and reliable network link, and a connection to mains power, so polling is not as costly. Moreover, you’ll only have one server doing the polling, rather than (potentially) millions of iOS devices. This server can then process the data, decide whether there’s anything that needs the user’s attention, and send them a push notification.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
call function in background mode
 
 
Q