我需要帮助或额外的关注来找出为什么我的查询和方法没有将数据返回到 Apple Watch。我正在尝试使用解析数据库中存储有 PFFiles
的图像来填充我的表。当我的消息需要返回 String 时,它们就会通过,但当我请求返回 NSData 时则不会。据我所知,我不得不要求返回 NSData 文件,因为 watch 不符合解析协议(protocol)。所以我试图在 AppDelegate 端转换它们,然后作为 NSData 传输。
这是我的 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 端检索它的方式:
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)")
})
最佳答案
您正在异步 block (message.getDataInBackgroundWithBlock
) 内填充 dataArray
,并在有机会填充该 block 之前将其返回到该 block 之外。
关于swift - WatchOS 2 不从 iPhone AppDelegate 读取 NSData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36072340/