Swift 3 Core Data Delete Object

后端 未结 8 2151
日久生厌
日久生厌 2020-11-29 02:41

Unfortunately the new Core Data semantics make me crazy. My previous question had a clean code that didn\'t work because of incorrect auto generation of header files. Now I

8条回答
  •  死守一世寂寞
    2020-11-29 03:08

    The trick here, it is save context after deleting your objects.

    let fetchRequest: NSFetchRequest = Profile.fetchRequest()
    fetchRequest.predicate = Predicate.init(format: "profileID==\(withID)")
    let objects = try! context.fetch(fetchRequest)
    for obj in objects {
        context.delete(obj)
    }
    
    do {
        try context.save() // <- remember to put this :)
    } catch {
        // Do something... fatalerror
    }
    

    I hope this can help someone.

提交回复
热议问题