Saving multiple PFFiles in parallel in Swift3

121 views Asked by At

After upgrading a Swift 2 project to Swift 3, I struggle with the BFTask/PFFile APIs of Parse.

I want to save a number of Parse files in parallel. So far I used BFTask.forCompletionOfAllTasks which got passed an array of BFTasks which were generated by PFFile.saveInBackground. Before migrating to Swift 3, this worked great:

let files: [PFFile] = ...
let tasks: [BFTask] = files.map { $0.saveInBackground() }
BFTask(forCompletionOfAllTasks: tasks)
  .continueWithSuccessBlock { ... }

However, with Swift 3 this does not work anymore: BFTask.forCompletionOfAllTasks now returns a BFTask<NSNumber> which is not accepted by forCompletionOfAllTasks which demands an array of <BFTask<AnyObject>.

Any help on this is appreciated. How can I convert BFTask<NSNumber> to <BFTask<AnyObject> -- or is there another way to solve this issue?

1

There are 1 answers

0
dr_barto On

Solved by force-casting the image-saving tasks to BFTask<AnyObject>:

let tasks = files.map { $0.saveInBackground() as! BFTask<AnyObject> }

Now tasks can be passed to BFTask<AnyObject>.forCompletionOfAllTasksWithResults.