Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save saintc0d3r/44d025e0ba9bfe0818df to your computer and use it in GitHub Desktop.
Save saintc0d3r/44d025e0ba9bfe0818df to your computer and use it in GitHub Desktop.
A sample of load & deleting a data in Swift Core Data
/**
* Cleanup auth user info from local storage
*/
func cleanup_auth_user_from_local_storage(){
let authenticated_user_info = load_auth_user_info()
if (authenticated_user_info != nil){
let managed_object_context = app_delegate.managed_object_context
managed_object_context.deleteObject(authenticated_user_info!)
app_delegate.save_context(nil)
}
}
/**
* Load authenticated user's info if it is available
*/
func load_auth_user_info() -> NSManagedObject?{
let managed_object_context = app_delegate.managed_object_context
let fetch_request = NSFetchRequest(entityName: "AuthenticatedUser")
var authenticated_user :NSManagedObject?
do{
let results = try managed_object_context.executeFetchRequest(fetch_request)
var authenticated_users = results as! [NSManagedObject]
if (authenticated_users.count > 0){
authenticated_user = authenticated_users[authenticated_users.count - 1]
}
}
catch let error as NSError {
print("Could not fetch record: \(error)")
}
return authenticated_user
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment