No array sent from parent app to Watch app

I am trying to receive an array of objects (that are retrieved from Parse within the app) from a parent application to be displayed in the watch application. I have been trying a few different things but with no success.

Here is my code in the extension:

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

    var parkPassed = context as! String
    openParentAppWithPark(parkPassed)        
}

private func openParentAppWithPark(park: String) {
    WKInterfaceController.openParentApplication(["request": park], reply: { (reply, error) -> Void in
        println(reply)
    })
}

And the code in the parent app:

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

        if let userInfo = userInfo, request = userInfo["request"] as? NSArray {
            if request == "Park 1" {
                DataManager.sharedInstance.loadRides("Park 1")
            } else if request == "Park 2" {
                DataManager.sharedInstance.loadRides("Park 2")
            } else if request == "Park 3" {
                DataManager.sharedInstance.loadRides("Park 3")
            } else {
               DataManager.sharedInstance.loadRides("Park 4")
            }

            let rides = DataManager.sharedInstance.rideArray
            println("Rides: (rides)")

            reply(["rideData": rides])
            return
        }
        reply([:])
}

The println I have always returns nil the first time I try to load, and then [:] every other time. I assume this is because the request is timing out before the app has time to load the data from Parse? Also, the println that is supposed to print "Test" is never called.


In the extension, you're passing a String ( park ) to the parent application via the request key, but in the parent application, you're testing whether userInfo["request"] is an NSArray or not. You should be testing for a String , as in:

if let userInfo = userInfo, request = userInfo["request"] as? String {

First add a background task assertion to the openParentCall, you can find more context on that here: Background Task Watchkit

    let backgroundTask = application.beginBackgroundTaskWithExpirationHandler { NSLog("TIME UP")}
   ///do code
   reply(callback)
   //
   application.endBackgroundTask(backgroundId)

Now for the actual handleWatchKitExtensionRequest call I would change the first line to

  if let request = userInfo["request"] as? String {

Now for the println("Test") not printing to console if you don't attach to process with the parentApplication then the println will not log out. If the ride data is returning empty then I would inspect this function:

  DataManager.sharedInstance.loadRides(ride: String)

make sure it is actually returning the correct data you need. Attach to process and place a breakpoint on each case and check that one of the cases is being called and also jump into the loadRides function to make sure it is coming back out from it. As a side note, the information you send back in the reply block has to be a property list or the reply block will always fail.

链接地址: http://www.djcxy.com/p/27772.html

上一篇: 集中式与分散式移动应用门户

下一篇: 没有数组从父应用程序发送到Watch应用程序