title: Setting default values for CoreData
body: <p>I am working on my first project with CoreData and I have the data model setup.</p>
  <p>I have a tableview with an Add button that triggers an alert and lets the user enter a string:</p>
  <pre><code>func handleAdd() {
      
      let alert = UIAlertController(title: &quot;New Company&quot;, message: &quot;Add a new company&quot;, preferredStyle: .alert)
      
      let saveAction = UIAlertAction(title: &quot;Save&quot;, style: .default) {
          [unowned self] action in
          
          guard let textField = alert.textFields?.first,
              let newCompany = textField.text else {
                  return
          }
          
          self.save(name: newCompany)
          self.tableView.reloadData()
      }
  
      let cancelAction = UIAlertAction(title: &quot;Cancel&quot;, style: .default)
      
      alert.addTextField()
      alert.addAction(saveAction)
      alert.addAction(cancelAction)
      
      present(alert, animated: true)
  }
  </code></pre>
  <p>The string is added to an NSManagedObject array:</p>
  <pre><code>func save(name: String) {
      
      guard let appDelegate =
          UIApplication.shared.delegate as? AppDelegate else {
              return
      }
      
      // 1
      let managedContext =
          appDelegate.persistentContainer.viewContext
      
      // 2
      let entity =
          NSEntityDescription.entity(forEntityName: &quot;Company&quot;,
                                     in: managedContext)!
      
      let company = NSManagedObject(entity: entity,
                                   insertInto: managedContext)
      
      // 3
      company.setValue(name, forKeyPath: &quot;name&quot;)
  
      
      
      // 4
      do {
          try managedContext.save()
          companies.append(company)
      } catch let error as NSError {
          print(&quot;Could not save. \(error), \(error.userInfo)&quot;)
      }
  }
  </code></pre>
  <p>All looking good so far except I want the app to launch with 5 cells already in place, then let the user add/delete as they wish. How do I set the 5 defaults that I want the app to launch with? I tried adding the below code to viewWillAppear</p>
  <pre><code>    let entity =
          NSEntityDescription.entity(forEntityName: &quot;Company&quot;,
                                     in: managedContext)!
      
      let company = NSManagedObject(entity: entity,
                                    insertInto: managedContext)
      
      company.setValue(&quot;Apple&quot;, forKeyPath: &quot;name&quot;)
      company.setValue(&quot;Google&quot;, forKeyPath: &quot;name&quot;)
      company.setValue(&quot;Facebook&quot;, forKeyPath: &quot;name&quot;)
      company.setValue(&quot;Tesla&quot;, forKeyPath: &quot;name&quot;)
      company.setValue(&quot;Twitter&quot;, forKeyPath: &quot;name&quot;)
  
      self.save(name: &quot;Apple&quot;)
      self.save(name: &quot;Google&quot;)
      self.save(name: &quot;Facebook&quot;)
      self.save(name: &quot;Tesla&quot;)
      self.save(name: &quot;Twitter&quot;)
  </code></pre>
  <p>At load time they all appear. Then when I navigate off the page and come back, they are added again. I know this is because they are in viewWillAppear and get called every time the page loads.</p>
  <p>Apple's docs (Core Data Programming Guide) are a bit hard to swallow as a beginner but they do say</p>
  <blockquote>
  <p>For small datasets, create the managed objects directly in code.</p>
  <p>If you are using iOS or creating an application for OS X that is not document-based, you can add a check on application launch to determine whether a file exists at the location you specify for the application’s store. If it doesn't, you need to import the data.</p>
  </blockquote>
  <p>I believe that second snippet is what I'm looking for in my case (correct me if I'm wrong), however I'm not sure how to implement it, so any help on how to set these defaults so they are added once (on load) and only once would be much appreciated!</p>
  <p>Here are the initializers if they are of interest</p>
  <pre><code>var companies = [NSManagedObject]()
  var container: NSPersistentContainer!
  let managedContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
  let companyEntity = &quot;Company&quot;
  </code></pre>
  <hr />
  <p>EDIT: code added to app delegate</p>
  <pre><code>    let vc = ViewController()
      let entity = NSEntityDescription.entity(forEntityName: &quot;Company&quot;, in: managedContext)!
      let company = NSManagedObject(entity: entity,insertInto: managedContext)
      let userDefaults = UserDefaults.standard
      userDefaults.set(true, forKey: &quot;firstRun&quot;)
      
      if userDefaults.valueForKey(&quot;firstRun&quot;) == true {
  
          company.setValue(&quot;Apple&quot;, forKeyPath: &quot;name&quot;)
          company.setValue(&quot;Google&quot;, forKeyPath: &quot;name&quot;)
          company.setValue(&quot;Facebook&quot;, forKeyPath: &quot;name&quot;)
          company.setValue(&quot;Tesla&quot;, forKeyPath: &quot;name&quot;)
          company.setValue(&quot;Twitter&quot;, forKeyPath: &quot;name&quot;)
          
          vc.save(name: &quot;Apple&quot;)
          vc.save(name: &quot;Google&quot;)
          vc.save(name: &quot;Facebook&quot;)
          vc.save(name: &quot;Tesla&quot;)
          vc.save(name: &quot;Twitter&quot;)
          
          userDefaults.set(false, forKey: &quot;firstRun&quot;)
      }
  </code></pre>