WatchOS 2 不从 iPhone AppDelegate 读取 NSData [英] WatchOS 2 not reading NSData from iPhone AppDelegate

查看:23
本文介绍了WatchOS 2 不从 iPhone AppDelegate 读取 NSData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助或额外的眼睛来找出为什么我的查询和方法没有将数据返回到 Apple Watch.我试图用在我的解析数据库中存储了 PFFiles 的图像填充我的表.当我的消息需要返回字符串时,它们会通过,但在我请求返回 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)")
})

推荐答案

您正在异步块 (message.getDataInBackgroundWithBlock) 中填充 dataArray,并返回它在该块之外,在它有机会填充它之前.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