Created
August 23, 2016 19:26
-
-
Save coryhymel/a84129402d272ad4e6135d7ac1e1d686 to your computer and use it in GitHub Desktop.
Recursive function to preload array of PFObjects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
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