IOS - data is saved once in Core Data

I am trying to save a ton of data in master data. I could save all the data in the main data, but it makes no sense to duplicate the data inside the main data, because the function will be executed once every time the project is built. The approach I'm thinking of now is an empty entity record before I add a new entry to it. Just want to know what is another way to make sure that data is saved only once?

func persistCurrencyData() {
    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

    let managedContext = appDelegate.managedObjectContext

    let currencyEntity = NSEntityDescription.entityForName("Currency", inManagedObjectContext: managedContext)


    let countryEntity = NSEntityDescription.entityForName("Country", inManagedObjectContext: managedContext)

    for currency in currencies {
      let currencyObject = CurrencyCore(entity: currencyEntity!, insertIntoManagedObjectContext: managedContext)

      currencyObject.setValue(currency.code, forKeyPath: "code")
      currencyObject.setValue(currency.name, forKeyPath: "name")

      for country in currency.country! {
        let countryObject = CountryCore(entity: countryEntity!, insertIntoManagedObjectContext: managedContext)

        countryObject.setValue(country.name, forKeyPath: "name")
        countryObject.setValue(country.code, forKeyPath: "code")
        countryObject.setValue(country.continent, forKeyPath: "continent")
        countryObject.setValue(country.continentCode, forKeyPath: "continentCode")

        countryObject.currency = currencyObject
      }
    }

    do {
      try managedContext.save()
    } catch let error as NSError  {
      print("Could not save \(error), \(error.userInfo)")
    }
}

Update:

@Wain , . , , GUID JSON. , JSON , GUID NSUserDefault . JSON , , GUID GUID. , JSON . , .

func requestCurrenciesData() {
    getCurrenciesDataFromFileWithSuccess { (data) -> Void in
      var json: [String: AnyObject]

      do {
        json = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions()) as! [String: AnyObject]

        guard let currencyData = CurrencyData(json: json) else {
          print("Error initializing object")
          return
        }

        self.currencies = currencyData.currency!

        // Checking the JSON id is same as previous read
        let defaults = NSUserDefaults.standardUserDefaults()
        if let id = defaults.stringForKey("id")
        {
          if id == currencyData.id {
            let notification = NSNotification(name: "CurrenciesDataUpdate", object: nil)
            NSNotificationCenter.defaultCenter().postNotification(notification)

            return
          }
        }

        self.persistCurrencyDataForId(currencyData.id)

        let notification = NSNotification(name: "CurrenciesDataUpdate", object: nil)
        NSNotificationCenter.defaultCenter().postNotification(notification)
      } catch {
        print(error)
        return
      }
   }
}

func persistCurrencyDataForId(id: String) {
    // Save the new JSON id into NSUserDefaults
    let defaults = NSUserDefaults.standardUserDefaults()
    defaults.setObject(id, forKey: "id")

    // Delete all the records in existing table
    deleteRecordsFromEntity("Country")
    deleteRecordsFromEntity("Currency")

    // Insert the new records
    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let managedContext = appDelegate.managedObjectContext

    let currencyEntity = NSEntityDescription.entityForName("Currency", inManagedObjectContext: managedContext)
    let countryEntity = NSEntityDescription.entityForName("Country", inManagedObjectContext: managedContext)

    for currency in currencies {
      let currencyObject = CurrencyCore(entity: currencyEntity!, insertIntoManagedObjectContext: managedContext)

      currencyObject.setValue(currency.code, forKeyPath: "code")
      currencyObject.setValue(currency.name, forKeyPath: "name")

      for country in currency.country! {
        let countryObject = CountryCore(entity: countryEntity!, insertIntoManagedObjectContext: managedContext)

        countryObject.setValue(country.name, forKeyPath: "name")
        countryObject.setValue(country.code, forKeyPath: "code")
        countryObject.setValue(country.continent, forKeyPath: "continent")
        countryObject.setValue(country.continentCode, forKeyPath: "continentCode")

        countryObject.currency = currencyObject
      }
    }

    do {
      try managedContext.save()
    } catch let error as NSError  {
      print("Could not save \(error), \(error.userInfo)")
    }
  }

func deleteRecordsFromEntity(entityName: String) {
    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let managedContext = appDelegate.managedObjectContext
    let coordinator = appDelegate.persistentStoreCoordinator

    let fetchRequest = NSFetchRequest(entityName: entityName)
    let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)

    do {
      try coordinator.executeRequest(deleteRequest, withContext: managedContext)
    } catch let error as NSError {
      print(error)
    }
 }
+4
1

: " , ". , - , , - , , - . currencyEntity countForFetchRequest:error:, for currency in currencies {. , .

, JSON...

+1

Source: https://habr.com/ru/post/1629481/


All Articles