Search code examples
ioscore-datamagicalrecord

how to get "Newest Record" when fetching record With Magical Record


How do I get the latest record when fetching records?

for example:

  • first iteration i save "Singapore" with object name Country,
  • second iteration i save "USA" with object name Country,second

Now when i fetch it

SomeDatabase *Country = [SomeDatabase MR_findFirst];

NSLog(@"Country Name---->%@",Country.Name); 

I get "Singapore",

I want get the last record inserted to coredata, which is supposed to be "USA".


Solution

  • The most straight forward way is to add a "createdDate" or similar date field to your entity. Then you can simply use the query:

    [MyEntity MR_findFirstOrderedByAttribute:@"createdDate" ascending:NO];