Kodeco Forums

NSOperation and NSOperationQueue Tutorial in Swift

Learn how to keep your apps feeling quick and responsive, and perform operations in the background in this tutorial on NSOperation and NSOperationQueue.


This is a companion discussion topic for the original entry at http://www.raywenderlich.com/76341/use-nsoperation-nsoperationqueue-swift
2 Likes

Hi, Thanks a lot for this tutorial. I am always confused while using NSOperation and NSOperationQueue, however this helped me to understand these topics. But it seems that this is a bit old code and doesnt support Xcode 7.2.1 and Swift 2.0. Because if I download the complete project it gives me 3 errors.
I followed all the steps mentioned in this tutorials (Using NSURLSession) but ended up with few questions.

  1. It takes more than 70+ MB memory
  2. My system starts making noise if I run this application in simulator more than 10 min
  3. The images are downloaded once and then are grayed out But all are happening row by row (in order by row…like image for first row, second row, thrid row and so on…I dont know if its a coincident or so) I was expecting the image for any row can be downloaded at any time but not in sequence

Any help to understand me on these points would be highly appreciated.

1 Like

An updated version of this tutorial is in the works, hopefully that will solve your problems!

http://www.raywenderlich.com/76341/use-nsoperation-nsoperationqueue-swift

Is this the latest one ?

You mind posting the latest tutorial url here (if I am missing that). Thanks a lot.

It’s not done yet! We’re still editing it :]

Hi, I found that when I click the origin Objective-C tutorial, it redirect to this tutorial.
Is that any way I can read the Objective-C tutorial?

Hi. I hope that it will help you. How To Use NSOperations and NSOperationQueues - Ray Wenderlich

Hey guys! Many thanks for the wonderful tutorial! I just finished an education in a coding bootcamp for iOS Engineering and we were only taught using NSNotificationCenter to get these sorts of scheduled things done, so I’m so grateful for a tutorial on how to use NSOperation and NSOperationQueues to get things done.

Now, my current issue: I’m unable to run the app due to build errors coming from the fetchPhotoDetails method. There are two errors:

  1. “let datasourceDictionary = …” → Extra argument ‘error’ in call

  2. “for(key : AnyObject,value : AnyObject) in …” → Definition conflicts with previous value

I’ve checked my typing many times and even copy/pasted your code into my editor but unfortunately the issues persist. After a little research, I see that error handling is different in Swift 2, which is likely the issue with the first problem. I’ve replaced that code with the following:

do {
    let datasourceDictionary = try NSPropertyListSerialization.propertyListWithData(data!, options: Int(NSPropertyListMutabilityOptions.Immutable.rawValue), format: nil) as! NSDictionary
                
     for(key : AnyObject,value : AnyObject) in datasourceDictionary {
          let name = key as? String
          let url = NSURL(string:value as? String ?? "")
          if name != nil && url != nil {
                let photoRecord = PhotoRecord(name:name!, url:url!)
                self.photos.append(photoRecord)
           }
      }
            } catch let error as NSError {
                print(error.localizedDescription)
            }

The issue I have now (other than the second issue which I’m not sure how to fix) is that now the error is:

"Cannot convert value of type ‘Int’ to expected argument type ‘NSPropertyListReadOptions’ (aka ‘NSPropertyListMutabilityOptions’)

Sorry this is so long! I’m trying to put an app on the app store to make it easier to get a junior dev job and I think that this tutorial could strengthen my code repertoire in a big way, and I figured more detail is better than not enough. Thanks!!!

I just passed “NSPropertyListReadOptions.Immutable” instead of trying to pass it through as an Int:

`
do {
let datasourceDictionary = try NSPropertyListSerialization.propertyListWithData(data!, options: NSPropertyListReadOptions.Immutable, format: nil) as! NSDictionary

for (key, value) in datasourceDictionary {
    let name = key as? String
    let url = NSURL(string: value as? String ?? "")

    if name != nil && url != nil {
        let photoRecord = PhotoRecord(name: name!, url: url!)
        self.photos.append(photoRecord)
    }
}

self.tableView.reloadData()

}
`

This is a really interesting article. I am going to apply a similar pattern to a sample app I am working on that displays weather data for a selected point on a map. I do have a question about the lines that assign to the queues variable.

lazy var filtrationQueue:NSOperationQueue = { … }

I had never seen this pattern where there is a curly brace on the right side of the assignment. I think I see what it is doing, but never saw this before. What is this kind of initialization called and where can I find documentation on it? It’s kind of like a anonymous function or closure that creates and sets up a new object and then returns it? I assume all of that code could have also been called on the filtrationQueue variable itself after it was created with a default init?

EDIT: Looking at it now it was almost like an if let {} pattern, but now I see the lazy keyword there. I figure this pattern has something to do with that. The clode in the { } will be run later on when the object is lazily created? Pretty cool…

Thanks
John

Look up “lazy initialization with closure”. You’re right- the closure is executed the first time the variable is accessed and the result of the closure is assigned to the value.

It’s useful for deferring creation and also for cases where you need to use self to set up a variable, but can’t do that in init because self isn’t available yet.

According to Apple:

sendAsynchronousRequest(_:queue:completionHandler:)’ was deprecated in iOS 9.0: Use [NSURLSession dataTaskWithRequest:completionHandler:]

Many thanks for the great tutorial on this important topic.

I’m a bit confused about how you have avoided potential read/write contentions on some of the variables - for instance it seems as if photoDetails.state and photoDetails.image could be read by the TableView at the same time as they are being written by ImageDownloader… I’d be really grateful if you could point out the aspect of the design that prevents this happening - or is it just not a problem?

Hi Richard, any news on the progress of this tutorial’s update? Thanks!

I followed this tutorial code and ported it to Swift 3, and published in Github. If anybody is interested, please go here: GitHub - lakshgandikota/ClassicPhotos: Sample app from raywenderlich.com ported to Swift 3

2 Likes

:+1: thanks for doing that @gandikota!

1 Like

I do not understand why the indicator is shown on the right, or image the left. I have not seen anywhere else in the code indicator positioning. Someone can tell why that goes? Thank you!

Because this is how default apple UITableViewCell built. Image on the left. Accessary view on the right.

For more information check documentation:

Thank you for your superb tutorial! Can you please post a solution for using dependencies?
Thanks again.

This code is help me a lot and also I am following you for that type of tutorials you have best experienced in this industry like Essay Writer UK Writing Services Would Love to Have on Board who also have wide experienced in the industry of education consultancy and last year when I was in my high school, help helps me in my assignment and make me tension free and I got A 