Ich möchte alle Daten aus einer CoreData-Entität reinigen, bevor Sie neuere Daten einfügen. Bisher löscht das, was ich versucht habe, alles, und erlaubt mir nicht, neuere Daten einzufügen, selbst wenn ich denselben Kontext für beide Operationen verwende. Gibt es einen besseren Weg, es zu tun? Hier ist der Code, den ich implementiert habe. Ich lösche die Entität aus älteren Datensätzen und speichere den Kontext. Dann füge ich neue Objekte in den Kontext ein und schließlich speichere ich es einfach. Offensichtlich, nachdem ich es gespeichert habe, wenn ich die gespeicherten Daten aus der Ansicht abrufe, wird es leer zurückgegeben.Einfügen nach dem Löschen in NSManagedObjectContext
class DS_Objectives {
let appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let dateFormatter = NSDateFormatter()
func storeObjectives(json: JSON) {
let context:NSManagedObjectContext = appDel.managedObjectContext
self.dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
self.dateFormatter.timeZone = NSTimeZone(abbreviation: "UTC")
let request : NSFetchRequest = NSFetchRequest.init(entityName: "EducationalObjective")
context.performBlock{
do{
let oldObjectives = try context.executeFetchRequest(request) as! [NSManagedObject]
for obj in oldObjectives {
context.deleteObject(obj)
}
try context.save()
} catch {
print(error)
}
}
for (_,subJson):(String, JSON) in json {
var obj : EducationalObjective? = self.findObjective(Int(subJson["IdObjetivoEducacional"].stringValue)!)
if obj == nil {
obj = NSEntityDescription.insertNewObjectForEntityForName("EducationalObjective",inManagedObjectContext: context) as? EducationalObjective
obj!.setValue(Int(subJson["IdObjetivoEducacional"].stringValue), forKey: "id")
}
obj!.setValue(Int(subJson["Numero"].stringValue), forKey: "numero")
obj!.setValue(Int(subJson["IdEspecialidad"].stringValue), forKey: "especialidad")
obj!.setValue(subJson["Descripcion"].stringValue, forKey: "descripcion")
obj!.setValue(subJson["CicloRegistro"].stringValue, forKey: "cicloRegistro")
obj!.setValue(subJson["Estado"].boolValue, forKey: "estado")
obj!.setValue(self.dateFormatter.dateFromString(subJson["updated_at"].stringValue), forKey: "updated_at")
for (_,res):(String,JSON) in json["students_results"] {
var edRes : StudentResult? = self.findStudentResult(Int(res["IdResultadoEstudiantil"].stringValue)!)
if edRes == nil {
edRes = NSEntityDescription.insertNewObjectForEntityForName("StudentResult",inManagedObjectContext: context) as? StudentResult
edRes!.setValue(Int(res["IdResultadoEstudiantil"].stringValue)!, forKey: "id")
}
edRes!.setValue(Int(res["IdResultadoEstudiantil"].stringValue), forKey: "id")
edRes!.setValue(res["Identificador"].stringValue, forKey: "identificador")
edRes!.setValue(res["Descripcion"].stringValue, forKey: "descripcion")
edRes!.setValue(res["CicloRegistro"].stringValue, forKey: "cicloRegistro")
edRes!.setValue(res["Estado"].boolValue, forKey: "estado")
edRes!.setValue(self.dateFormatter.dateFromString(res["updated_at"].stringValue)!, forKey: "updated_at")
}
}
do{ try context.save() } catch { print(error) }
}
Ok, aber wie kann ich verhindern, dass das Löschen nach dem Einfügen geschieht? –
Sie könnten beide Aktionen innerhalb der gleichen 'performBlock' Schließung. Sie können jede Aktion in ihrem eigenen 'performBlock'-Abschluss ausführen, und diese Schließung wird in der Reihenfolge ausgeführt, in der sie der Warteschlange hinzugefügt werden. Wenn Sie wissen, dass Sie sich bereits in der richtigen Nebenläufigkeits-Warteschlange befinden, können Sie "performBlock" vollständig vermeiden. – Jonah
Tut mir leid, funktioniert nicht, wenn ich die beiden in den performBlock-Verschluss setze –