Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering query in Realm by NSDate throws NSInvalidArgumentException

I've looked everywhere, even hitting some dubious sites with virus warning messages that never goes away, and I can't figure this out.

I'm simply trying to filter Results<T> object by date:

let messages = realm.objects(RMChatMessage).filter("timestamp > \(date)) AND (timestamp <= \(to))"))

And whenever this line is run, it raises the following:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "timestamp > 1970-01-01 00:00:00 +0000"'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010fba8c65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000011174ebb7 objc_exception_throw + 45
    2   Foundation                          0x000000010ffb66bd _qfqp2_performParsing + 8495
    3   Foundation                          0x000000010ffb4526 +[NSPredicate predicateWithFormat:arguments:] + 46
...

I tried using NSDateFormatter with formats like yyyy-MM-dd hh:mm:ss, or date.description, using NSPredicate(format:...) instead of Result<T>.filter(...), and so on, yet nothing worked.

Is this some bug in Realm?

like image 249
nekonari Avatar asked Feb 09 '23 08:02

nekonari


1 Answers

let messages = realm.objects(RMChatMessage).filter("timestamp > %@ AND timestamp <= %@", date, to)

NSPredicate does not have any special handling for Swift string interpolation and doesn't support writing dates directly within the format string.

like image 117
Thomas Goyne Avatar answered Feb 24 '23 22:02

Thomas Goyne