Deleting from Core Data Swift 4 - swift

I have a Favorites button that when clicked will add the image of the particular character to CoreData.
#IBAction func favButtonClicked(_ sender: UIButton) {
if sender.isSelected != true {
saveFav()
}
}
func saveFav() {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let newFav = NSEntityDescription.insertNewObject(forEntityName: "Favorite", into: context)
newFav.setValue((figure?.image)!, forKey: "favFig")
do {
try context.save()
print("Saved!")
} catch {
//Process Error
}
}
My question is how do I remove that image from CoreData when clicking the button again?

In coredata every object should have Id if you want to delete it , like this
let fetchRequest: NSFetchRequest<Favorite> = Favorite.fetchRequest()
fetchRequest.predicate = Predicate.init(format: "FavoriteID==\(ID)")
do {
let objects = try context.fetch(fetchRequest)
for object in objects {
context.delete(object)
}
try context.save()
} catch _ {
// error handling
}

Related

how to print a count of the amount of core data binary images

In my swift code below the goal is to print the number of items in the a core data attribute. Right now what is being printed out is not making sense to me. What is being printed out is 413091 and it should be 1. I assume that its printing out a number of how core data binary is saved but it should be 1 photo saved 1 item stored and print just 1.
import UIKit;import CoreData
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let gwen = UIImage(named: "a.jpeg")
if let imageData = gwen.self?.pngData() {
DataBaseHelper.shareInstance.saveImage(data: imageData)
}
let arr = DataBaseHelper.shareInstance.fetchImage()
let jake = Int()
print("core data number is : ", arr[jake].img!.count)
}
}
class DataBaseHelper {
static let shareInstance = DataBaseHelper()
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
func saveImage(data: Data) {
let imageInstance = Image(context: context)
imageInstance.img = data
do {
try context.save()
print("Image is saved")
} catch {
print(error.localizedDescription)
}
}
func fetchImage() -> [Image] {
var fetchingImage = [Image]()
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Image")
do {
fetchingImage = try context.fetch(fetchRequest) as! [Image]
} catch {
print("Error while fetching the image")
}
return fetchingImage
}
}

How to Change A Label's Text From Another View Controller and Save it With Core Data

I have a Input(UITextfied) and i want the data in it to change DesA in My other ViewController
My First ViewController: Facial (where the label DesA is)
My Second ViewController: ServiceAChangeViewController (where The Input and button is)
(sorry for the weird names)
Essentially i want to write a text in my UITextField, then click a button that saves it using core data and then changes the DesA label in my First View Controller
I followed some tutorials on youtube and wrote this code
Second ViewController(ServiceAChangeViewController):
var score = Facial(nibName: nil, bundle: nil).TitleChange
var OutputLabel = Facial(nibName: nil, bundle: nil).DesA
``` #IBAction func saveData(_ sender: Any) {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let entity = NSEntityDescription.entity(forEntityName: "Entity", in: context)
let newEntity = NSManagedObject(entity: entity!, insertInto: context)
newEntity.setValue(score, forKey: "titleA")
do {
try context.save()
print("saved")
} catch {
print("Failed savign")
}
}
func getData() {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Entity")
request.returnsObjectsAsFaults = false
do {
if let result = try? context.fetch(request) as? [NSManagedObject] {
for data in result {
score = data.value(forKey: "titleA") as! String
}
} else {
print("Failed")
}
}
}
#IBAction func SubmitAnswer(_ sender: Any) {
OutputLabel?.text = score
score = String(InputA.text!)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
getData()
OutputLabel?.text = score
}```
First ViewController(Facial):
#IBOutlet var TitleA: UILabel!
var TitleChange = String()
override func viewDidAppear(_ animated: Bool) {
}
But When I run the app and enter the text, it doesn't seem to change it nor save it.
try this for saving core data entity, you also can use "didset" for variable
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
let newInfo = Entity(entity: Entity.entity(), insertInto: context)
// do something with entity
do {
try context.save() } catch let error as NSError {
print("Error... \(error), \(error)")
}

presently delete core data item in uicollectionviewcell through tag

I am trying to permanently delete a single element from a core data set. Right now the code below removes the core data item but it does not permanently delete it. Once the class is called again it shows up again like nothing happened. I want it permanently deleted.
func loadData() {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
let managedContext = appDelegate.persistentContainer.viewContext
let fetch = NSFetchRequest<Item>(entityName: "Item")
do {
self.itemName = try managedContext.fetch(fetch)
}
catch {
print("Could not load. \(error)")
}
}
override func viewDidLoad() {
super.viewDidLoad()
loadData()
}
var itemName : [Item] = []
#objc func elete(_ sender:UIButton){
itemName.remove(at: sender.tag)
collectionView.reloadData()
}

