handleWatchKitExtensionRequest没有被调用的问题?

时间:2015-09-14 18:16:27

标签: ios json swift parse-platform apple-watch

我正在尝试更新我的监视工具包应用程序以添加从JSON文件解析的一些数据。我这样做是为了从Parse获取一些数据。

我收到错误:

  

iPhone App中的UIApplicationDelegate从未调用过reply() - [UIApplicationDelegate application:handleWatchKitExtensionRequest:reply:]

我尝试调试handleWatchKitExtensionRequest,但它没有崩溃,我添加的println因某些原因从未打印过。

这是我的代码:

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

    println("1")
    var keyStore = NSUbiquitousKeyValueStore()
    if keyStore.arrayForKey("FavouritesList") != nil {
        let tempFavourites: NSArray = keyStore.arrayForKey("FavouritesList")!
        favouritesArray = tempFavourites.mutableCopy() as! NSMutableArray
    } else {
        println("3")
    }

    println(favouritesArrayDefaults)

    if let pfqueryRequest: AnyObject = (userInfo as? [String: AnyObject])?["parkName"] {
        println("HELLO")

        let taskID = beginBackgroundUpdateTask()
        //Must start and end task to allow time for query to complete

        var name = ""
        var waitTime = ""
        var hasWaitTime = false
        var fastPass = false
        var heightRestriction = false
        var singleRider = false
        var disabledAccess = false
        var mustSee = false
        var fastPassTime = ""

        var namesArray: [String] = []
        var waitTimesArray: [String] = []
        var favouritesArray: [String] = []
        var hasWaitTimeArray: [Bool] = []
        var fastPassArray: [Bool] = []
        var heightRestrictionArray: [Bool] = []
        var singleRiderArray: [Bool] = []
        var disabledAccessArray: [Bool] = []
        var mustSeeArray: [Bool] = []
        var fastPassTimeArray: [String] = []

        var query = PFQuery(className: pfqueryRequest as! String)
        query.orderByAscending("Name")
        var objects = query.findObjects()

        if let objects = objects as? [PFObject] {
            for object in objects {
                name = (object.objectForKey("Name") as? String)!
                waitTime = (object.objectForKey("WaitTime") as? String)!
                hasWaitTime = (object.objectForKey("HasWaitTime") as? Bool)!
                fastPass = (object.objectForKey("FastPass") as? Bool)!
                heightRestriction = (object.objectForKey("HeightRestriction") as? Bool)!
                singleRider = (object.objectForKey("SingleRider") as? Bool)!
                disabledAccess = (object.objectForKey("DisabledAccess") as? Bool)!
                mustSee = (object.objectForKey("MustSee") as? Bool)!

                namesArray.append(name)
                waitTimesArray.append(waitTime)
                hasWaitTimeArray.append(hasWaitTime)
                fastPassArray.append(fastPass)
                heightRestrictionArray.append(heightRestriction)
                singleRiderArray.append(singleRider)
                disabledAccessArray.append(disabledAccess)
                mustSeeArray.append(mustSee)
            }

            var urlString = (DataManager.sharedInstance.getParkByName(pfqueryRequest as! String)?.dataSource)!

            if let url = NSURL(string: urlString) {
                if let data = NSData(contentsOfURL: url, options: .allZeros, error: nil) {
                    let json = JSON(data: data)

                    for count in 0...json.count - 1 {
                        let rideName: JSON = json[count]["name"]
                        let waitTime: JSON = json[count]["waitTime"]
                        var fastPassTime: JSON = json[count]["fastPass"]

                        if find(namesArray, rideName.string!) != nil {
                            let index = find(namesArray, rideName.string!)

                            var modifiedWaitTime = "".join(waitTime.string!.componentsSeparatedByCharactersInSet(NSCharacterSet.decimalDigitCharacterSet().invertedSet))

                            if waitTime == "Down" || waitTime == "Closed" {
                                modifiedWaitTime = "Closed"
                            }

                            if fastPassTime.string == nil {
                                fastPassTime = "Unavailable"
                            } else if fastPassTime.string == "N/A" {
                                fastPassTime = "Expired"
                            }

                            waitTimesArray.removeAtIndex(index!)
                            waitTimesArray.insert(waitTime.string!, atIndex: index!)

                            fastPassTimeArray.removeAtIndex(index!)
                            fastPassTimeArray.insert(fastPassTime.string!, atIndex: index!)
                        }
                    }
                }
            }

            for favourite in favouritesArrayDefaults {
                favouritesArray.append(favourite as! String)
            }

            println(fastPassTimeArray)
            reply(["success": true, "name": namesArray, "waitTime": waitTimesArray, "hasWaitTime": hasWaitTimeArray, "favourites": favouritesArray, "fastPass": fastPassArray, "heightRestriction": heightRestrictionArray, "singleRider": singleRiderArray, "disabledAccess": disabledAccessArray, "mustSee": mustSeeArray, "fastPassTime": fastPassTimeArray])

            endBackgroundUpdateTask(taskID)
        }
    } else if let pfqueryRequest: AnyObject = (userInfo as? [String: AnyObject])?["parkNameSubmit"] {
        let taskID = beginBackgroundUpdateTask()

        var query = PFQuery(className: pfqueryRequest as! String)
        //query.orderByAscending("Name")
        query.whereKey("Name", equalTo: userInfo!["name"]!)
        query.getFirstObjectInBackgroundWithBlock {
            (object: PFObject?, error: NSError?) -> Void in
            if error != nil {
                println("The getFirstObject request failed.")
            } else {
                if let obj = object {
                    obj.setValue(userInfo!["waitTime"], forKey: "WaitTime")
                    obj.saveInBackground()
                }
                println("Successfully retrieved the object.")
            }
        }

        reply(["success": true, "message": "Hello!"])

        endBackgroundUpdateTask(taskID)
    }
}

