【问题标题】:Insertion after deletion in NSManagedObjectContext在 NSManagedObjectContext 中删除后插入
【发布时间】:2016-06-23 22:11:18
【问题描述】:

我想在插入新数据之前清除 CoreData 实体中的所有数据。到目前为止,我尝试删除的所有内容都不允许我插入更新的数据,即使对两个操作使用相同的上下文也是如此。有更好的方法吗?这是我一直在实现的代码。我所做的是从旧记录中删除实体,然后保存上下文。然后我在上下文中插入新对象,最后,我只是保存它。显然,保存后,当我从视图中调用存储的数据时,它返回空。

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) }
}

【问题讨论】:

    标签: ios swift core-data nsmanagedobjectcontext


    【解决方案1】:

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

    【讨论】:

    • 好的,但是如何防止插入后发生删除?
    • 您可以在同一个 performBlock 闭包中执行这两个操作。您可以在它自己的 performBlock 闭包中执行每个操作,这些闭包将按照它们添加到队列中的顺序执行。如果你知道你已经在正确的并发队列中,你可以完全避免performBlock
    • 对不起,将两者放在 performBlock 闭包中时不起作用
    猜你喜欢
    • 1970-01-01
    • 2020-05-04
    • 2012-08-17
    • 1970-01-01
    • 1970-01-01
    • 2021-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多