Reputation: 9466
I'm trying to check to see if the app has already been installed and if so, delete all pending local notifications. I'm trying to use this code, but it doesn't seem to be saving correctly to my Core Data.
// 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
}
The next time I run the app, the wasLoad bool comes back as false still. Do I have run a fetch request here to make this work properly?
Upvotes: 0
Views: 62
Reputation: 5332
Each time you're making a new Global
managed object, so it always has the default value. To achieve what you want, you'd have to fetch the already set Global
managed object and check it's properties.
Core Data is a very heavy solution to this problem, a better one would be to use UserDefualts
like this:
if UserDefaults.standard.bool(forKey: "isFirstLaunch") == false {
// Perform first launch actions
UserDefaults.standard.set(true, forKey: "isFirstLaunch")
}
// Continue with regular setup
You compare to false
because bool(forKey:)
returns false
if the value was never set for a given key.
Upvotes: 3