Realm Flexible Sync, Dates/TimeStamps as Queryable Fields?

Hi all,

Using Realm Flexible sync (in Swift), is it possible to subscribe to a Date-valued queryable field?

I am using the latest Realm release for SwiftUI (11.11.0) and get the following error when I try to set a subscription on date-valued queryable field (‘date’) :

Received QUERY_ERROR “Client provided query with bad syntax: unsupported query for table “DiaryEntry”: server-side queries on timestamp values only supports precision to the millisecond” (error_code=300, query_version=3)

  • The function I’m trying to call:

func setSubscriptionDiaryEntriesBeforeToday(realm: Realm) {
        let subscriptions = realm.subscriptions
        subscriptions.write {
            if let currentSubscription = subscriptions.first(named: "diary entry before date") {
                currentSubscription.update(toType: DiaryEntry.self) { entry in
                    entry.date <= Date.now                }
            } else {
                subscriptions.append(QuerySubscription<DiaryEntry>(name: "diary entry before date") { entry in
                    entry.date <= Date.now
                })
            }
        }
}```



  • The class that I’m trying to query results for:
**class** DiaryEntry: Object, ObjectKeyIdentifiable {

@Persisted(primaryKey: **true** ) **var** _id = UUID().uuidString

@Persisted **var** ownerID: String

@Persisted **var** title = ""

@Persisted **var** body = ""

@Persisted **var** date: Date

**convenience** **init** (ownerID: String, title:String = "default", body: String = "default") {

**self** .init()

**self** .ownerID = ownerID

**self** .title = title

**self** .body = body

}

}

Thanks in advance for any suggestions / clarifications!

1 Like

Hey, yeah unfortunately this is a correctness issue with the fact that Realm uses nanosecond precision and MongoDB uses millisecond precision. Therefore, the easiest workaround is to just trim the Time.now to milliseconds (removing the nanosecond precision).

We actually will be releasing a change tomorrow that will allow you to only use > or < (not >= and <= unfortunately), because we can use those safely. Sorry for this inconvenience but this is to prevent diverging states on the sync client due to the subscription being interpreted differently in MongoDB.

1 Like

Clear, thanks for your quick reply!
Looking forward to that new release.

Makes sense that queries on dates are constrained, think the err message could be a bit more informative then though, since I reckon a lot of people will be trying to query that way and I personally could not find anything on these constraints in the docs?

Cheers!

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.