今天开发中遇到一个这样的问题,为了以后温故特作记录也方便遇到问题的同仁可以给点灵感和思路。
问题:
现在有20个网络请求需求,执行完毕就做个记录然后继续下面的逻辑,相信大家都知道多任务并发同时需要阻塞线程的常用方法是dispatch_group来处理,然后因为是大量的并发,所以我想到的是通过信号量dispatch_semaphore来控制最大并发数量,我设定阀值是最大不超过5个。所以就有以下代码:
MBProgressHUD.showAdded(to:self.view, animated:true)
let selectedCount:Int=friendsListView.selectedDatas.count
///限制每次最大请求次数不超过5个
varsempore =DispatchSemaphore(value:5)
ifselectedCount<5{
sempore =DispatchSemaphore(value: selectedCount)
}
let group =DispatchGroup()
// let queue = DispatchQueue(label: "invitedUsers_queue")
varsucessUser_ids = [String]()
friendsListView.selectedDatas.forEach { (user_id) in
// queue.async(group: group, execute: DispatchWorkItem(block: {
sempore.wait()
group.enter()
TIMManager.sharedInstance()?.getConversation(.C2C, receiver: user_id)?.send(WDTIMMessage.genMediaTIMMessage(isVoiceCall:false, mediaInstType: .request), succ: {
sempore.signal()
group.leave()
sucessUser_ids.append(user_id)
WDLog(TagConst.msgLstHeader,"\(user_id)的消息发送成功")
}, fail: { (errorcode, msg)in
sempore.signal()
group.leave()
WDLog(TagConst.msgLstHeader,"\(user_id)的消息发送失败")
})
// }))
}
group.notify(queue:DispatchQueue.main, work:DispatchWorkItem(block: {
WDSettingMgr.shared().saveInvitedSuccessUsers(user_ids: sucessUser_ids)
self.removeFriendsFromListDatas(friends: sucessUser_ids)
self.friendsListView.clearSelectedData()
WDLog(TagConst.msgLstHeader,"\(sucessUser_ids.count)条消息发送完成")
MBProgressHUD.hide(for:self.view, animated:true)
self.selectedNumber.text= LOCALE_STR("已选0/\(20 - WDSettingMgr.shared().getInvitedSuccessUsers().count)人")
}))
这段代码在5个并发以内没有任何问题,但是超5个就会卡死。
咋一看是逻辑正常没问题的,而且我一开始测试也发现正常。但是当测试数据大一点就出现问题了,经过一翻折腾,最后发现是我在用信号量阻塞了主线程,所以当超过5个信号量为0 阻塞住主线程,就卡死无法运行,找到问题,就改进,赶紧搞个子线程进行处理,然后这个时候group直接就收到通知了,然而里面数据安全没有更新,仔细检查发现是我没把队列放到group里面。最后贴下正确代码:
MBProgressHUD.showAdded(to:self.view, animated:true)
letselectedCount:Int=friendsListView.selectedDatas.count
///限制每次最大请求次数不超过5个
varsempore =DispatchSemaphore(value:5)
ifselectedCount<5{
sempore =DispatchSemaphore(value: selectedCount)
}
letgroup =DispatchGroup()
letqueue =DispatchQueue(label:"invitedUsers_queue")
varsucessUser_ids = [String]()
friendsListView.selectedDatas.forEach { (user_id) in
queue.async(group: group, execute:DispatchWorkItem(block: {
sempore.wait()
group.enter()
TIMManager.sharedInstance()?.getConversation(.C2C, receiver: user_id)?.send(WDTIMMessage.genMediaTIMMessage(isVoiceCall:false, mediaInstType: .request), succ: {
sempore.signal()
group.leave()
sucessUser_ids.append(user_id)
WDLog(TagConst.msgLstHeader,"\(user_id)的消息发送成功")
}, fail: { (errorcode, msg)in
sempore.signal()
group.leave()
WDLog(TagConst.msgLstHeader,"\(user_id)的消息发送失败")
})
}))
}
group.notify(queue:DispatchQueue.main, work:DispatchWorkItem(block: {
WDSettingMgr.shared().saveInvitedSuccessUsers(user_ids: sucessUser_ids)
self.removeFriendsFromListDatas(friends: sucessUser_ids)
self.friendsListView.clearSelectedData()
WDLog(TagConst.msgLstHeader,"\(sucessUser_ids.count)条消息发送完成")
MBProgressHUD.hide(for:self.view, animated:true)
self.selectedNumber.text= LOCALE_STR("已选0/\(20 - WDSettingMgr.shared().getInvitedSuccessUsers().count)人")
}))
}
总结一下个人的理解,开发中以后要注意多线程的队列执行是并行还是串行,不然可能出现常见的死锁问题。
网友评论