title: How to save and fetch one Object from Database in Swift?
body: <p>I have a method which will save Object into Database. As per logic, whenever there is internet, it will save object by downloading from server.The method is as follows.</p>
  
  <pre><code>func saveConfiguration (config : ConfigDao){
  
          let entity = NSEntityDescription.entityForName("AppConfig", inManagedObjectContext:self.del.managedObjectContext!)
  
          let configurationContext = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: self.del.managedObjectContext!) as AppConfig
  
          configurationContext.categoriesVer = Int32(config.categoriesVer)
          configurationContext.fireBallIp = config.fireBallIP
          configurationContext.fireBallPort = Int32(config.fireBallPort)
          configurationContext.isAppManagerAvailable = config.isAppManagerAvailable
          configurationContext.isFireBallAvailable = config.isFireballAvailable
          configurationContext.timePerQuestion = config.timePerQuestion
  
  
      }
  </code></pre>
  
  <p>Issue is that this adds all objects, and doesn't replace it, so my first query is</p>
  
  <p>"How to add only one object in DB and replace when next Object comes?"</p>
  
  <p>I also want to retrieve the same object, the only one object, normally in array, I do fetch last index, but how can I save only one and fetch same in DB.</p>
  
  <pre><code>func fetchAppConfig() -&gt; AppConfig {
  
          var fetchRequest =  NSFetchRequest (entityName: "AppConfig")
  
          var error : NSError?
          let fetchResults = del.managedObjectContext?.executeFetchRequest(fetchRequest, error: &amp;error) as [NSManagedObject]
  
          if error != nil {
              println ("Error \(error)")
          }
  
          return  fetchResults
      }
  </code></pre>
  
  <p>Thanks.</p>