Swift Remove Object from Realm

后端 未结 6 1095
庸人自扰
庸人自扰 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:45

    What you can do is assign a primary key to the object you are inserting, and when receiving a new parsed JSON you verify if that key already exists or not before adding it.

    class Items: Object {
        dynamic var id = 0
        dynamic var name = ""
    
        override class func primaryKey() -> String {
            return "id"
        }
    }
    

    When inserting new objects first query the Realm database to verify if it exists.

    let repeatedItem = realm.objects(Items.self).filter("id = 'newId'")
    
    if !repeatedItem {
       // Insert it
    }
    
    0 讨论(0)
  • 2021-02-04 04:51

    imagine your Items object has an id property, and you want to remove the old values not included in the new set, either you could delete everything with just

    let result = realm.objects(Items.self)
    realm.delete(result)
    

    and then add all items again to the realm, or you could also query every item not included in the new set

    let items = [Items]() // fill in your items values
    // then just grab the ids of the items with
    let ids = items.map { $0.id }
    
    // query all objects where the id in not included
    let objectsToDelete = realm.objects(Items.self).filter("NOT id IN %@", ids)
    
    // and then just remove the set with
    realm.delete(objectsToDelete)
    
    0 讨论(0)
  • 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)")
        }
    
    0 讨论(0)
  • 2021-02-04 04:58

    I will get crash error if I delete like top vote answer.

    Terminating app due to uncaught exception 'RLMException', reason: 'Can only add, remove, or create objects in a Realm in a write transaction - call beginWriteTransaction on an RLMRealm instance first.'
    

    Delete in a write transaction:

    let items = realm.objects(Items.self)
    try! realm!.write {
        realm!.delete(items)
    }
    
    0 讨论(0)
  • 2021-02-04 05:02
    func realmDeleteAllClassObjects() {
        do {
            let realm = try Realm()
    
            let objects = realm.objects(SomeClass.self)
    
            try! realm.write {
                realm.delete(objects)
            }
        } catch let error as NSError {
            // handle error
            print("error - \(error.localizedDescription)")
        }
    }
    

    // if you want to delete one object

    func realmDelete(code: String) {
    
        do {
            let realm = try Realm()
    
            let object = realm.objects(SomeClass.self).filter("code = %@", code).first
    
            try! realm.write {
                if let obj = object {
                    realm.delete(obj)
                }
            }
        } catch let error as NSError {
            // handle error
            print("error - \(error.localizedDescription)")
        }
    }
    
    0 讨论(0)
  • 2021-02-04 05:02

    The first suggestion that comes to mind is to delete all objects before inserting new objects from JSON.

    Lear more about deleting objects in Realm at https://realm.io/docs/swift/latest/#deleting-objects

    0 讨论(0)
提交回复
热议问题