How to remove UserDefaults data swift? [duplicate]

This question already has answers here:
How to auto clear NSUserDefault values in swift? [duplicate]
(5 answers)
Closed 3 years ago.
userdaeults does not delete the data in memory, although I do delete it. Keychain does the deletion. I share the code below. It calls the handlelogout function when exiting. The recording is fine, but when I exit, it does not delete the data that I have saved.
#objc func deleteee(){
do {
try keychain.remove("chipnumbernew")
} catch let error {
print("error: \(error)")
}
}
#objc func deleteeetimer(){
UserDefaults.standard.removeObject(forKey: "timertext")
UserDefaults.standard.removeObject(forKey: "timertext2")
UserDefaults.standard.synchronize()
}
#objc func handleLogout(){
do {
deleteee()
deleteeetimer()
try Auth.auth().signOut()
let mainTableVC = LoginViewController()
let navController = UINavigationController(rootViewController: mainTableVC)
self.present(navController, animated: true, completion: {
//
})
} catch let signOutError as NSError {
print ("Giriş Yapılamadı: %#", signOutError)
}
}
override func viewDidLoad() {
super.viewDidLoad()
let token = try? keychain.getString("chipnumbernew")
chip1InfoString = token
if let strtimer = UserDefaults.standard.string(forKey: "timertext") {
print("strtimer", strtimer)
timertextNew.text = strtimer
}
if let strtimer2 = UserDefaults.standard.string(forKey: "timertext2") {
print("strtimer2", strtimer2)
timertext2New.text = strtimer2
} }
The removeObject in UserDefaults itself will clear the value. You can verify that directly in the plist. Please see this post for accessing plist.
Still, you want to clean up, you can try below code.
if let appDomain = Bundle.main.bundleIdentifier {
UserDefaults.standard.removePersistentDomain(forName: appDomain)
}

How To Delete all Entity Objects in CoreData Swift 3

So I have this code which works fine, but I want a much better one.
func deleteCoreDataObjects() {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
//where groupData is an Array of an Entity
for i in 0..<self.groupData.count {
context.delete(groupData[i])
}
(UIApplication.shared.delegate as! AppDelegate).saveContext()
}
Currently I'm deleting the objects one by one via for loop.
You can try this:
func deleteAllData(entity: String)
{
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let fetchRequest = NSFetchRequest(entityName: entity)
fetchRequest.returnsObjectsAsFaults = false
do
{
let results = try managedContext.executeFetchRequest(fetchRequest)
for managedObject in results
{
let managedObjectData:NSManagedObject = managedObject as! NSManagedObject
managedContext.deleteObject(managedObjectData)
}
} catch let error as NSError {
print("Detele all data in \(entity) error : \(error) \(error.userInfo)")
}
}
Usage:
self.deleteAllData("your_entityName")
Already seen in: https://stackoverflow.com/a/33931528/2894160
Best is delete the persistence storage and then add new one instead of looping each entity (if you want to delete all entities from coredata).
func deletePersistentStoreCoordinator () {
do {
let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("YourDatabaseName.sqlite")
try self.persistentStoreCoordinator.destroyPersistentStoreAtURL(url, withType: NSSQLiteStoreType, options: nil)
try self.persistentStoreCoordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil)
}
catch{
}
}
Here is the code for deleting records from Core Data :
//Delete user info from local db
func deleteUserInfo() {
let context = appdelegate.managedObjectContext
let coord = appdelegate.persistentStoreCoordinator
let fetchRequest = NSFetchRequest(entityName: "User")
if #available(iOS 9.0, *) {
let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)
//let predicate = NSPredicate(format: "id == %#", key)
//fetchRequest.predicate = predicate
do {
try coord.executeRequest(deleteRequest, withContext: context)
}
catch let error as NSError {
//Error handling
}
catch {}
} else {
// Fallback on earlier versions
do {
let users: NSArray = try appdelegate.managedObjectContext.executeFetchRequest(fetchRequest)
for user in users {
appdelegate.managedObjectContext.delete(user)
}
try appdelegate.managedObjectContext.save()
} catch let error as NSError {
//Error handling
}
catch {}
}
}