如何从核心数据库获取最后插入的记录?

时间:2022-08-04 22:13:29

I am trying to fetch last inserted object from core database. But i did't find way to fetch last inserted object from database. Currently i am fetching all records from database in array and using last object of array for last inserted object. But i want to fetch only single object from database.

我正在尝试从core数据库中获取最后插入的对象。但是我没有找到从数据库中获取最后插入的对象的方法。目前,我正在从数据库中获取数组中的所有记录,并使用数组的最后一个对象作为最后插入的对象。但我只想从数据库中获取单个对象。

Is there anyway to find a last single record from database.?

是否存在从数据库中找到最后一条记录的方法?

Thanks and regards Sarabjit Singh

谢谢并问候Sarabjit Singh

2 个解决方案

#1


3  

//=== Sort your record ======//
NSSortDescriptor *sorting = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sorting]];

NSArray *fetchObjects = [[self managedObjectContext] executeFetchRequest:fetchRequest error:nil];
NSMutableArray *lastIndex = [[NSMutableArray alloc]init];
[lastIndex addObject:[fetchObjects objectAtIndex:0]];

#2


0  

See this question: Get last inserted item from Core Data, iOS

请参见这个问题:从Core Data获取最后一个插入项,iOS

The following is the key when running your fetch:

下面是运行取回时的关键:

setFetchLimit:1 and setFetchOffset:[number of all entries - 1]

#1


3  

//=== Sort your record ======//
NSSortDescriptor *sorting = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sorting]];

NSArray *fetchObjects = [[self managedObjectContext] executeFetchRequest:fetchRequest error:nil];
NSMutableArray *lastIndex = [[NSMutableArray alloc]init];
[lastIndex addObject:[fetchObjects objectAtIndex:0]];

#2


0  

See this question: Get last inserted item from Core Data, iOS

请参见这个问题:从Core Data获取最后一个插入项,iOS

The following is the key when running your fetch:

下面是运行取回时的关键:

setFetchLimit:1 and setFetchOffset:[number of all entries - 1]