bme003
bme003

Reputation: 131

iPhone AppDelegate only returning first object of an array to Watch OS 2 iOS 9

I've just learned how to create Watch apps from a Watch OS 1 tutorial and most of it translates to OS 2 except for communication from the Watch and iPhone. Here are the steps I'm taking:

1 - Watch InterfaceController.swift - create a WCSession, sendMessage to iPhone if user is logged in or not. If yes ask for table data, if no show a label to go to phone.

2 - I correctly get whether user is logged in or not, then I ask for an array of dog names to populate my table.

override func awakeWithContext(context: AnyObject?) {
    super.awakeWithContext(context)

    if (WCSession.isSupported()) {
        session = WCSession.defaultSession()
        session.delegate = self
        session.activateSession()
        print("Activated Session ----")

        if session.reachable == true {


            session.sendMessage(["content" : "isLoggedIn"], replyHandler: { (result) -> Void in

                if let response = result["isLoggedIn"] as? String {

                    if response == "true" {

                        userLoggedOn = true


                        self.session.sendMessage(["content":"getDogNames"], replyHandler: { (result) -> Void in

                            if let nameRequest = result as? [String:[String]] {

                                if let dogData = nameRequest["names"] {

                                    if dogData.count > 0 {

                                        dogNames = dogData
                                        self.logInLabel.setHidden(true)
                                        self.loadTableData()
                                    }

                                    else {

                                        self.logInLabel.setHidden(false)
                                        self.logInLabel.setText("Add dog profiles from iPhone")
                                    }

                                    print(dogData)

                                }
                            }

                            }, errorHandler: { (error) -> Void in
                                print("got names error")
                        })

                    } else {

                        userLoggedOn = false
                        self.logInLabel.setText("Create a profile from  iPhone")
                    }
                }
                }, errorHandler: { (error) -> Void in
                    print("error:------ /(error)")
            })
        }
    }   
}

3 - iPhone AppDelegate.swift - In my result, I'm only getting the first object of an array queried from my parse.com database.

@available(iOS 9.0, *)
func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) {

    if let watchMessage = message as? [String : String] {

        if let content = watchMessage["content"] {

            if content == "isLoggedIn" {

                if PFUser.currentUser() != nil {

                    replyHandler(["isLoggedIn":"true"])

                }

                else {

                    replyHandler(["isLoggedIn":"false"])
                }

            } else if content == "getDogNames" {

                let query = PFQuery(className: "dogInfo")
                query.whereKey("userId", equalTo: (PFUser.currentUser()?.objectId)!)
                query.orderByAscending("dogName")
                query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in

                    if error == nil && objects!.count > 0 {

                        for object in objects! {

                            var dogsArray = [String]()

                            if let message = object["dogName"] as? String {

                                dogsArray.append(message)
                            }

                            replyHandler(["names":dogsArray])
                        }
                    }
                })   
            }
        }
    }
}

I've tested this by manually putting in var dogsArray = ["dog1","dog2","dog3"] and it results in all 3 items coming back. I know for a fact there are 7 items that I should be getting but only getting the first one. Very strange, any help is much appreciated!

Upvotes: 0

Views: 46

Answers (1)

ccjensen
ccjensen

Reputation: 4656

There seems to be a logical error in your code, try changing it to this:

query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if error == nil && objects!.count > 0 {
    var dogsArray = [String]() // moved this
    for object in objects! {
        if let message = object["dogName"] as? String {
            dogsArray.append(message)
        }
    }
    replyHandler(["names":dogsArray]) // and this
}

})

where I've moved the call to the replyHandler and the creation of the dogsArray outside of the for loop so that it accumulates all the dog names, and then afterwards calls the replyHandler.

Upvotes: 1

Related Questions