Persistent data with Core Data

I have an application which uses Core data. I can save and load data. But if I close xcode and reopen it all the data is gone. What step did I miss ?

If you want an answer, you need to be more specific about the following statement:


all the data is gone

Is the saved data in your app gone when you restart Xcode? Or are you losing data in your app's Xcode project when you restart Xcode? Are you running your app on a device or the iOS Simulator?


I can save and load data. But if I close xcode and reopen it all the data is gone. 

  • Where and how do you save data ?

     Please show code.
  • How do you know you effectively saved data ?

    Please show code where you read data.
  • How do you close app (in simulator I assume) ?

     By quitting the app in simulator ?
     Or by stopping from Xcode (then, data may not yet be recorded)

Thanks for your answer!
I am running the app on the iOS Simulator, the saved date is gone. I saved new data and closed and reopened xcode and the saved data were still there. Maybe it was just because of the update?
@Claude31 I close the app sometimes in the simulator and sometimes in xcode.
Code Block
func saveProcedures(){
        guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else{
            return
        }
        let context = appDelegate.persistentContainer.viewContext
        let entityname = "Fruits"
        guard let newEnity = NSEntityDescription.entity(forEntityName: entityname, in: context) else {
            return
        }
        let newProcedure = NSManagedObject(entity: newEnity, insertInto: context)
        let size = "5-7cm"
        let caption = "Orange"
        newProcedure.setValue(size, forKey: "size")
        newProcedure.setValue(caption, forKey: "caption")
        do {
            try context.save()
            print("saved fruit")
        } catch  {
            print(error)
        }
    }
    func loadProcedure (){
        guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else{
            return
        }
        let context = appDelegate.persistentContainer.viewContext
        let entityName = "Fruits"
        let request = NSFetchRequest<NSFetchRequestResult>(entityName: entityName)
        do {
            let results = try context.fetch(request)
            for r in results{
                if let result = r as? NSManagedObject{
                    guard let importance = result.value(forKey: "size") as? String else {
                        return
                    }
                    guard let protocol_caption = result.value(forKey: "caption") as? String else {
                        return
                    }
                    self.fruitsArray.append(caption)
                    print("loaded")
                }
            }
            print ("done")
            print (fruitsArray)
        } catch  {
            print(error)
        }
    }


Where exactly do you call saveProcedures() ?
When you quit ? In a button action ? In a completion code ?

Where exactly do you call loadProcedure()

May be you could try to call loadProcedure() at the end of save, to reload all your data structures ?

An advice: you'd better use consistent naming: all with or without ending "s"
I call saveProcedures() in the view did load of the view where I want to add data.
The same with load Procedure(), if I dont want to add more data I make save Procedure a comment in the viewdidload().
Thank you for your advice, I will try it out!
Take care, viewDidload may not be called when you expect it to be called.

And I don't understand:
Code Block
I call saveProcedures() in the view did load of the view where I want to add data. 

So you save before you add new data ?
Persistent data with Core Data
 
 
Q