WatchOS 2无法从iPhone AppDelegate读取NSData [英] WatchOS 2 not reading NSData from iPhone AppDelegate
问题描述
我需要帮助或更多的眼睛来找出为什么我的查询和方法没有将数据返回给Apple Watch的原因.我正在尝试使用在解析数据库中存储了PFFiles
的图像填充表.当我的消息要求返回String时,它们会通过,但是当我请求NSData返回时却不会.据我了解,我不得不要求退回NSData文件,因为Watch不符合解析协议.因此,我尝试在AppDelegate端对其进行转换,然后将其转换为NSData.
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.
这是我的AppDelegate:
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])
}
})
这是我在Watch InterfaceController端检索它的方式:
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)")
})
推荐答案
您正在将dataArray
填充到异步块(message.getDataInBackgroundWithBlock
)中,并在该块有机会填充之前将其返回到该块之外它.
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.
这篇关于WatchOS 2无法从iPhone AppDelegate读取NSData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!