2016-06-23 16 views
0

我想在执行插入新数据之前清除CoreData实体中的所有数据。到目前为止,我已经尝试删除所有不允许我插入新数据的东西,即使对两个操作使用相同的上下文。有没有更好的方法来做到这一点?这是我一直在实施的代码。我所做的是从旧记录中删除实体,然后保存上下文。然后我在上下文中插入新的对象,最后,我保存它。显然,在我保存之后,当我从视图中调用存储的数据时,它将返回空白。在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) } 
} 

回答

0

context.performBlock在管理对象上下文的队列上异步调度提供的块。由于您使用此方法来安排删除操作,但同时执行插入操作(并且可能位于错误队列中),因此您将插入新对象,然后删除所有内容。

+0

好的,但是如何防止插入后发生删除? –

+0

你可以在同一个'performBlock'闭包中执行两个动作。你可以在它自己的'performBlock'闭包中执行每个动作,那些闭包将按照它们被添加到队列中的顺序执行。如果你知道你已经在正确的并发队列中,你完全可以避免使用'performBlock'。 – Jonah

+0

对不起,将两个放入performBlock关闭时不起作用 –