BoltsFramework / Bolts-Swift

Bolts is a collection of low-level libraries designed to make developing mobile apps easier.
Other
1.32k stars 124 forks source link

using Bolts with Parse Queries #52

Open ghost opened 7 years ago

ghost commented 7 years ago
func findAsync(query:PFQuery<PFObject>) -> BFTask<[PFObject]> {
        let task = BFTaskCompletionSource<[PFObject]>()

        query.findObjectsInBackground { (results:[PFObject]?, error:Error?) in
            if (error == nil) {
                task.setResult(results!)
            } else {
                task.setError(error!)
            }
        }

        return task.task
    }

unfortunately, the above code throws the error

Type [PFObject] does not conform to protocol for 'AnyObject'

however, I need to type as [PFObject] to put the results of the query as the result to the task.

how do I accomplish this in Swift 3?

i was able to get past the error by using NSArray, however the result for the function never seems to get set, waituntilfinished just hangs.

func findAsync(query:PFQuery<PFObject>) -> BFTask<NSArray> {
        let task = BFTaskCompletionSource<[NSArray]>()

        query.findObjectsInBackground { (results:[PFObject]?, error:Error?) in
            if (error == nil) {
                task.setResult(NSArray.init(results!))
                print(task.task.result!) // at this point i can verify that the result appears to be the desired
            } else {
                task.setError(error!)
            }
        }

        return task.task
    }

but calling the function with self.findAsync() with a query i know returns data, just simply returns nil or an uncompleted task