Code Monkey home page Code Monkey logo

swift-multithreading-lab-ios-0616's Introduction

Multithreading in Swift

Multithreading may seem esoteric and dense, but it's super important. With so many apps in the App Store, the user interface (UI) and user experience (UX) of your app will have to be as close to flawless as possible for it to stand out. You can hire a team of designers to create the most beautiful graphics for your photo filtering app, but it won't do any good if the whole app freezes without warning when a user attempts to process an image!

Bluto on Ice

To that end we can use multithreading to run heavy processes off the main thread of a device, thereby ensuring the user interface doesn't stutter and the user experience is maintained.

In this lab you will fix a broken photo filter app which hangs when the user tries to apply an "antique" filter.

Goals

  • When the Antique button is tapped, an activity indicator should be presented and animated to show the user some processing is going on. This activity indicator should stop when the image is filtered.
  • We also want to allow the user to continue to pan and zoom the image while the filtration occurs in the background.

Instructions

Show an activity indicator

  • Let's add an activity indicator to our filtering app. Instead of doing this in Interface Builder, we'll add it programmatically. In the ImageViewController class, add a property called antiqueButton of type UIActivityIndicatorView!.
  • Next, after setupViews in viewDidLoad, we'll create the actual view and set it up. Paste in the following lines of code:
activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .WhiteLarge)
activityIndicator.color = UIColor.cyanColor()
activityIndicator.center = view.center
  • Here we instantiate a UIActivityIndicatorView object for the property we just created with the stile of .WhiteLarge. We then tint the indicator so it fits the theme of our app, and finally align it with the main view.
  • If you run the app now and hit Antique, you'll see the activity indicator still doesn't appear. We're not done yet! The UIActivityIndicatorView has been created, but hasn't been added to the superview. Do this by inserting view.addSubview(activityIndicator) into your viewDidLoad. This adds our activityIndicator to the view controller's view.
  • The last step we need to make our activityIndicator visible is to call it to start. Use the following lines to start and stop the indicator, respectively:
activityIndicator.startAnimating()  // Presents and starts the activity indicator
activityIndicator.stopAnimating()   // Hides and stops the activity indicator
  • Add just the startAnimating line to your viewDidLoad and run your app. You should see a blue spinning activity indicator! We don't want the indicator to start as soon as the app opens, though, and we don't want it to keep going after the image has been filtered. Let's move this start function call to the antiqueButtonTapped function ahead of the call to filterImage.
  • Uh oh! If you run the app now and tap Antique you'll see that the activity indicator doesn't appear until after the image has finished filtering. Try to add the stopAnimating call on the activityIndicator to the completion block for the call to filterImage. Now the indicator never shows!
  • It looks like the filtering process is blocking the indicator, so we'll have to move the filtering to a different thread.

Allow for user interaction during filtering

  • Create a new NSOperationQueue in antiqueButtonTapped and set its qualityOfService to .UserInitiated. Next, move into this block the call to the filterImage function and its completion block which prints based on the result.
  • Inside the completion block of the call to filterImage, add a new operation block on the mainQueue which will wrap the previous contents of the completion block. This ensures that when filterImage has completed and returned, the activity indicator's status will be updated on the main thread.
  • We still need to update the imageView in the main thread. Look for the line in filterImage where we print the line "Setting final result". Add a mainQueue operation block and insert the two lines where we set the imageView's image to finalResult and return true to the completion block.
  • Now everything should work as expected and the user will never be left hanging for a filtering process! ๐Ÿ˜‰

Advanced

Play around with other filters and see what you can create! Here's a list of all available CIFilters to get you started.

View Multithreading in Swift on Learn.co and start learning to code for free.

swift-multithreading-lab-ios-0616's People

Contributors

ianrahman avatar johann avatar imryan avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.