自从升级到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