Swift4.0 使用GCD多线程请求多个网络数据

Swift4.0 中,GCD的API更加独立,而不再是像OC一样了,所以我们经常遇到这样一种场景,某个页面需要多个网络请求才可以获得全部数据,在没有顺序要求的情况下,使用GCD Group进行线程调度是最合适的,如果存在顺序要求,需要使用信号量,信号量的具体方法在这里不讨论,下面展示如何使用Group请求多个线程网络数据:

   func get(){
        
        let queue = DispatchQueue.init(label: "getCount")//定义队列
        let group = DispatchGroup()//创建一个组
        
        //将队列放进组里
        queue.async(group: group, execute: {
            group.enter()//开始线程1
            
            let url = rootUrl + "/getDetailedQuestion.do"
            let parame:[String:AnyObject] = {
                return ["questionId":self.id]
                }() as [String : AnyObject]
            
            AlaRequestManager.shared.POST(urlString: url, params: parame, success: ({success in
              //  print(success)
                print("thread1")
                group.leave()//线程1结束
            }))
    
        })
        
        
        queue.async(group: group, execute: {
            group.enter()//开始线程2
            
            let url = rootUrl + "/getDetailedQuestion.do"
            let parame:[String:AnyObject] = {
                return ["questionId":self.id]
                }() as [String : AnyObject]
            
            AlaRequestManager.shared.POST(urlString: url, params: parame, success: ({success in
               // print(success)
                print("thread2")
                group.leave()//线程2结束
            }))
        })
        
        
        group.notify(queue: queue){
            //队列中线程全部结束
            print("end")
        }
        
        
    }
    



链接:https://www.jianshu.com/p/60a66c38f5d6

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Swift 4.0中,系统没有提供直接的底部弹窗控件。但是你可以使用UIAlertController和UIAlertAction来创建一个自定义的底部弹窗。下面是一个示例代码: ```swift func showBottomAlert() { let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) let action1 = UIAlertAction(title: "Action 1", style: .default) { (action) in // 处理点击 Action 1 的操作 } alertController.addAction(action1) let action2 = UIAlertAction(title: "Action 2", style: .default) { (action) in // 处理点击 Action 2 的操作 } alertController.addAction(action2) let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (action) in // 处理点击取消按钮的操作 } alertController.addAction(cancelAction) if let popoverPresentationController = alertController.popoverPresentationController { popoverPresentationController.sourceView = self.view popoverPresentationController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.maxY, width: 0, height: 0) popoverPresentationController.permittedArrowDirections = [] } present(alertController, animated: true, completion: nil) } ``` 在上面的代码中,我们创建了一个UIAlertController,并添加了两个UIAlertAction作为选项,以及一个UIAlertAction作为取消按钮。然后,我们通过present方法将底部弹窗显示在屏幕上。 注意,为了将弹窗显示在底部,我们设置了popoverPresentationController的sourceView为当前的view,并且设置了sourceRect的位置在屏幕底部中间。这样就能够实现底部弹窗的效果。 希望这可以帮助你实现底部弹窗!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值