Quickly remove an object from the kingdom

I have a Realm object that saves a list from JSON Response. But now I need to delete the object if the object is not in the list again from JSON. How am i doing this? This is my init for realm

func listItems (dic : Array<[String:AnyObject]>) -> Array<Items> { let items : NSMutableArray = NSMutableArray() let realm = try! Realm() for itemDic in dic { let item = Items.init(item: itemDic) try! realm.write { realm.add(item, update: true) } items.addObject(item) } return NSArray(items) as! Array<Items> } 
+10
source share
5 answers

imagine that your Items object has an id property and you want to delete old values ​​not included in the new set, or you can delete everything just

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

and then again add all the elements to the area, or you can also request every element 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) 
+23
source

What you can do is assign a primary key to the object that you are inserting, and when you receive a new processed JSON, you check whether this key 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 see if it exists.

 let repeatedItem = realm.objects(Items.self).filter("id = 'newId'") if !repeatedItem { // Insert it } 
+4
source

I will get a crash error if I delete as the top vote vote.

 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 entries in the transaction:

 let items = realm.objects(Items.self) try! realm!.write { realm!.delete(items) } 
+4
source

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

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

+3
source
 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
source

Source: https://habr.com/ru/post/1012890/


All Articles