Realm List Filter Swift

穿精又带淫゛_ 提交于 2019-12-14 03:56:31

问题


var numbersDetail = List is type of ContactDetail()
let predicate = NSPredicate(format: ContactDetail.NUMBER + " = %@", formattedNumber!)
let realmContactDetail = numbersDetail.filter(predicate).first

Fetching ERROR :

throw RLMException("This method may only be called on RLMArray instances retrieved from an RLMRealm");


回答1:


This error occurs if you try and perform a query on a Realm Swift List object (Which are actually Objective-C RLMArray objects under the hood) before its parent object has been added to a Realm.

class Person: Object {
  dynamic var name = ""
  dynamic var picture: NSData? = nil // optionals supported
  let dogs = List<Dog>()
}

let dog = Dog()
dog.name = "Rex"

let person = Person()
person.dogs.append(dog)

let rex = person.dogs.filter("name == 'Rex'") // QUERY WILL TRIGGER EXCEPTION AT THIS POINT

let realm = try! Realm()
try! realm.write {
    realm.add(person)
}

let rex = person.dogs.filter("name == 'Rex'") // Query will now work as expected

In a nutshell, you need to make sure that numbersDetail belongs to a Realm before you perform that query. You can easily test this by checking numbersDetail.realm != nil.



来源:https://stackoverflow.com/questions/40592305/realm-list-filter-swift

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