Swift线程化多个任务?
Swift threading multiple tasks?
我有这个应该显示用户名的标签。现在,我已经进行了相当多的 IOS 开发,但是线程对我来说仍然有点不清楚。我如何确保此代码完成:
User(name: "", email: "", _id: "").getCurrentUser(userId: userId)
在执行之前?:
self.nameLabel.text = currentUser.name
我一直在摸索DispatchQueue
,但我似乎无法弄明白...
提前致谢!
你必须区分同步和异步任务。
通常,同步任务是阻塞程序执行的任务。在上一个任务完成之前,不会执行下一个任务。
异步任务则相反。一旦启动,执行就会转到下一条指令,您通常会通过委托或块从该任务中获得结果。
所以如果没有更多的指示,我们无法知道 getCurrentUser(:)
到底做了什么...
根据 Apple 的说法:
DispatchQueue manages the execution of work items. Each work item submitted to a queue is processed on a pool of threads managed by the system.
不一定是在后台线程上执行工作项。它只是一个结构,允许您在 queues(它可以是主队列或另一个队列)上同步或异步执行工作项。
作为一种解决方案,您可以使用 DispatchGroups 来执行此操作。这是一个例子:
// create a dispatch group
let group = DispatchGroup()
// go "into that group" starting it
group.enter()
// setup what happens when the group is done
group.notify(queue: .main) {
self.nameLabel.text = currentUser.name
}
// go to the async main queue and do primatry work.
DispatchQueue.main.async {
User(name: "", email: "", _id: "").getCurrentUser(userId: userId)
group.leave()
}
只需在您的 getCurrentUser()
方法中发送一个通知,并在您的 UIViewController 中添加一个观察者来更新标签。
public extension Notification.Name {
static let userLoaded = Notification.Name("NameSpace.userLoaded")
}
let notification = Notification(name: .userLoaded, object: user, userInfo: nil)
NotificationCenter.default.post(notification)
在你的 UIViewController 中:
NotificationCenter.default.addObserver(
self,
selector: #selector(self.showUser(_:)),
name: .userLoaded,
object: nil)
func showUser(_ notification: NSNotification) {
guard let user = notification.object as? User,
notification.name == .userLoaded else {
return
}
currentUser = user
DispatchQueue.main.async {
self.nameLabel.text = self.currentUser.name
}
}
我有这个应该显示用户名的标签。现在,我已经进行了相当多的 IOS 开发,但是线程对我来说仍然有点不清楚。我如何确保此代码完成:
User(name: "", email: "", _id: "").getCurrentUser(userId: userId)
在执行之前?:
self.nameLabel.text = currentUser.name
我一直在摸索DispatchQueue
,但我似乎无法弄明白...
提前致谢!
你必须区分同步和异步任务。 通常,同步任务是阻塞程序执行的任务。在上一个任务完成之前,不会执行下一个任务。 异步任务则相反。一旦启动,执行就会转到下一条指令,您通常会通过委托或块从该任务中获得结果。
所以如果没有更多的指示,我们无法知道 getCurrentUser(:)
到底做了什么...
根据 Apple 的说法:
DispatchQueue manages the execution of work items. Each work item submitted to a queue is processed on a pool of threads managed by the system.
不一定是在后台线程上执行工作项。它只是一个结构,允许您在 queues(它可以是主队列或另一个队列)上同步或异步执行工作项。
作为一种解决方案,您可以使用 DispatchGroups 来执行此操作。这是一个例子:
// create a dispatch group
let group = DispatchGroup()
// go "into that group" starting it
group.enter()
// setup what happens when the group is done
group.notify(queue: .main) {
self.nameLabel.text = currentUser.name
}
// go to the async main queue and do primatry work.
DispatchQueue.main.async {
User(name: "", email: "", _id: "").getCurrentUser(userId: userId)
group.leave()
}
只需在您的 getCurrentUser()
方法中发送一个通知,并在您的 UIViewController 中添加一个观察者来更新标签。
public extension Notification.Name {
static let userLoaded = Notification.Name("NameSpace.userLoaded")
}
let notification = Notification(name: .userLoaded, object: user, userInfo: nil)
NotificationCenter.default.post(notification)
在你的 UIViewController 中:
NotificationCenter.default.addObserver(
self,
selector: #selector(self.showUser(_:)),
name: .userLoaded,
object: nil)
func showUser(_ notification: NSNotification) {
guard let user = notification.object as? User,
notification.name == .userLoaded else {
return
}
currentUser = user
DispatchQueue.main.async {
self.nameLabel.text = self.currentUser.name
}
}