【问题标题】:WatchOS 2 not reading NSData from iPhone AppDelegateWatchOS 2 未从 iPhone AppDelegate 读取 NSData
【发布时间】:2016-07-04 11:55:42
【问题描述】:

我需要帮助或额外的观察来找出我的查询和方法没有将数据返回到 Apple Watch 的原因。我正在尝试使用在我的解析数据库中存储有PFFiles 的图像填充我的表。当我的消息需要返回 String 时,它们会通过,但当我请求返回 NSData 时不会。我不得不要求返回一个 NSData 文件,因为 Watch 不符合解析协议,我了解到。所以我试图在 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)")
})

【问题讨论】:

    标签: swift ios9 appdelegate watchos-2


    【解决方案1】:

    您正在异步块 (message.getDataInBackgroundWithBlock) 中填充您的 dataArray,然后在它有机会填充它之前将其返回到该块之外。

    【讨论】:

    • 是的,看来我是。您有我可以尝试的解决方案吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多