我对在后台队列上运行 CoreData 代码有一些困惑。
我们可以使用一些方法使用 NSManagedObjectContext 在后台线程上执行 CoreData 代码。
viewContext.perform { /*some code will run on the background thread*/ }
viewContext.performAndWait{ /*some code will run on the background thread*/ }
Run Code Online (Sandbox Code Playgroud)
我的问题是为什么我应该使用这些函数而不是使用普通方式使用 DispatchQueue 在后台线程上运行一些代码
DispatchQueue.global(qos: .background).async {
/*some code will run on the background thread*/
}
Run Code Online (Sandbox Code Playgroud) 我知道队列的创建并能够执行单个任务,但我如何并行执行多个任务。
并发队列---->
let concurrentQueue = DispatchQueue(label: "com.some.concurrentQueue", attributes: .concurrent)
concurrentQueue.async {
//executable code
}
Run Code Online (Sandbox Code Playgroud)
无优先级的BackgroundQueue 默认--->
DispatchQueue.global().async {
//executable code
}
Run Code Online (Sandbox Code Playgroud)
具有优先级的后台队列---->
DispatchQueue.global(qos: .userInitiated).async { //.userInteractive .background .default .unspecified
//executable code
}
Run Code Online (Sandbox Code Playgroud)
回到主队列---->
DispatchQueue.main.async {
//executable code
}
Run Code Online (Sandbox Code Playgroud)
所有都是异步的,但是我如何一次执行多个方法我应该如何快速编码。
我有两个执行异步的函数。
我尝试将它们“同步”:DispatchGroup 和 DispatchQueue
let queue = DispatchQueue(label: "com.company.app.queue", attributes: .concurrent)
let group = DispatchGroup()
queue.async(group: group) {
//func1
}
queue.async(group: group) {
//func2
}
group.notify(queue: queue) {
print("#3 finished")
}
Run Code Online (Sandbox Code Playgroud)
Func1 和 Func2 仅调用:
class func getDataFromUrl( url: URL, completion: @escaping ( Data?, URLResponse?, Error? ) -> ( ) )
{
URLSession.shared.dataTask( with: url )
{
data, response, error in
completion( data, response, error )
}.resume( )
}
Run Code Online (Sandbox Code Playgroud)
但问题是我不知道如何等待queue.async中的完成块..
有人有什么想法吗?
如何修复此代码,使其不会出现错误:没有更多上下文的表达式类型不明确?
var int = 0
while int < 100 {
DispatchQueue.main.asyncAfter(deadline: .now() + int) { // change 2 to desired number of seconds
// do stuff
}
int += 1
}
Run Code Online (Sandbox Code Playgroud)
该代码正在 applicationDidEnterBackground 的调度队列中运行,以尝试在后台进行文本到语音的工作。
为什么print("2")在下面的代码中永远不会调用该部分?我认为内部main.async会将块推入主循环的队列中,然后RunLoop.run执行它,但显然情况并非如此。(它打印1、run、run、run等)
另外,如果我删除外部块main.async,然后直接运行该块中的代码(仍在主队列上,在viewDidLoad新的单视图应用程序中),那么内部main.async块就会被执行(打印1,,run)2。为什么这一变化会产生如此大的差异?
var x = -1
DispatchQueue.main.async { // comment out this line for question #2
print("1")
x = 1
DispatchQueue.main.async {
print("2")
x = 2
}
while x == 1 {
print("run")
RunLoop.main.run(mode: .default, before: Date() + 1)
}
} // comment out this line for question #2
Run Code Online (Sandbox Code Playgroud) 每个人Thread都有自己的RunLoop,如何DispatchQueue与他们互动?是DispatchQueue使用RunLoop分派任务Thread还是以其他方式完成?
nsoperationqueue grand-central-dispatch runloop swift dispatch-queue
我有一个用于introViewController的DispatchQueue,它显示一个11秒的gif然后显示我的登录页面......但是还有一个跳过介绍并显示登录的按钮.当我点击它时,仍在运行的时间和当我在应用程序中导航时,在时间结束时返回登录.
这是我的班级
class GifClass: UIViewController {
@IBOutlet weak var gifImage: UIImageView!
@IBOutlet weak var skipButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
gifImage.loadGif(name: "promed")
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(11)) {
self.performSegue(withIdentifier: "introLogin", sender: self)
}
}
@IBAction func skip(_ sender: Any) {
performSegue(withIdentifier: "introLogin", sender: self)
}
}
Run Code Online (Sandbox Code Playgroud)
如何点击按钮停止时间?
我在这里和那里进行了一些搜索,但我没有找到(或理解)如何在SpriteKit中循环延迟.
这是个想法:我有一些SKSpriteNodes按数组排序,我想在屏幕上显示它们,每秒一个.问题是......我根本无法做到这一点(对不起,我对此很新).
let sprite1 = SKSpriteNode(color: .red, size: CGSize(width: 20, height: 20))
sprite1.position = CGPoint(x: 100, y: 100)
let sprite2 = SKSpriteNode(color: .red, size: CGSize(width: 20, height: 20))
sprite2.position = CGPoint(x: 100, y: 300)
let sprite3 = SKSpriteNode(color: .red, size: CGSize(width: 20, height: 20))
sprite3.position = CGPoint(x: 300, y: 100)
let sprite4 = SKSpriteNode(color: .red, size: CGSize(width: 20, height: 20))
sprite4.position = CGPoint(x: 300, y: 300)
let allSprites : [SKSpriteNode] = [sprite1, sprite2, sprite3, sprite4]
let delay = 1.0
var …Run Code Online (Sandbox Code Playgroud) 通过使用以下代码,我期望每次调用 func 时,执行之间的最短时间为 3-4 秒。
但是:当我writeData()连续调用4 次时,我看到异步块在不等待前一次调用完成的情况下执行。
func writeData(){
DispatchQueue(label: "be.io").asyncAfter(deadline: .now() + 1) {
print("START :\(Int64((Date().timeIntervalSince1970 * 1000.0).rounded()))")
Thread.sleep(forTimeInterval: 3)
}
}
...
writeData()
writeData()
writeData()
writeData()
Run Code Online (Sandbox Code Playgroud)
预期的输出应该是这样的:
START :1611250630000
START :1611250634000
START :1611250638000
START :1611250642000
Run Code Online (Sandbox Code Playgroud)
但在执行时,我得到了所有 4 个调用相同的时间戳(大约 1 毫秒的差异)。
START :1611250630000
START :1611250630000
START :1611250630000
START :1611250630000
Run Code Online (Sandbox Code Playgroud)
我在做什么错?
PS我毫不拖延地尝试了异步,没有运气。
对于这个 swift 游乐场(查看 swiftfiddle或以下),我有三个流程密集型工作循环。使用线程的线程会产生 50 个线程,所有线程一起工作(通过最后报告的循环数量的增加来显示),而具有并发性的线程只会同时执行两个任务。什么决定了要在其中运行任务的线程数?正如我在评论中提到的,一种方法不一定比另一种方法更快,因为 50 个线程竞争资源远非理想,但 2 似乎是一个非常低的数字。
import PlaygroundSupport
import UIKit
PlaygroundPage.current.needsIndefiniteExecution = true
func asyncProcess(_ this:Int) async{
print(" async -- starting \(this)")
let x = Int.random(in: 1000...10000000)
await withUnsafeContinuation{
for _ in 1..<x {}
$0.resume()
}
print(" async -- ending \(this) after \(x) times")
}
func threadedProcess(_ this:Int) {
print("threaded -- starting \(this) on \(Thread.current)")
let x = Int.random(in: 1000...10000000)
for _ in 1..<x {}
print("threaded -- ending \(this) after \(x) times on \(Thread.current)") …Run Code Online (Sandbox Code Playgroud) dispatch-queue ×10
swift ×9
ios ×4
xcode ×2
asynchronous ×1
core-data ×1
delay ×1
nsrunloop ×1
runloop ×1
sprite-kit ×1
swift4 ×1