将获取结果分配给变量时,Swift NSFetchRequest内存泄漏

自从升级到Swift 5.2以来,在将获取结果分配给AppDelegate中的局部变量时,我遇到了新的内存泄漏。我使用Xcode Instruments(11.2)查看泄漏。下面是曾经没有内存泄漏但不再存在的代码。


@NSApplicationmain
class AppDelegate: NSObject,NSApplicationDelegate {

    var clipboardFormats: [CoreclipboardFormat]?

    func applicationDidFinishlaunching(_ aNotification: Notification) {

        if let context = (NSApplication.shared.delegate as? AppDelegate)?.persistentContainer.viewContext {
            let fetchy = CoreclipboardFormat.fetchRequest() as NSFetchRequest<CoreclipboardFormat>
            do {
                let res = try context.fetch(fetchy)
                self.clipboardFormats = res // leaks
            } catch {}
        }
    }

   ...

}

在下面您可以看到内存泄漏... Instruments Profile

my_winter13292 回答:将获取结果分配给变量时,Swift NSFetchRequest内存泄漏

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3120326.html

大家都在问