问题
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