Realm-cocoa: Fail with query.

Created on 25 Nov 2016  路  3Comments  路  Source: realm/realm-cocoa

Goals

I want to get query works.

Actual Results

Always get crash...

Terminating app due to uncaught exception 'Invalid property name', reason: 'Property 'New' not found in object of type 'Item''

Steps to Reproduce

Code Sample

I have object with properties:

dynamic var categoryTitle = ""
dynamic var isNew: Bool = false

Then I try to query them:

let realm = try! Realm()
newItems = realm.objects(Item.self).filter("isNew = \(true) AND categoryTitle = \(category.title)")

If I use next query - everything is work fine, but I need complex query:

newItems = realm.objects(Item.self).filter("isNew = \(true)")

Don't know why Realm search property with name New instead of isNew. May be it depends of Swift version - I use Swift 3.

Version of Realm and Tooling

Realm version: 2.1.0

Xcode version: 8.1 (8B62)

iOS/OSX version: iOS 10.1

Dependency manager + version: CocoaPods

T-Help

Most helpful comment

It looks like the value of category.title is treated as property not the string. Try to specify your query like this:

// NOTE: this could lead to error if category.title contains \' use the second example instead
.filter("isNew = '\(true)' AND categoryTitle = '\(category.title)'")

or, the recommended way:

.filter("isNew = true AND categoryTitle = %@", category.title)

All 3 comments

It looks like the value of category.title is treated as property not the string. Try to specify your query like this:

// NOTE: this could lead to error if category.title contains \' use the second example instead
.filter("isNew = '\(true)' AND categoryTitle = '\(category.title)'")

or, the recommended way:

.filter("isNew = true AND categoryTitle = %@", category.title)

Oh! Thanks! It works.
But any way behavior is strange...

Awesome! Learn more about queries in docs at https://realm.io/docs/swift/latest/#queries.

Was this page helpful?
0 / 5 - 0 ratings