在Swift 2中,我能够使用以下代码创建队列:
let concurrentQueue = dispatch_queue_create("com.swift3.imageQueue", DISPATCH_QUEUE_CONCURRENT)
Run Code Online (Sandbox Code Playgroud)
但这不能在Swift 3中编译.
在Swift 3中写这个的首选方法是什么?
我熟悉AsyncTaskAndroid中的使用:创建子类,调用子类execute的实例,并onPostExecute在UI线程或主线程上调用.什么是iOS中的等价物?
在Swift中编写的SpriteKit iOS游戏中,播放非常短的声音(~0.5s)会产生打嗝(如滞后).在其他问题中,我读到了我应该prepareToPlay()发出的声音.
我甚至使用变量(soundReady)来检查声音是否在播放前准备好了.每当完成播放(audioPlayerDidFinishPlaying())时我也会重新准备声音.以下是代码的相关部分:
class GameScene: SKScene, AVAudioPlayerDelegate {
var splashSound = NSURL()
var audioPlayer = AVAudioPlayer()
var soundReady = false
override func didMoveToView(view: SKView) {
let path = NSBundle.mainBundle().pathForResource("plopSound", ofType: "m4a")
splashSound = NSURL(fileURLWithPath: path)
audioPlayer = AVAudioPlayer(contentsOfURL: splashSound, error: nil)
audioPlayer.delegate = self
soundReady = audioPlayer.prepareToPlay()
}
func playSound(){
if(soundReady){
audioPlayer.play()
soundReady = false
}
}
func audioPlayerDidFinishPlaying(player: AVAudioPlayer!, successfully flag: Bool){
//Prepare to play after Sound finished playing
soundReady = audioPlayer.prepareToPlay()
} …Run Code Online (Sandbox Code Playgroud) 我刚刚在tableview中实现了库图表(https://github.com/danielgindi/Charts),但是当我的图表包含大量数据时,我在滚动期间遇到了相当大的延迟.
我在ChartTableViewCell中有一个方法,我根据传递的数据绘制图表,并从我的viewcontroller调用.
func updateCell(chartData: ChartData) {
DispatchQueue.global(qos: .background).async {
print("This is run on the background queue")
self.readings = (readings != nil) ? readings!.readings : []
self.period = period
if (chartData.isKind(of: LineChartData.self)) {
data.lineData = chartData as! LineChartData
}
else if (chartData.isKind(of: BarChartData.self)) {
data.barData = chartData as! BarChartData
}
}
DispatchQueue.main.async {
self.chartView.data = data
}
}
Run Code Online (Sandbox Code Playgroud)
在我的tableViewController中,我在解析数据后调用该函数:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let meta = chartMetas[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "chartCell", for: …Run Code Online (Sandbox Code Playgroud) 我有一组进程,每个进程都需要以一定的执行速率(每秒3次,每10秒一次等)在自己的后台线程上运行.
我相信 CFRunLoop和/或NSRunLoop提供此功能
如何创建(在swift中)执行定期任务的新后台线程?
我需要在后台调用一个调用 web 服务的 API。我不想将(非常复杂的)方法转换为异步,只需说“在后台执行所有这些操作”。
但是我不知道如何使用 F# 来做到这一点。这就是我所拥有的:
let task = async {
let result = SyncApi.syncData(login.url, login.zone, login.user, login.pwd) <-- THIS MUST RUN IN BACKGROUND...
match result with
|Some(msg) -> failwith msg
| None -> ()
}
task
|> Async.Catch
|> Async.RunSynchronously
|> fun x ->
match x with
| Choice1Of2 x -> rootPage.Navigation.PopToRootAsync(true) |> ignore
| Choice2Of2 ex -> showMsgError(ex.Message)
Run Code Online (Sandbox Code Playgroud) 我希望在下载数据时使用我的应用程序.不想被装载机困扰.我正在执行几个alamofire GET调用.做东西时我需要有什么平滑的应用程序.
let headers = [ "header" : "pass"
]
var nextCheck = next
if(next > count){
let tempN = next - 1000
nextCheck = count - tempN
}
Alamofire.request(.GET, urlDomain + "_table1?offset=\(nextCheck)", headers: headers, encoding: .URL)
.responseJSON() { response in
if response.result.isSuccess {
let json = JSON(response.result.value!)
//print(json)
let done = true
var doneDownloading = false
for i in 0..<json["resource"].count {
let id_pobor = json["resource"][i]["id_bor"].stringValue
let misto = json["resource"][i]["mio"].stringValue
let nazev_oc = json["resource"][i]["naze"].stringValue
let tel = json["resource"][i]["tel"].stringValue
let ulice …Run Code Online (Sandbox Code Playgroud) 我的班级的任务是制作一个实现 RESTful API 调用的应用程序。我的应用程序有 2 个屏幕:
在我们为该项目撰写的文章中,我们的任务是用“后台线程”指示一个代码片段。我不确定“后台线程”在我的代码中的位置。
我找到了这个 StackOverflow answer,它表明其中的代码在DispatchQueue.global(qos: .background).async {}后台线程中运行,而其中的代码在DispatchQueue.main.async {}主线程中运行。但是,我只有DispatchQueue.main.async {}和没有DispatchQueue.global(qos: .background).async {}。这是否意味着我没有后台线程?我现在必须实施它吗?还是所有不在主线程中的东西都默认在后台线程中运行?
列出所有 Pokemon 类型的ViewController.swift代码如下:
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
final let url = URL(string: "https://pokeapi.co/api/v2/type/")
private var results = [Movetype]()
@IBOutlet var tableView: UITableView!
var sequeIdentifiers = ["Normal", "Fighting", "Flying", "Poison", "Ground", "Rock", "Bug", "Ghost", "Steel", "Fire", "Water", …Run Code Online (Sandbox Code Playgroud) 我想推送一个视图控制器,并希望其余任务在后台工作而不冻结 UI。但要么我的 UI 冻结,要么后台线程之前执行。附上我工作的一个小例子。
DispatchQueue.global(qos: .background).async {
DispatchQueue.main.async {
print("This is run on the main queue, after the previous code in outer block")
}
print("This is run on the background queue")
}
Run Code Online (Sandbox Code Playgroud)
结果:
This is run on the background queue
This is run on the main queue, after the previous code in outer block
Run Code Online (Sandbox Code Playgroud)
主队列应该比后台线程先执行。