【问题标题】:iOS - Concurrency performing 5 operations uploading images to server using NSOperationQueue followed by single Task in Objective-ciOS - 并发执行 5 个操作,使用 NSOperationQueue 将图像上传到服务器,然后是 Objective-c 中的单个任务
【发布时间】:2017-10-26 21:54:08
【问题描述】:

我必须同时使用 nsoperationqueue 执行以下操作。

我需要同时在后台执行多项操作,例如 5(Uploading 文件到服务器),我必须管理所有队列取决于跟随场景

  • 1) 网络为 2G 仅执行 1 次操作,其余 4 次操作 应该停止

  • 2) 网络是 3G/Wifi 并行执行所有操作。

我如何使用 Objective-c 来实现这一点???

提前致谢。

【问题讨论】:

  • 你可以试试这种方式(添加观察者进行互联网监控并处理 NSOperationQueue 操作),stackoverflow.com/questions/30182128/…
  • 您可以使用苹果@developer.apple.com/library/content/samplecode/Reachability/… 提供的可达性类或查看链接中提到的tonymillion 的自定义可达性类以及苹果实现的本机可达性类。 stackoverflow.com/questions/17926026/…
  • 我不鼓励在这个用例中使用 NSOperationQueue。为了从使用它中获得微不足道的好处 - 与其他方法相反,您需要创建一个并发的 NSOperation 子类,该子类令人惊讶地容易出错且复杂。恕我直言,您最好只调用异步函数(使用完成处理程序)并利用DispatchGroup(分别为enterleavenotify)来序列化您的网络请求。无论如何,有效负载越大,您从并行请求中获得的好处就越少。
  • 我也遇到了同样的问题,如果你有什么解决办法请告诉我

标签: ios objective-c objective-c-blocks nsoperationqueue reachability


【解决方案1】:

检查您的互联网状态并根据需要处理操作

串行调度队列

在串行队列中,每个任务在执行之前等待前一个任务完成。

当网络慢时,你可以使用它。

let serialQueue = dispatch_queue_create("com.imagesQueue", DISPATCH_QUEUE_SERIAL) 

dispatch_async(serialQueue) { () -> Void in
    let img1 = Downloader .downloadImageWithURL(imageURLs[0])
    dispatch_async(dispatch_get_main_queue(), {
        self.imageView1.image = img1
   })      
}

dispatch_async(serialQueue) { () -> Void in
   let img2 = Downloader.downloadImageWithURL(imageURLs[1])
   dispatch_async(dispatch_get_main_queue(), {
       self.imageView2.image = img2
   })
}

并发队列

每个下载器都被视为一个任务,所有任务都在同一时间执行。

网络快用的时候。

let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
dispatch_async(queue) { () -> Void in

        let img1 = Downloader.downloadImageWithURL(imageURLs[0])
        dispatch_async(dispatch_get_main_queue(), {

            self.imageView1.image = img1
        })

}

dispatch_async(queue) { () -> Void in

        let img2 = Downloader.downloadImageWithURL(imageURLs[1])

        dispatch_async(dispatch_get_main_queue(), {

            self.imageView2.image = img2
        })

}

NSOPration

当你需要启动一个依赖于另一个执行的操作时,你会想要使用 NSOperation。

您还可以设置操作的优先级。

addDependency 为不同的operation

queue = OperationQueue()

let operation1 = BlockOperation(block: {
     let img1 = Downloader.downloadImageWithURL(url: imageURLs[0])
     OperationQueue.main.addOperation({
          self.imgView1.image = img1
     })
})

// completionBlock for operation        
operation1.completionBlock = {
    print("Operation 1 completed")
}

// Add Operation into queue
queue.addOperation(operation1)

let operation2 = BlockOperation(block: {
     let img2 = Downloader.downloadImageWithURL(url: imageURLs[1])
     OperationQueue.main.addOperation({
          self.imgView2.image = img2
     })
})

// Operation 2 are depend on operation 1. when operation 1 completed after operation 2 is execute. 
operation2.addDependency(operation1)

queue.addOperation(operation2)

你也可以设置优先级

public enum NSOperationQueuePriority : Int {
    case VeryLow
    case Low
    case Normal
    case High
    case VeryHigh
}

也可以设置并发操作

queue = OperationQueue()

queue.addOperation { () -> Void in

    let img1 = Downloader.downloadImageWithURL(url: imageURLs[0])

    OperationQueue.main.addOperation({
        self.imgView1.image = img1
    })
 }

您也可以取消并完成操作。

【讨论】:

  • 好的,我会转换它@Priya
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-26
  • 2018-01-07
相关资源
最近更新 更多