Anton Diablo
Anton Diablo

Reputation: 23

Realm filter firstDate older then newDate

I am trying to get the objects from realm, where newDate is later then firstDate. So if the date from firstDate is 05.10.2017, it will get objects after that date, example 06.10.2017, but not 04.10.2017.

This is how I am storing the date:

class User: Object {
    @objc dynamic var firstDate = Date()
    @objc dynamic var newDate = Date()
}

This is how I am saving the objects:

let date = Date()
let realm = try! Realm()
let myUser = User()

myUser.firstDate = self.date

This is how I am trying to retrieve the objects:

var userData: Results<User>?
if (homeIndexPathRow == 0) {
    let getData = realm.objects(User.self).filter("firstDate > newDate")
    userData = getData
    print("userData", userData!)
}

When trying to retrieve the objects, the app crashes.. Is it something wrong with the filter format?

Upvotes: 1

Views: 391

Answers (1)

Erik Auranaune
Erik Auranaune

Reputation: 1414

Try this:

var yourNSDate = NSDate()
let predicate = NSPredicate(format: "firstDate < %@", yourNSDate)
let dataResults = realm.objects(User.self).filter(predicate)
userData = dataResults

Replace that with the code below if (homeIndexPathRow == 0) { ...

Upvotes: 1

Related Questions