Swift Remove Object from Realm

后端 未结 6 1149
庸人自扰
庸人自扰 2021-02-04 04:06

I have Realm Object that save list from the JSON Response. But now i need to remove the object if the object is not on the list again from JSON. How i do that? This is my init f

6条回答
  •  梦如初夏
    2021-02-04 04:51

    do {
            let realm = try Realm()
    
            if let obj = realm.objects(Items.self).filter("id = %@", newId).first {
    
                //Delete must be perform in a write transaction
    
                try! realm.write {
                     realm.delete(obj)
                 }
            }
    
        } catch let error {
            print("error - \(error.localizedDescription)")
        }
    

提交回复
热议问题