There is no array sent from the parent application to view the application

I am trying to get an array of objects (which are retrieved from Parse in the application) from the parent application that will be displayed in the watch application. I tried several different things, but without 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 application:

 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([:]) } 

println I always return nil the first time I try to load, and then [:] every time. I assume this is because the request is disconnected before the application has time to load data from Parse? In addition, println , which should print "Test", is never called.

+7
ios swift watchkit apple-watch
source share
2 answers

In the extension, you pass String ( park ) to the parent application with the request key, but in the parent application you check whether userInfo["request"] NSArray or not. You should test String , as in:

 if let userInfo = userInfo, request = userInfo["request"] as? String { 
+3
source share

First add the original task to the openParentCall, here you can find more context: Watchkit Background Task

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

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

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

Now for println ("Test") not to print to the console, if you do not attach to the process using parentApplication, then println will not exit the system. If the trip data is returned empty, I would examine this function:

  DataManager.sharedInstance.loadRides(ride: String) 

make sure that it really returns the data you need. Attach to the process and place a breakpoint in each case and make sure that one of these cases is called, and also go to the loadRides function to make sure that it returns from it. As an additional note, the information you send back in the response block should be a list of properties, or the response block always fails.

0
source share

All Articles