gpt4 book ai didi

arrays - 警告 : A long-running operation is being executed on the main thread. 尝试一下!获取数据()。 swift

转载 作者:行者123 更新时间:2023-11-30 10:06:52 24 4
gpt4 key购买 nike

我知道错误来自try! converyPFFile.getData()

如果我不想在后台获取数据,想知道如何消除错误,因为如果我在后台获取数据,则图像无法附加到数组中。谢谢。

        query.findObjectsInBackgroundWithBlock { (objects: [PFObject]?, error: NSError?) -> Void in
for object in objects! {
self.senderArray.append(object.objectForKey("sender") as! String)
self.messageArray.append(object.objectForKey("message") as? String)
if object.objectForKey("photo") != nil {
if let converyPFFile = object.objectForKey("photo") as? PFFile{
let temp = try! converyPFFile.getData()
let image = UIImage(data: temp)
self.photoArray.append(image)
}
} else {
self.photoArray.append(nil)
}
}
// create a chat interface
if self.senderArray[i] == self.userName {
if self.messageArray[i] != nil {
// using scroll view
// create username label
// create message text label
} else {
// using scroll view
// create username label
// create message image
messageImage.image = self.photoArray[i]
}
....

最佳答案

我发现这个警告来自 Parse。您可以使用 getDataInBackgroundWithBlock 来克服它,然后附加到该 block 内的数组。因此,您的代码变得类似于(未经测试,我认为我的大括号配对错误):

    query.findObjectsInBackgroundWithBlock { (objects: [PFObject]?, error: NSError?) -> Void in
for object in objects! {
self.senderArray.append(object.objectForKey("sender") as! String)
self.messageArray.append(object.objectForKey("message") as? String)
if object.objectForKey("photo") != nil {
if let converyPFFile = object.objectForKey("photo") as? PFFile{
converyPFFile.getDataInBackgroundWithBlock { (data: NSData!, error: NSError!) -> Void in
let image = UIImage(data: data)
self.photoArray.append(image)
}
} else {
self.photoArray.append(nil)
}
}
// create a chat interface
if self.senderArray[i] == self.userName {
if self.messageArray[i] != nil {
// create username label
// create message text label
} else {
// create username label
// create message image
messageImage.image = self.photoArray[i]
}
....

关于arrays - 警告 : A long-running operation is being executed on the main thread. 尝试一下!获取数据()。 swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35441474/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com