问题
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