Created
February 3, 2016 07:05
-
-
Save saintc0d3r/44d025e0ba9bfe0818df to your computer and use it in GitHub Desktop.
A sample of load & deleting a data in Swift Core Data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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