我正在尝试检查该应用程序是否已安装,如果已安装,请删除所有待处理的本地通知。我正在尝试使用此代码,但它似乎没有正确保存到我的核心数据中。
// check to see if the app has already been installed
let global = Global(context:coreDataStack.managedContext)
print("Has the app been installed before = \(global.wasLoaded)") // returns false
if(global.wasLoaded == false){
print("remove local notifications")
// remove all notifications
center.removeAllPendingNotificationRequests()
global.wasLoaded = true
coreDataStack.saveContext()
print("Has the app been installed before = \(global.wasLoaded)") // returns true
}
下次我运行该应用程序时,wasLoad bool 值仍然为 false。 我是否在此处运行了获取请求以使其正常工作?
最佳答案
每次您创建一个新的Global
托管对象时,它总是具有默认值。要实现您想要的效果,您必须获取已设置的 Global
托管对象并检查其属性。
Core Data 是这个问题的一个非常繁重的解决方案,更好的方法是像这样使用 UserDefualts
:
if UserDefaults.standard.bool(forKey: "isFirstLaunch") == false {
// Perform first launch actions
UserDefaults.standard.set(true, forKey: "isFirstLaunch")
}
// Continue with regular setup
您与 false
进行比较,因为如果从未为给定键设置值,则 bool(forKey:)
返回 false
。
关于ios - 在 coredata 加载时更新 Bool,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42012430/