我为广泛的代码道歉,但我真的不知道问题出在哪里。

有人有什么想法吗?

2 个答案:

答案 0 :(得分:0)

reply()来电置于“不相关代码”部分。问题是reply()处理程序应该在任何情况下都被调用,但在你的例子中pfqueryRequestnil,处理程序永远不会被调用。

答案 1 :(得分:0)

reply()处理程序

中序列化自定义对象

此错误可能发生的另一个原因是if you are attempting to send a custom object in the reply() handler。如果您尝试这样做,iOS / watchOS将无法将您的对象序列化到回复的plist文件,这就是通信的发生方式。由于文件序列化失败,因此永远不会写出文件,并且WatchKit扩展程序认为从未调用reply(),因此错误。

您必须将这些对象分解为更基本的对象类型,或者在回复中发送之前使用NSKeyedArchiver将这些自定义对象编码为NSData,然后解码然后使用NSKeyedUnarchiver将数据解码回通信扩展端的自定义对象。

立即启动后台任务以阻止iOS暂停您的应用

您也need to start the background task as the very first thing in your handler,否则iOS可以暂停您的App的执行(即使您正在做的只是验证请求或首先创建变量)。

来自Apple's UIApplicationDelegate Protocol Reference

  

因为当你的应用程序在后台时可能会调用此方法,所以在处理完回复后,调用实现开始时的beginBackgroundTaskWithName:expirationHandler:方法和endBackgroundTask:方法并执行了回复块。启动后台任务可确保您的应用在有机会发送回复之前不会被暂停。

附加到WatchKit扩展程序时,iOS应用程序的Xcode控制台输出不可用

当您在WatchKit扩展程序上进行调试时,将不会显示print()println()NSLog()或从iOS应用程序写入控制台的类似方法。

解决此问题的一种方法是在模拟器中运行WatchKit App,然后在iOS模拟器中打开iOS App。完成后,返回Xcode并转到Debug > Attach to Process并在列表中找到您的iOS应用程序进程。 Xcode现在应该显示来自两个进程的控制台输出(但是你需要使用Xcode的Debugging / Console区域顶部的控件进行切换。

在Watch Connectivity Framework中使用较新的WatchKit API

我强烈建议使用+[WKInterfaceController openParentApplication:reply:]较旧的{{1}}。 Apple已经做了大量工作来改进watchOS应用程序与其主机iOS应用程序之间共享数据的初始机制。

我个人认为,使用原始API会遇到很多问题,当我放弃对watchOS 1.x的支持并转移到观察2.x及更高版本时,所有这些问题都消失了。