Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • How to download data Using NSOperations and NSOperationQueue

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2.67k
    Comment on it

    Hi Readers,

    It is sometimes required to download files or images from server into local. We can perform these tasks by NSOperation and NSOperationQueue efficiently. The basic idea is to create each download operation separately and add these operations into our queue which maintain these operation execution. Lets look at an example of downloading images from URLs. All you need to install Alamofire pod into your project. You can find many links to see how to install it. After installing please follow these steps:

    Create a different class as DownloadOperation in which you define your obejct for downloading as PhotoRecord, OperationQueue as PendingOperations and NSOperation as Download like: 

    enum PhotoRecordState {
        case New, Downloaded, Filtered, Failed
    class PhotoRecord {
        let name:String
        let url:NSURL
        var image = UIImage(named: "Placeholder")
        var imageview : UIImageView
        var state = PhotoRecordState.New
        init(name:String, url:NSURL, imageview:UIImageView) {
   = name
            self.url = url
            self.imageview = imageview
    class PendingOperations {
        lazy var downloadsInProgress = [Int:NSOperation]()
        lazy var downloadQueue:NSOperationQueue = {
            var queue = NSOperationQueue()
   = "Download queue"
            queue.maxConcurrentOperationCount = 1
            return queue
    class Download: NSOperation {
        let photoRecord: PhotoRecord
        init(photoRecord: PhotoRecord) {
            self.photoRecord = photoRecord
        func filepath(filename:String) -> String {
            let documentsDirectoryURL = try! NSFileManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true)
            // create a name for your image
            let fileURL = documentsDirectoryURL.URLByAppendingPathComponent(filename)
            return fileURL.path!
            //if !NSFileManager.defaultManager().fileExistsAtPath(fileURL.path!) {
        override func main() {
            if self.cancelled {
          var localPath: NSURL?
          let response =,
                destination: { (temporaryURL, response) in
                    let directoryURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
                    let pathComponent = response.suggestedFilename
                    localPath = directoryURL.URLByAppendingPathComponent(pathComponent!)
                    return localPath!
          }).progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
            // This closure is NOT called on the main queue for performance
            // reasons. To update your ui, dispatch to the main queue.
            dispatch_async(dispatch_get_main_queue()) {
                print("Total bytes read on main queue: \(totalBytesRead)")
            if let error = response.error {
                print("Failed with error: \(error)")
            } else {
                print("Downloaded file successfully")
            dispatch_async(dispatch_get_main_queue(), {
                self.photoRecord.imageview.image = UIImage(contentsOfFile: (localPath?.path)!)
            if self.cancelled {

    In above code we have we create an enum as PhotoRecordState which can be used to get state of the downloading process. PhotoRecord class has init method to set downloading URL and corresponding imageview. You can see class PendingOperation which is used to create operation queue object, later we add operations into this queue. Download is a subclass of NSOperation, when it comes to start state, main() called after it. So we override main() function in which we put code for downloading provided by Alamofire. All images will be downloaded into your local directory or document directory which can be used later.

    Now we all set for adding operation into our operation queue. Add following code into your controller where you need to perform operations like:

    let task1: PhotoRecord = PhotoRecord(name: "test1", url: NSURL(string:"")!,imageview: iamge1)
            let task2: PhotoRecord = PhotoRecord(name: "test2", url: NSURL(string:"")!,imageview: iamge2)
            let task3: PhotoRecord = PhotoRecord(name: "test3", url: NSURL(string:"")!,imageview: iamge3)
            let task4: PhotoRecord = PhotoRecord(name: "test4", url: NSURL(string:"")!,imageview: iamge4)
            let task5: PhotoRecord = PhotoRecord(name: "test5", url: NSURL(string:"")!,imageview: iamge5)
            let task6: PhotoRecord = PhotoRecord(name: "test6", url: NSURL(string:"")!,imageview: iamge6)
            let task7: PhotoRecord = PhotoRecord(name: "test7", url: NSURL(string:"")!,imageview: iamge7)
            let task8: PhotoRecord = PhotoRecord(name: "test8", url: NSURL(string:"")!,imageview: iamge8)
            let downloadtask1 = Download(photoRecord: task1)
            let downloadtask2 = Download(photoRecord: task2)
            let downloadtask3 = Download(photoRecord: task3)
            let downloadtask4 = Download(photoRecord: task4)
            let downloadtask5 = Download(photoRecord: task5)
            let downloadtask6 = Download(photoRecord: task6)
            let downloadtask7 = Download(photoRecord: task7)
            let downloadtask8 = Download(photoRecord: task8)
            var nsoperationque = [NSOperation]()
            for (var i = nsoperationque.count-1 ; i > 0 ; i -= 1 )
                var x = i - 1
            pendingOperations.downloadQueue.addOperations(nsoperationque, waitUntilFinished: false)

    These are some sample images url which we used for downloading. You can choose these according to your requirement. Now you can easily do things with downloaded data. Same approach will be followed to download either pdf, doc, image, audio and video file also. You can change URL of file accordingly.

    Hope it helps. Thanks for reading.


 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: