Filtering Realm Objects Using Swift

I always get the following error when trying to filter my Realm database using NSPredicate :

The 'text' property is not a reference in an object of type 'getType'

I want to filter the Realm database to display only those elements that contain specific text in them. This is what I tried:

 let realm = try! Realm() let predicate = NSPredicate(format: "typez.text.filter = 'special'") let filterThis = realm.objects(Publication).filter(predicate) print(filterThis) 

The relevant part of my model classes:

 class Publication: Object, Mappable { dynamic var id: Int = 0 var typez = List<getType>() dynamic var url: String? } class getType: Object, Mappable { dynamic var text: String = "" } 
+5
source share
3 answers

I usually don't use NSPredicate, instead I do a built-in predicate closure in the filter parameter.

 let realm = try! Realm() //Array of publications let realmObjects = realm.objects(Publication) //any publication where .text property == special will be filtered. and filter out empty array let filterThis = realmObjects.filter({ $0.getType.filter({ $0.text == "special" } != [] ) }) print(filterThis) 
0
source

You mentioned that the relevant parts of your model classes look like this:

 class Publication: Object, Mappable { dynamic var id: Int = 0 var typez = List<getType>() dynamic var url: String? } class getType: Object, Mappable { dynamic var text: String = "" } 

If you understand correctly, you want to find instances of Publication that have an entry in the typez list with text equal to special . You can express it as:

 let realm = try! Realm() let result = realm.objects(Publication).filter("ANY typez.text = 'special'") print(result) 
+9
source

I did not like the accepted answer because it actually did not answer the question ... but then it helped me more than I understood. Now I will use closure instead of NSPredicates. The actual answer to this question should be a slightly modified version of @NSGangster's answer:

 let realm = try! Realm() //Array of publications let realmObjects = realm.objects(Publication) //any publication where .text property == special will be filtered. and filter out empty array let filterThis = realmObjects.filter({ $0.typez.filter({ $0.text == "special" } != [] ) }) print(filterThis) 

.. or something close to that.

But what I was looking for was a little different. I need a way to filter on the exact words of a verbose string, and using NSPredicate with "CONTAINS" would match any contained substring, for example. a search for "red" will match "fred". The kingdom does not yet support LIKE or regex, so using closure was the only way I could work:

 //I was going for a "related terms" result for a dictionary app let theResults = terms.filter( { //Looking for other terms in my collection that contained the //title of the current term in their definition or more_info strings $0.definition.components(separatedBy: " ").contains(term.title) || $0.more_info.components(separatedBy: " ").contains(term.title) } ) 

With so many days when I searched, we hope this helps someone else with a similar problem.

+1
source

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


All Articles