背景提取什么时候用完了?

时间:2016-02-15 13:56:17

标签: ios swift

我想知道在backgroundfetch模式

时实际设备会发生什么

例如,以下代码:

第1步:启用“后台获取”功能

第2步:设置AppDelegate.swift

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    let settings = UIUserNotificationSettings(forTypes: UIUserNotificationType.Alert, categories: nil)

    UIApplication.sharedApplication().registerUserNotificationSettings(settings)

    UIApplication.sharedApplication().setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)

    return true;





}

步骤3:在AppDelegate.swift中注册应用程序事件,以注册完成处理程序

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

    println("Complete");

    completionHandler(UIBackgroundFetchResult.NewData)



    getData();



}



func getData() -> Void{

    var url = "http://api.rottentomatoes.com/api/public/v1.0/lists/movies/box_office.json?apikey=xxxxxx&limit=20";

    var request = NSURLRequest(URL: NSURL(string: url));



    NSURLConnection.sendAsynchronousRequest(request,queue: NSOperationQueue.mainQueue()) {

        (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in

        var moviesResult = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: nil) as NSDictionary;

        var movies: [NSDictionary]=[];

        movies = moviesResult["movies"] as [NSDictionary];

        var localNotification:UILocalNotification = UILocalNotification()

        localNotification.alertAction = "Testing notifications on iOS8"

        localNotification.alertBody = "Movie Count : \(movies.count)"

        localNotification.fireDate = NSDate(timeIntervalSinceNow: 1)

        UIApplication.sharedApplication().scheduleLocalNotification(localNotification)

    }





}

执行getdata函数时?

有时间程序完全关闭,

一直在跑?

1 个答案:

答案 0 :(得分:1)

Apple不保证后台提取的执行频率。这取决于许多因素,包括手机的当前状态(即,您是WiFi还是蜂窝电话,强/弱信号,其他应用正在做什么,您的应用返回新数据的频率等等)和设置(它不是例如,在低功耗模式下运行)。它的运行频率与Apple认为合理的一样频繁。

如果您认为他们正在进行错误的判断调用,您将需要使用其他机制来触发您的getData方法。