Ayrad
Ayrad

Reputation: 4056

How do you retrieve the last month's entries from core data

how do I ask core data to fetch me the last 30 elements entered or alternatively the elements entered within a month of today?

Upvotes: 1

Views: 474

Answers (1)

ZhangChn
ZhangChn

Reputation: 3184

To fetch a certain number of records, use qualification like [fetchRequest setFetchLimit:30] in combination with NSPredicate instance you desired.

To retrieve elements within a range of time, you need first calculate the beginning and the end NSDate instances of the period you want, then code your predicate instance like:

NSPredicate *predicate = [NSPredicate predicateWithFormat:
            @"(date >= %@) AND (date <= %@)", startDate, endDate];

See this and this.

Upvotes: 1

Related Questions