Applications open until spots fill up - Apply now.

We've made some great progress throughout the last steps! We have a query that builds a users timeline (even though that's a lonely place right now with only one user that uses the app...). We are downloading photos from posts on that timeline and displaying them nicely.

Now that all of this is working - let's work on cleaning up the code a little bit. We'll start with the timeline query in this step and then improve the the image download code in the next one.

Adding a Parse Helper Class

Oftentimes, the most difficult part about tidying up is finding a good place for every object at hand. It's been mentioned a few times throughout the tutorial already, but as a reminder: We want to avoid putting all of our code into view controllers.

Currently, the entire timeline request is written within the TimelineViewController. Later on we're going to add a bunch more such requests, which could quickly result in a TimelineViewController with well over 1000 lines.

So where is the right place to put our query code?

If in doubt, create a new file (don't take this too literally...).

We're going to introduce a ParseHelper.swift file that will contain most of our code that's responsible for talking to our Parse server. That way we can avoid bloated view controllers.

Start by adding the new ParseHelper.swift file to the Helpers group in your Xcode project: image

Often when we create new classes, we use the Cocoa Touch Class templates that are built into Xcode. These are nice, because when subclassing certain UIKit classes (UIViewController or UITableViewCell for example) the new class will be created with a lot of boilerplate code written for us.

In this case, the ParseHelper class we're going to create is not a subclass of anything - so using the Cocoa Touch Class creation wizard won't help us out. Instead, use the Swift File template, which will create an empty Swift file.

Now we're going to tidy up the TimelineViewController by moving the timeline query into our new ParseHelper.swift file.

Fill the ParseHelper.swift file with the following content:

import Foundation
import Parse

// 1
class ParseHelper {

  // 2
  static func timelineRequestForCurrentUser(completionBlock: PFArrayResultBlock) {
    let followingQuery = PFQuery(className: "Follow")
    followingQuery.whereKey("fromUser", equalTo:PFUser.currentUser()!)

    let postsFromFollowedUsers = Post.query()
    postsFromFollowedUsers!.whereKey("user", matchesKey: "toUser", inQuery: followingQuery)

    let postsFromThisUser = Post.query()
    postsFromThisUser!.whereKey("user", equalTo: PFUser.currentUser()!)

    let query = PFQuery.orQueryWithSubqueries([postsFromFollowedUsers!, postsFromThisUser!])

    // 3

  1. We are going to wrap all of our helper methods into a class called ParseHelper. We only do this so that all of the functions are bundled under the ParseHelper namespace. That makes the code easier to read in some cases. To call the timeline request function you call ParseHelper.timelineRequestforUser... instead of simply timelineRequestforUser. That way you always know exactly in which file you can find the methods that are being called.
  2. We mark this method as static. This means we can call it without having to create an instance of ParseHelper - you should do that for all helper methods. This method has only one parameter, completionBlock: the callback block that should be called once the query has completed. The type of this parameter is PFArrayResultBlock. That's the default type for all of the callbacks in the Parse framework. By taking this callback as a parameter, we can call any Parse method and return the result of the method to that completionBlock - you'll see how we use that in 3.
  3. The entire body of this method is unchanged, it's the exact timeline query that we've built within the TimelineViewController. The only difference is the last line of the method. Instead of providing a closure and handling the results of the query within this method, we hand off the results to the closure that has been handed to use through the completionBlock parameter. This means whoever calls the timelineRequestForCurrentUser method will be able to handle the result returned from the query!

Now we can tidy up the TimelineViewController and replace the query in there with a call to our newly crafted helper method.

Update the viewDidAppear method in TimelineViewController to look as following:

override func viewDidAppear(animated: Bool) {

  ParseHelper.timelineRequestForCurrentUser {
    (result: [AnyObject]?, error: NSError?) -> Void in
      self.posts = result as? [Post] ?? []

      for post in self.posts {
        let data = post.imageFile?.getData()
        post.image = UIImage(data: data!, scale:1.0)


Suddenly the viewDidAppear method is much, much shorter. All we need to do is call ParseHelper.timelineRequestforCurrentUser and hand the method a completion block (which we are doing using the trailing closure syntax).

The content of the completion block remains exactly the same; we store the posts, download all of the images, and finally update the table view.

You can test this version of the app and it should behave exactly as before; however, our code is now better organized.


In this section we've set up a corner stone for the sound structure of our app. The next section will be really exciting - we will use lazy loading to download our images and you will learn about bindings!


If you have feedback on this tutorial or find any mistakes, please open issues on the GitHub Repository.

Summer academy

An iOS Development Summer Course

Design, code and launch your own app. Locations across the USA and Asia

Find your location

Product College

A computer science college

Graduate into a successful career as a founder or software engineer.

Learn more