gpt4 book ai didi

Swift UIApplication.delegate 只能在主线程中使用

转载 作者:行者123 更新时间:2023-11-28 07:57:34 26 4
gpt4 key购买 nike

当我尝试设置 appDelegate 和上下文变量时,Xcode 报错,这是使用 CoreData 所必需的。

本质上,我想将我的 Vision/CoreML 图像分类请求的结果存储到 Core Data 数据库中以供离线分析。

看到了与此相关的线程,并尝试了很多。问题并没有消失,现在(我不知道!)CoreData 在几百条记录后保存错误。我希望完全消除此问题将解决错误问题,或者我可以稍后进行故障排除...

这是专门用于调试的,数据分析完成后可能不需要 CoreData。

尝试将变量声明放在 ViewController 类的顶部,并附加“!”据我所知,我稍后会设置它们。尝试将 2 行放在 DispatchQueue.main.async 闭包中。

尝试将这两行包装在“DispatchQueue.main.async({ })”行中,但之后我无法再引用“newItem”行中的上下文。包裹整个部分也不起作用,可能是因为 CoreData 无法查看/访问图像请求中的数据(?)

代码:

func processCameraBuffer(sampleBuffer: CMSampleBuffer) {

let coreMLModel = Inceptionv3()

if let model = try? VNCoreMLModel(for: coreMLModel.model) {
let request = VNCoreMLRequest(model: model, completionHandler: { (request, error) in
if let results = request.results as? [VNClassificationObservation] {
var counter = 1

for classification in results {

let timestamp = NSDate().timeIntervalSince1970

// Purple Error is here
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext

let newItem = NSEntityDescription.insertNewObject(forEntityName: "Predictions", into: context)
newItem.setValue(classification.confidence, forKey: "confidence")
newItem.setValue(classification.identifier, forKey: "identifier")
newItem.setValue(counter, forKey: "index")
newItem.setValue(timestamp, forKey: "timestamp")
newItem.setValue("inceptionv3", forKey: "mlmodel")
print("counter: \(counter) \(classification.identifier)")
do {
try context.save()
} catch {
print("CoreData Save error")
print(error.localizedDescription)
}
counter += 1
}
}
})

if let pixelBuffer: CVPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) {
let handler = VNImageRequestHandler(cvPixelBuffer: pixelBuffer, options: [:])
do {
try handler.perform([request])
} catch {
print(error.localizedDescription)
}
}
}
}

最佳答案

这是一个烦人的警告;然而,它似乎是有原因的,因为 iOS 想要避免死锁(???)。

使用 Swift 3,这是我发现的一种管理 Core Data + AppDelegate 的安全方法(您的里程可能会有所不同):

DispatchQueue.main.async {
if let app = UIApplication.shared.delegate as? AppDelegate {
app.managedObjectContext.performAndWait {
// .. Core Data work here
// .. context.save()
} // app.managedObjectContext.performAndWait
} // if let app = UIApplication.shared.delegate as? AppDelegate
} // DispatchQueue.main.async

希望这对您有所帮助!

关于Swift UIApplication.delegate 只能在主线程中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47613353/

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