I'm trying to add this instance of serie to a country via a core-data relation. The only problem is that my NSOrderedSet gets very big and this method of adding gets inefficient because I have to load the whole NSOrderedSetthat already exists (way to big) into memory. Than duplicate it into a editable copy, add the new instance. Than set the large NSOrderedSet the new OrderdSet for that country and save it. (This is by the way a simple solution that I found on the internet)
Is it possible to do this without loading the whole NSOrderdSet associated with that specific country? So only add the new instance without loading the complete list?
I'm sorry for my vague explanation. I'm new to the concept of Core-Data.
for data in rawData {
//Setup let
let seriesName = data.1.array![1].string!
if let seriesID = Int(data.1.array![0].string!){
if let itemCount = Int(data.1.array![2].string!){
privateMOC.performBlockAndWait{
let serie = NSEntityDescription.insertNewObjectForEntityForName("Series", inManagedObjectContext: privateMOC) as! Series
serie.seriesID = seriesID
serie.seriesName = seriesName
serie.itemCount = itemCount
let series = currentCountry.series!.mutableCopy() as! NSMutableOrderedSet
series.addObject(serie)
currentCountry.series = series.copy() as? NSOrderedSet
do {
try privateMOC.save()
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
}
}
}
}
You don't need to do any of this:
let series = currentCountry.series!.mutableCopy() as! NSMutableOrderedSet
series.addObject(serie)
currentCountry.series = series.copy() as? NSOrderedSet
because managed objects have mutators that do it for you:
currentCountry.addSeriesObject(serie)
Is it possible to do this without loading the whole NSOrderdSet associated with that specific country?
Yes, that's exactly what will happen if you use the provided accessor. BTW, if you have a set of objects to add, you can do it all at once:
currentCountry.addSeries(someSetOfSeries)