3

我必须同时使用nsoperationqueue执行以下操作。iOS - 并发执行5个操作使用NSOperationQueue将图像上传到服务器,然后使用Objective-c中的单个任务

我需要一次像5(上传 文件服务器)执行后台多次手术,我必须管理的所有队列上后续scenorio取决于起来

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

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

我怎样才能达到这个使用Objective-C?

在此先感谢。

+0

你可以试试这个方法(添加观察员互联网的监测,做好NSOperationQueue操作的处理), https://stackoverflow.com/questions/30182128/upload-multiple-images-using-afnetworking –

+0

您可以使用通过apple @ https://developer.apple.com/library/content/samplecode/Reachability/Introduction/Intro.html 提供的可达性类或检查链接中提到的tonymillion的自定义可达性类以及Native Reachability类的Apple实现也。 https://stackoverflow.com/questions/17926026/objective-c-reachability-class –

+0

我不鼓励在这个用例中使用NSOperationQueue。为了从使用它中获得微不足道的好处 - 与其他方法相反,您需要创建一个并发的NSOperation子类,这非常容易出错并且非常复杂。恕我直言,你最好只是调用异步函数(使用完成处理程序)并利用DispatchGroup(和'enter','leave'和'notify'分别)来串行化你的网络请求。无论如何,有效载荷越大,从并行请求中获得的好处就越少。 – CouchDeveloper

回答

0

检查您的网络状况和处理操作需要

串行调度队列

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

当网络速度较慢时,您可以使用它。

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 
    }) 
} 

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

+0

好吧,我会转换它@Priya –

相关问题