ios - Realm query 'where before date' -
i've discovered realm , started working in objective-c.
i'm trying objects where create_date
before specific date , time
i read this answer on stackoverflow, tried :
nsstring *datestring = @"2015-06-03 08:24:00"; nsdateformatter * dateformatter = [nsdateformatter new]; [dateformatter setdateformat:@"yyyy-mm-dd hh:mm:ss"]; nsdate *newdate = [dateformatter datefromstring:datestring]; rlmrealm *realm = [rlmrealm defaultrealm]; nsstring *_where = [nsstring stringwithformat:@"create_date < '%@'", newdate]; rlmresults *results = [database_articles objectsinrealm:realm where:_where];
but i'm getting following error:
'expected object of type date property 'created' on object of type 'database_articles', received: 2015-06-03 05:24:00 +0000'
so how can pass nsdate in query in realm ??
thanks in advance ..
you used method + (rlmresults *)objectsinrealm:(rlmrealm *) where:(nsstring *), ...;
, convenience api, taking string , further arguments , constructing nspredicate
on-the-fly you. providing arguments first [nsstring stringwithformat:]
, serialize date, comes error.
use 1 of 2 variants avoid that:
rlmresults *results = [database_articles objectsinrealm:realm where:@"create_date < %@", newdate];
nspredicate *_where = [nspredicate predicatewithformat:@"create_date < %@", newdate]; rlmresults *results = [database_articles objectsinrealm:realm withpredicate:_where];
Comments
Post a Comment