首页 > 解决方案 > CoreData saving objects earlier than expected

问题描述

I have a Match class using Core Data. In my view controller I create a Match instance in a context. I'm using and modifying the match object in the controller and then have a button that save the instance in context. But if I close the application without taping the save button, the Match instance is still saved.

Here's my object declaration:

class ViewController: UIViewController {

//(...)

let match = Match(context: AppDelegate.viewContext)

   override func viewDidLoad() {
   super.viewDidLoad()
   }

//(...)
}

And here's my save fonction that is called by the button:

@IBAction func saveMatch() {   
    try? AppDelegate.viewContext.save()
    self.dismiss(animated: true, completion: nil)
}

标签: iosswiftcore-data

解决方案


查看您的其余代码以了解您调用的其他位置save()。如果您在核心数据周围使用包装器,请搜索它们调用的位置save()。您可能与applicationDidEnterBackground. 添加断点save()并查看调用它的内容。


推荐阅读