SHARE
TWEET

Untitled

a guest Oct 19th, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  func getContext () -> NSManagedObjectContext {
  2.         let appDelegate = UIApplication.shared.delegate as! AppDelegate
  3.         return appDelegate.persistentContainer.viewContext
  4.     }
  5.    
  6.     func storeDishesInfo (itemName: String, itemPrice: String, itemType: String) {
  7.         let context = getContext()
  8.        
  9.         //retrieve the entity that we just created
  10.         let entity =  NSEntityDescription.entity(forEntityName: "Dishes", in: context)
  11.        
  12.         let transc = NSManagedObject(entity: entity!, insertInto: context)
  13.        
  14.         //set the entity values
  15.         transc.setValue(itemName, forKey: "itemName")
  16.         transc.setValue(itemPrice, forKey: "itemPrice")
  17.         transc.setValue(itemType, forKey: "itemType")
  18.        
  19.         //save the object
  20.         do {
  21.             try context.save()
  22.             print("saved!")
  23.         } catch let error as NSError  {
  24.             print("Could not save \(error), \(error.userInfo)")
  25.         } catch {
  26.            
  27.         }
  28.     }
  29.    
  30.     func getDishesInfo () -> String {
  31.         var info = ""
  32.        
  33.         //create a fetch request, telling it about the entity
  34.         let fetchRequest: NSFetchRequest<Dishes> = Dishes.fetchRequest()
  35.        
  36.         do {
  37.             //go get the results
  38.             let searchResults = try getContext().fetch(fetchRequest)
  39.            
  40.             //I like to check the size of the returned results!
  41.             print ("num of results = \(searchResults.count)")
  42.            
  43.             //You need to convert to NSManagedObject to use 'for' loops
  44.             for trans in searchResults as [NSManagedObject] {
  45.                 let itemName = trans.value(forKey: "itemName") as! String
  46.                 let itemPrice = String(trans.value(forKey: "itemPrice") as! Double)
  47.                 let itemType = trans.value(forKey: "itemType") as! String
  48.                 info = info + itemName + " " + itemPrice + ", " + itemType + "\n"
  49.             }
  50.         } catch {
  51.             print("Error with request: \(error)")
  52.         }
  53.         return info;
  54.     }
  55.    
  56.     func removeRecords () {
  57.         let context = getContext()
  58.         // delete everything in the table Dishes
  59.         let deleteFetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Dishes")
  60.         let deleteRequest = NSBatchDeleteRequest(fetchRequest: deleteFetch)
  61.        
  62.         do {
  63.             try context.execute(deleteRequest)
  64.             try context.save()
  65.         } catch {
  66.             print ("There was an error")
  67.         }
  68.     }
  69.    
  70. }
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
 
Top