Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Alamofire 使用多个线程时Alamorefile上载图像超时_Alamofire_Rx Swift - Fatal编程技术网

Alamofire 使用多个线程时Alamorefile上载图像超时

Alamofire 使用多个线程时Alamorefile上载图像超时,alamofire,rx-swift,Alamofire,Rx Swift,我正在使用moya通过使用OperationQueue来控制maxConcurrentOperationCount来上传许多图像。假设我有100张图片,每次上传5张图片。Alamorefire超时设置为10秒 上传一张图片非常快,无需触发超时。但当我使用下面的方法上传100幅图像时,即使使用多线程,也会触发超时。为什么? 谢谢大家! queue = OperationQueue() queue.maxConcurrentOperationCount = 5

我正在使用moya通过使用OperationQueue来控制maxConcurrentOperationCount来上传许多图像。假设我有100张图片,每次上传5张图片。Alamorefire超时设置为10秒

上传一张图片非常快,无需触发超时。但当我使用下面的方法上传100幅图像时,即使使用多线程,也会触发超时。为什么? 谢谢大家!

queue = OperationQueue()
            queue.maxConcurrentOperationCount = 5
            var i = 0
            for image in photos {
                autoreleasepool {
                    let operation:BlockOperation = BlockOperation(block: {
                        [weak self] in
                        guard let strongSelf = self else {return}
                        print("hyl cur thread %@", Thread.current)
                        strongSelf.uploadImage(image)
                        return
                    })
                    i += 1
                    queue.addOperation(operation)
                }
            }

基本上,这是在允许运行
URLSessionTask
s之前创建和恢复它们的问题。Alamofire加剧了这种情况,它在创建相应的
*请求
值后立即创建
URLSessionTask
s。增加用于图像下载的
URLRequest
s的超时时间可能会有所帮助。一个更彻底的解决方案是立即停止创建Alamofire请求,而只在前几个请求完成时创建它们。这将防止相关任务超时

private func uploadImage(_ image: UIImage) {
        AladdinProvider.rx.request(.upload(access_token: UserInfo.instance.access_token!, file_name: "file_name", data: image)).asObservable().mapJSON().mapObject(type: AlbumDatas.self).subscribe(onNext: {
            [weak self] result in
            guard let strongSelf = self else {return}
            // TODO success

            }, onError: {
                 error in
                print(\(error))

        }).disposed(by: disposeBag)
    }