WatchOS 2 not reading NSData from iPhone AppDelegate

南楼画角 提交于 2019-12-25 06:44:45

问题


I need help or an extra eye to find out why my query and methods aren't returning data to the Apple Watch. I am trying to populate my table with images that have PFFiles stored in my parse database. When my messages require a String back, they are going through, but not when I'm requesting NSData back. I am having to ask for an NSData file back because the Watch does not conform to parse protocols, I've learned. So I am trying to convert them on the AppDelegate side, then transfer as NSData.

Here's my AppDelegate:

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 {

        var dataArray = [NSData]()

        for object in objects! {

            if let message = object["imageFile"] as? PFFile {

                message.getDataInBackgroundWithBlock({ (data, error) -> Void in

                    if error == nil {

                        dataArray.append(data!)
                    }
                })
            }
        }

        replyHandler(["images":dataArray])
    }
})

This is how I'm retrieving it on the Watch InterfaceController side:

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

    if let imagesRequest = result as? [String:[NSData]] {

        if let dogData = imagesRequest["images"] {

            self.imageFiles = dogData

            print("Imagefiles count:\(self.imageFiles.count)")                                              
            self.logInLabel.setHidden(true)
            self.loadTableData()
        }
    }

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

回答1:


You are filling up your dataArray inside an asynchronous block (message.getDataInBackgroundWithBlock), and returning it outside of that block, before it has a chance to populate it.



来源:https://stackoverflow.com/questions/36072340/watchos-2-not-reading-nsdata-from-iphone-appdelegate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!