Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
func tryStoringDefaultPlanets() {
// dataStore is an instance of NSPersistentContainer subclass CoreDataStore.
guard dataStore.count(for: "Planet") == 0 else { return }
// Insert new entities into managed object context.
let names = ["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"]
let paths = ["https://en.wikipedia.org/wiki/Mercury_(planet)", "https://en.wikipedia.org/wiki/Venus", "https://en.wikipedia.org/wiki/Earth", "https://en.wikipedia.org/wiki/Mars", "https://en.wikipedia.org/wiki/Jupiter", "https://en.wikipedia.org/wiki/Saturn", "https://en.wikipedia.org/wiki/Uranus", "https://en.wikipedia.org/wiki/Neptune"]
zip(names, paths).enumerated().forEach { (offset, element) in
guard let planet = dataStore.insertNewEntity(named: "Planet") as? Planet else { return }
planet.position = Int64(offset)
planet.name = element.0
planet.url = URL(string: element.1)
}
// Save changes in the managed object context what at the moment contains added Planets.
dataStore.save()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.