Realm - how to find a string in a List

喜夏-厌秋 提交于 2021-01-29 16:57:33

问题


Here's my object:

class Cat: Object {
    let toys = List<String>()
}

How can I find a toy in the toys array and delete it?

if let foundToy = cat.toys.filter(???).first {
     try! realm.write {
        realm.delete(foundToy)
     }
}

回答1:


If you are keeping an array of distinct String (i.e., the toys array doesn't contain duplicates), you can just delete the first String found:

if let toyIndex = cat.toys.firstIndex(of: toyNameToBeDeleted) {
     try! realm.write {
          cat.toys.remove(at: toyIndex)
     }
}

If you are trying to delete all String objects == to a certain toy name, do this instead:

 try! realm.write {
     cat.toys = cat.toys.filter { $0 != toyNameToBeDeleted }
 }



回答2:


Realm does not support queries on a List of primities (yet). You will need to define a Realm ToyClass and have a property of String.

See Array of Primitives: Support queries #5361

So create a ToyClass

class ToyClass: Object {
    @objc dynamic var toy_name = ""
}

and update your CatClass List

class CatClass: Object {
    let toys = List<ToyClass>()
}

There are lots of way to delete but if you know the name of the toy, you can delete it directly from Realm.

IMPORTANT - this will remove the first object that matches the filter criteria completely from Realm which includes the object and the reference to it in the list. Note that it's going to delete whatever object matches the first object so if you have two objects called 'toy 1' it will delete one of them - data stored in realm is 'unsorted' so the result may not be what you want.

if let toyToDelete = realm.objects(ToyClass.self).filter("toy_name == 'toy 1'").first {
    try! realm.write {
        realm.delete(toyToDelete)
    }
}

If you just want to remove the first object that matches the criteria (which may be dangerous) from the list but keep the object in Realm, you can do this

let cat = realm.objects(CatClass.self).first!
if let toyToDelete = cat.toys.filter("toy_name == 'toy 1'").first {
    try! realm.write {
        cat.toys.realm?.delete(toyToDelete)
    }
}

You should really add a primary key to your objects so you can tell realm specifically which object to find/delete.

class ToyClass: Object {
    @objc dynamic var toy_id = UUID().uuidString
    @objc dynamic var toy_name = ""
    
    override static func primaryKey() -> String? {
        return "toy_id"
    }
}

EDIT: Some testing code to demonstrate firstIndex potentially not working

Set up the cat and two toys

let cat0 = CatClass()
cat0.cat_name = "cat 0"

let toy0 = ToyClass()
toy0.toy_name = "toy 0"

let toy1 = ToyClass()
toy1.toy_name = "toy 1"

cat0.toys.append(toy0)
cat0.toys.append(toy1)

try! realm.write {
    realm.add(cat0)
}

Then retrieve cat0 and attempt to get the index of toy 1

let cat = realm.objects(CatClass.self).filter("cat_name == 'cat 0'").first!
let toyToBeDeleted = cat.toys.filter("toy_name == 'toy 1'").first!

print(toyToBeDeleted) //prints toy 1
let index = cat.toys.firstIndex(of: toyToBeDeleted)
print(index) //prints nil


来源:https://stackoverflow.com/questions/62603458/realm-how-to-find-a-string-in-a-list

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!