4

I have to perform below operations using nsoperationqueue concurrently.

I need to perform multiple operations in background at a time like 5(Uploading files to Server) , i have to manage all queues depends up on follow scenorio

  • 1) network is 2G only perform 1 Operation,remaining 4 operations should be stop

  • 2) network is either 3G/Wifi perform all operations parallally.

How can i achieve this Using Objective-c???

Thanks in advance.

Priya
  • 199
  • 2
  • 16
  • You can try this way(Add observer for internet monitoring and do the handling of NSOperationQueue operations), https://stackoverflow.com/questions/30182128/upload-multiple-images-using-afnetworking – Jaymin Raval May 26 '17 at 05:59
  • You can use Reachability class provided by apple @ https://developer.apple.com/library/content/samplecode/Reachability/Introduction/Intro.html or check out tonymillion's custom reachability class mentioned in the link along with Native Reachability class of apple implementation also. https://stackoverflow.com/questions/17926026/objective-c-reachability-class – Jaymin Raval May 26 '17 at 06:48
  • I would discourage from using NSOperationQueue in this use case. In order to gain a minuscule benefit from using it - as opposed to other approaches, you need to create a concurrent NSOperation subclass, which is surprisingly error prone and elaborate. IMHO, you are better off just calling asynchronous functions (with a completion handler) and leverage a `DispatchGroup` (and `enter`, `leave` and `notify` respectively) to serialise your network requests. The larger the payload, the less benefit you gain from parallelising the requests, anyway. – CouchDeveloper May 26 '17 at 15:26
  • I am also facing the same issue, if you have any solution please let me know – Satheeshkumar Naidu Apr 17 '19 at 08:54

1 Answers1

0

Check your internet status and handle a operation as require

Serial Dispatch Queues

In serial queue each task waits for the previous task to finish before being executed.

When network is slow you can use it.

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

Concurrent Queue

Each downloader is considered as a task and all tasks are being performed in same time.

When network fast use it.

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

When you need to start an operation that depends on the execution of the other, you will want to use NSOperation.

You can also set a priority of operation.

addDependency for different 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)

You can also set a priority

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

You can also set a concurrent operation

queue = OperationQueue()

queue.addOperation { () -> Void in

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

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

you can also cancel and finish operation.

Harshal Valanda
  • 5,331
  • 26
  • 63