分布式系统中的并发编程与任务处理
1. 系统管理与连接处理
在系统管理方面,我们需要处理系统断开连接的情况。在第二个任务闭包中添加以下代码:
for await notification in
NotificationCenter.default.notifications(named: .disconnected)
{
guard let name = notification.object as? String else
{ return }
print("[Notification] Disconnected: \(name)")
await systems.removeSystem(name: name)
Task { @MainActor in
isConnected = await systems.systems.count > 1
}
}
此任务与之前的任务类似,当对等节点断开连接时,会根据名称移除系统。完成上述代码添加后,还需在 init(total:localName:) 中调用新方法,即添加 systemConnectivityHandler() 这一行代码。
为了让连接操作更有趣,我们可以添加一个连接指示器。打开 SkyApp.swift 文件,在警报视图修饰符下方插入以下代码:
.toolbar {
if scanModel.
超级会员免费看
订阅专栏 解锁全文

被折叠的 条评论
为什么被折叠?



