Search code examples
iosswiftparse-platformwatchkit

Nested PFQuery's in HandleWatchKitExtensionRequest


I'm trying to nest 2 PFQuery's inside HandleWatchKitExtensionRequest so that I can get the data passed back to my Watch Extension in my reply. In the code below the first Query (for a PFUser matching the given userName) returns, but I cannot get the second (for a list of the User's people) to return. Is there some limitation I'm missing on making multiple queries within the same block? Is my background task timing out before this long running request can return?

*I edited my code so that nameList is not an optional, but still no return value. While the same block (nested findObjectsInBackGroundBlocks) executes and returns perfectly in my IOS app.

Extending the time of the delay of background task didn't help.

 func application(application: UIApplication, handleWatchKitExtensionRequest userInfo: [NSObject : AnyObject]?, reply: (([NSObject : AnyObject]!) -> Void)!) {


    var dictionary = userInfo! as NSDictionary


    if let currentUserName: AnyObject = dictionary.objectForKey("currentUserName") {
        if let currentUserNameAsString = currentUserName as? String {


            // Bogus task for keeping app going
            var bogusTask = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler { () -> Void in
            }
            // End bogus task after 2 seconds
            delay(2.0, closure: { () -> () in
                UIApplication.sharedApplication().endBackgroundTask(bogusTask)
            })

            // look up current user logged in on phone from name passed from Watch
            let query = PFUser.query()
            query!.whereKey("username", equalTo: currentUserNameAsString)
            query!.findObjectsInBackgroundWithBlock { (objects, error) -> Void in

                if let user = objects?.first as? PFUser {

                    var testName: String?
                    var nameList:  String = []
                    let query = PFQuery(className: Person.parseClassName())
                    query.whereKey("user", equalTo: user)

                    query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
                        if let people = objects as? [Person] {
                            for person in people {
                                if let name = person.name {
                                nameList.append(name)
                                    testName = nameList.first
                                }
                            }
                        }
                        testName = ("test")
                    }






                let testDict = ["success" : testName!]
                reply(testDict)

                 UIApplication.sharedApplication().endBackgroundTask(bogusTask) // End the bogusTask in case the work was faster than 2 seconds
                    }
                }


                }

            }




}

// Utility function for delay
func delay(delay:Double, closure:()->()) {
    dispatch_after(
        dispatch_time(
            DISPATCH_TIME_NOW,
            Int64(delay * Double(NSEC_PER_SEC))
        ),
        dispatch_get_main_queue(),
        closure
    )
}

Solution

  • It looks like you are calling "reply(testDict)" outside of your nested query.

    Since the query is asynchronous the "reply(testDict)" is probably being called before the nested query is finished executing.