2017-07-19 55 views
3

我是新来的后台任务。我有一个小工作,我正在获取推文,如果我的应用程序处于后台模式,那么它也应该获取推文,但我不知道如何。如何使用Swift 3使用后台任务?

我在的appdelegate didFinishLaunchOption方法使用简单的定时器。当我关闭应用程序时,它不起作用。我是新手,所以请提出任何建议。下面是我的代码:

Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(getTweets), userInfo: nil, repeats: true). 

func getTweets() { 

    let locationName = Helper.sharedInstance.userDefault.value(forKey: ModelKey.currentLocation) as? String 

    let accessToken = Helper.sharedInstance.userDefault.value(forKey: ModelKey.twitterAccessToken) as? String 

    if (locationName == "Bengaluru" && nil != accessToken) || (locationName == "Bangalore" && nil != accessToken){ 
     tweetModel.getTweets(accessToken: accessToken!, city: ModelKey.blrcitytraffic, cityName: "Bengaluru") 
    } 
} 

文本到语音也有,但是当我关闭应用程序然后停止发言。如果我不使用应用程序,那么它也可以获取推文和文本到语音应该使用背景模式工作。这工作多久了?

+1

相关:https://developer.apple.com/library/content/documentation/ iPhone /概念/ iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html –

回答

0

后台任务意味着你需要使用后台线程。 iOS中的线程太多了,但是如果你只想做后台任务,你应该使用两个线程;主要和后台线程的结构是:

DispatchQueue.global(qos: .background).async { 
    //background code 
    DispatchQueue.main.async { 
     //your main thread 
    }  
} 

所以,你首先用背景模式初始化全局队列。这个线程可以用于后台任务,然后你必须在后台任务完成时使用主线程(只在你需要的时候)做某件事。这可以是一个选项。在AppDelegate中,另一个选项应该是applicationDidEnterBackground,并且只能将该代码放入该方法中。

+0

我不知道为什么downvotes。这是我来这里寻找的。 – Suragch

+0

也帮助了我。 +1投票 –

3

你需要做三件事情:

  1. 在你的Info.plist添加关键Required background modes允许后台网络访问以下条目:

    Required background modes:App downloads content from the network

  2. 在你的AppDelegate添加到您的applicationDidEnterBackground():

    func applicationDidEnterBackground(_ application: UIApplication) { 
        // Fetch no sooner than every (60) seconds which is thrillingly short actually. 
        // Defaults to Infinite if not set. 
        UIApplication.shared.setMinimumBackgroundFetchInterval(60)) 
    } 
    
  3. 此外,在实施的AppDelegate

    func application(application: UIApplication, performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) { 
        var fetchResult: UIBackgroundFetchResult! 
    
    
        if doingYourStuffActuallyCreatesNetworkTraffic() { 
        fetchResult = UIBackgroundFetchResult.newData 
        } else if thereWasAnError() { 
        fetchResult = UIBackgroundFetchResult.failed 
        } else { 
        fetchResult = UIBackgroundFetchResult.noData 
        }   
        completionHandler(fetchResult) 
    
        return  
    } 
    

仍然有一些缺陷,例如没有保证的最大提取间隔,并且XCode/Simulator中的后台执行可能与真实设备中的执行有很大不同。

你可以看看这个漂亮的similiar话题:

performFetchWithCompletionHandler never gets fired

,当然还有 https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html