Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@coryhymel
Created August 23, 2016 19:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coryhymel/a84129402d272ad4e6135d7ac1e1d686 to your computer and use it in GitHub Desktop.
Save coryhymel/a84129402d272ad4e6135d7ac1e1d686 to your computer and use it in GitHub Desktop.
Recursive function to preload array of PFObjects
/**
Recusivly pre load an array of PFObjects.
- parameter items: The items to preload.
- parameter completion: Invoked once all loading in the background has been completed. If an error occured it will be passed back otherwise `success` will be `true` with a `nil` error parameter.
*/
func preloadItems(items: [PFObject], completion: (success: Bool, error: NSError?) -> ()) {
func recursiveLoad(index: Int) {
if index == items.count {
completion(success: true, error: nil)
return
}
items[index].fetchInBackgroundWithBlock({ (object, error) in
if let err = error {
completion(success: false, error: err)
return
}
else {
recursiveLoad(index + 1)
}
})
}
recursiveLoad(0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment