SHARE
TWEET

Untitled

a guest Jun 18th, 2019 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class SwiftCoreDataHelper {
  2.  
  3.     //Get saved data
  4.     func getData(forEntity: String, andSaveToArray entityArray: inout [NSManagedObject]) {
  5.  
  6.         //Get managedContext, refrence to AppDelegate, and prepare fetchRequest
  7.         guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {return}
  8.         let managedContext = appDelegate.persistentContainer.viewContext
  9.         let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "(forEntity)")
  10.  
  11.         //Get saved data
  12.         do {
  13.             entityArray = try managedContext.fetch(fetchRequest)
  14.         } catch let error as NSError {
  15.             print("Could not fetch. (error), (error.userInfo)")
  16.         }
  17.     }
  18.  
  19.     //Save data
  20.     func save(name: String, useEntity nameOfEntity: String, useArray entityArray: inout [NSManagedObject], usingKeypathName appropriateKeyPathName: String) {
  21.  
  22.         guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {return}
  23.         let managedContext = appDelegate.persistentContainer.viewContext
  24.         let entity = NSEntityDescription.entity(forEntityName: nameOfEntity, in: managedContext)!
  25.         let genericItem = NSManagedObject(entity: entity, insertInto: managedContext)
  26.         genericItem.setValue(name, forKeyPath: appropriateKeyPathName)
  27.  
  28.         do { //Save context and add to array
  29.             try managedContext.save()
  30.             entityArray.append(genericItem)
  31.         } catch let error as NSError {
  32.             print("Could not save. (error), (error.userInfo)")
  33.         }
  34.     }
  35.  
  36.     //Updating stored data
  37.     func updateData(forEntity: String, updateValueTo updatedValue: String, andSaveToArray entityArray: inout [NSManagedObject]){
  38.  
  39.         //Get managedContext, refrence to AppDelegate, and prepare fetchRequest
  40.         guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {return}
  41.         let managedContext = appDelegate.persistentContainer.viewContext
  42.         let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "(forEntity)")
  43.  
  44.         do {
  45.             let fetched = try managedContext.fetch(fetchRequest)
  46.             let objectUpdate = fetched.last! as! NSManagedObject
  47.  
  48.             //Update value
  49.             objectUpdate.setValue(updatedValue, forKey: "name")
  50.  
  51.             do { //Save context
  52.                 try managedContext.save()
  53.             }
  54.             catch {
  55.                 print(error)
  56.             }
  57.         }
  58.         catch {
  59.             print(error)
  60.         }
  61.     }
  62.  
  63.     //Delete saved data
  64.     func deleteData(forEntity: String){
  65.  
  66.         //Get managedContext, refrence to AppDelegate, and prepare fetchRequest
  67.         guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {return}
  68.         let managedContext = appDelegate.persistentContainer.viewContext
  69.         let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "(forEntity)")
  70.  
  71.         do {
  72.             let fetched = try managedContext.fetch(fetchRequest)
  73.  
  74.             //Delete object
  75.             let objectToDelete = fetched as! [NSManagedObject]
  76.             if (objectToDelete.count != 0){ //Do not delete if nothing to delete
  77.                 managedContext.delete(objectToDelete.last!)
  78.             }
  79.  
  80.             do { //Save context
  81.                 try managedContext.save()
  82.             }
  83.             catch {
  84.                 print(error)
  85.             }
  86.         }
  87.         catch {
  88.             print(error)
  89.         }
  90.       }
  91.     }
  92.      
  93. let myData = SwiftCoreDataHelper()
  94. var people: [NSManagedObject] = []
  95.  
  96. //Write data to entity previously created in xcdatamodeld file
  97. let personName = "Harry"
  98. myData.save(name: personName, useEntity: "Person", useArray: &people, usingKeypathName: "name")
  99.  
  100. //Fetch data saved in an Entity:
  101. myData.getData(forEntity: "Person", andSaveToArray: &people)
  102. Update data in an entity as follows
  103.  
  104. //Update data in an Entity
  105. let otherName = "Tom"
  106. myData.updateData(forEntity: "Person", updateValueTo: otherName, andSaveToArray: &people)
  107. Remove an entity entirely as follows
  108.  
  109. //Delete all data in an Entity
  110. myData.deleteData(forEntity: "Person")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top