I declared this property in my realm model:
class Photo:Object{ let score = RealmOptional<Double>() }And I want to query all Photos whose
scorehave not been calculated yet.Expected Results
I did the following query:
let photos = realm.objects(Photo.self).filter("value = nil") print(photos.count)Actual Results
But it returns nothing (it compiles though).
0Version of Realm and Tooling
0.98.3
dependency manager involved : CocoaPods
I have a similar question:
A query such as:
class Person : Object
{
let friend_count = RealmOptional<Int>()
}
let people_with_not_many_friends = realm.objects(Person).filter("friend_count <= 10")
Filters out those with 'nil' for friend_count.
Is this expected behaviour/is there a way to prevent this?
@JosephZZ Could you try let photos = realm.objects(Photo.self).filter("score = nil")?
Using value property when you access RealmOptional, but in query, you should specify just its property name.
@akramhussein Yes, it is intended behavior. You can add OR friend_count = nil if you'd like to include the objects that friend_count is nil. Like the following:
let people_with_not_many_friends = realm.objects(Person)
.filter("friend_count <= 10 || friend_count = nil") // Query for less than 10 friends or no friends
:+1: That fixed it. Many thanks. Realm is great :100: .
However, I'm having a related issue with Sort Descriptors now. When I sort on friend_count I previously had nil values at the bottom e.g. descending = 10,9, nil, nil. ascending = 1, 2, nil, nil (going down a table view for example).
To achieve this with CoreData, the only option available was to add a transient property Boolean to the Model called has_friends which would be used as the first NSSortDescriptor to force ordering of 'Nil' values.
Not sure if my explanation makes sense. I'm currently trying different methods, but I'm wondering if there is a Realm way of achieving this?
Apologies - problem solved, was possible in Realm, just needed to tweak my properties. :+1:
Glad to hear your _sorted_ this out :wink:
Most helpful comment
Glad to hear your _sorted_ this out :wink: