Originally created by @sohilspaceo on GitHub (Jun 1, 2018).
I have a class name History in which I store history of users. I want to get top 30 recent items store in the DB and here is my query.
var arrRecentLanguages = [Model]()
if let count = CoreStore.fetchCount(From<History>()), count > 0 {
if let arrHistoryItems = CoreStore.fetchAll(From<History>(),
OrderBy<History>(.descending("objectID")),
Tweak({ (request) in
request.fetchLimit = 30
request.returnsDistinctResults = true
})) {
for item in arrHistoryItems {
//Checking From Lang
if !arrRecentLanguages.contains(where: { (recentItem) -> Bool in
return (recentItem.name == item.fromLangData.name)
}) {
arrRecentLanguages.append(item.translationModel.fromLangData)
}
//Checking To Lang
if !arrRecentLanguages.contains(where: { (recentItem) -> Bool in
return (recentItem.name == item.toLangData.name)
}) {
arrRecentLanguages.append(item.translationModel.toLangData)
}
}
}
}
The above code is working on iOS 11 or later but on iOS 10 or later I am getting crash which says:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'keypath objectID not found in entity '
Can anybody tell me what I am missing and same thing happens when I need to fetch last inserted item.
Originally created by @sohilspaceo on GitHub (Jun 1, 2018).
I have a class name `History` in which I store history of users. I want to get top 30 recent items store in the DB and here is my query.
```
var arrRecentLanguages = [Model]()
if let count = CoreStore.fetchCount(From<History>()), count > 0 {
if let arrHistoryItems = CoreStore.fetchAll(From<History>(),
OrderBy<History>(.descending("objectID")),
Tweak({ (request) in
request.fetchLimit = 30
request.returnsDistinctResults = true
})) {
for item in arrHistoryItems {
//Checking From Lang
if !arrRecentLanguages.contains(where: { (recentItem) -> Bool in
return (recentItem.name == item.fromLangData.name)
}) {
arrRecentLanguages.append(item.translationModel.fromLangData)
}
//Checking To Lang
if !arrRecentLanguages.contains(where: { (recentItem) -> Bool in
return (recentItem.name == item.toLangData.name)
}) {
arrRecentLanguages.append(item.translationModel.toLangData)
}
}
}
}
```
The above code is working on iOS 11 or later but on iOS 10 or later I am getting crash which says:
>
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'keypath objectID not found in entity <NSSQLEntity History id=2>'
Can anybody tell me what I am missing and same thing happens when I need to fetch last inserted item.
adam
added the question label 2025-12-29 18:24:28 +01:00
Hi, sorry for the late response. I'm not sure what is going on with iOS 10, but have you tried to use #keyPath(History.objectID) instead of "objectID"? It's possible they evaluate differently.
In the first place, I'm not sure why you would want to sort by object ID. Nothing in NSManagedObjectID pertains to anything about "recentness" as CoreData's primary keys are UUID type. It's likely you are sorting items arbitrarily, and may be better off removing the OrderBy clause.
@JohnEstropia commented on GitHub (Jun 10, 2018):
Hi, sorry for the late response. I'm not sure what is going on with iOS 10, but have you tried to use `#keyPath(History.objectID)` instead of `"objectID"`? It's possible they evaluate differently.
In the first place, I'm not sure why you would want to sort by object ID. Nothing in `NSManagedObjectID` pertains to anything about "recentness" as CoreData's primary keys are UUID type. It's likely you are sorting items arbitrarily, and may be better off removing the `OrderBy` clause.
Thank for the feedback. But I changed the entire logic. Well, still I can try it out. I was sorting based on objectID as we seen that it's increasing while entering new values, so I think better to go with and I din't know earlier about this functionality, but right now I added a timeStamp as a value in Database and based on that I am fetching recent Items.
@sohilspaceo commented on GitHub (Jun 11, 2018):
Thank for the feedback. But I changed the entire logic. Well, still I can try it out. I was sorting based on objectID as we seen that it's increasing while entering new values, so I think better to go with and I din't know earlier about this functionality, but right now I added a timeStamp as a value in Database and based on that I am fetching recent Items.
While it may be true that objectID gets higher with new records, this is most likely just an implementation artifact. ObjectIDs change across migrations, for example. If you have a timeStamp property now I definitely recommend to use that instead. Let me know if you find further issues.
@JohnEstropia commented on GitHub (Jun 11, 2018):
While it may be true that `objectID` gets higher with new records, this is most likely just an implementation artifact. ObjectIDs change across migrations, for example. If you have a timeStamp property now I definitely recommend to use that instead. Let me know if you find further issues.
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Originally created by @sohilspaceo on GitHub (Jun 1, 2018).
I have a class name
Historyin which I store history of users. I want to get top 30 recent items store in the DB and here is my query.The above code is working on iOS 11 or later but on iOS 10 or later I am getting crash which says:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'keypath objectID not found in entity '
Can anybody tell me what I am missing and same thing happens when I need to fetch last inserted item.
@JohnEstropia commented on GitHub (Jun 10, 2018):
Hi, sorry for the late response. I'm not sure what is going on with iOS 10, but have you tried to use
#keyPath(History.objectID)instead of"objectID"? It's possible they evaluate differently.In the first place, I'm not sure why you would want to sort by object ID. Nothing in
NSManagedObjectIDpertains to anything about "recentness" as CoreData's primary keys are UUID type. It's likely you are sorting items arbitrarily, and may be better off removing theOrderByclause.@sohilspaceo commented on GitHub (Jun 11, 2018):
Thank for the feedback. But I changed the entire logic. Well, still I can try it out. I was sorting based on objectID as we seen that it's increasing while entering new values, so I think better to go with and I din't know earlier about this functionality, but right now I added a timeStamp as a value in Database and based on that I am fetching recent Items.
@JohnEstropia commented on GitHub (Jun 11, 2018):
While it may be true that
objectIDgets higher with new records, this is most likely just an implementation artifact. ObjectIDs change across migrations, for example. If you have a timeStamp property now I definitely recommend to use that instead. Let me know if you find further issues.