I’m working in SwiftUI and my objective is to display a view that observes results for the past week - functionally equivalent to:
let today = Calendar.current.startOfDay(for: Date())
let oneWeekAgo = calendar.date(byAdding: .day, value: -7, to: today)!
let logs = try! Realm().objects(Log.self).filter("entryDate BETWEEN {%@, %@}", today, oneWeekAgo)
My current understanding is that I can’t pass in a computed predicate like this into the ObservedResults wrapper initializer. (If I’m mistaken please let me know.) Is calling the above code on view initialization and observing it manually via notification tokens the appropriate/optimal method of achieving the desired result?
Hi @Rudi_Strahl,
You can pass an NSPredicate to @ObservedResults as a filter…
@ObservedResults(Decisions.self, filter: NSPredicate(format: "isSoft == NO AND isSplit == NO")) var decisions
I’ve not had the chance to check whether that predicate can be dynamic.
There’s been some recent work to have “type-safe” queries (i.e. not using strings to formulate the query). realm-cooa 10.19 contains the latest enhancements.
Thanks @Andrew_Morgan - after some experimentation, this doesn’t appear possible at the moment given how initialization of view variables and wrappers work in SwiftUI. I was able to achieve the results by using an ObservableObject and initializing a @Published var logs: Result<Log>.