iPhone OS:使用 NSPredicate Nsfetchrequest 和核心数据获取随机实体实例
在开发一个应用程序时,我有大量的托管对象集合,我想针对这些对象获取一些随机实例。
我的问题是,有什么方法可以使用 NSPredicate 和 NSFetchRequest 随机返回多个对象。
我看到您实际上可以使用数据建模器将 NSFetchRequest 添加到实体中,有什么方法可以使用它进行随机获取吗?
另外,确定表“计数”的最佳方法是什么,以便我可以设置随机数生成器的范围。
如果您需要更多详细信息,请告诉我。
谢谢!
缺口
Working on an app where I have a large collections of managed objects against which I want to fetch a few random instances.
My question is, is there any way I can use NSPredicate and NSFetchRequest to return several objects at random.
I saw that you could actually add a NSFetchRequest into the entity using the data modeler, any way to do the random fetch using this?
Also what would be the best method for determining the "count" of a table so I can set the bounds of the random number generator.
let me know if you need more details.
Thanks!
Nick
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
相反,将 fetchLimit 与 fetchOffset 结合使用,这样您就可以有效地将单个实体提取到内存中:
Instead use the fetchLimit in conjunction with the fetchOffset that way you can efficiently fetch only a single entity into memory:
这可能不完全是您实现此操作的方式,但希望它能帮助您入门。
在标头或实现文件顶部的某个位置:
实现中的其他位置:
如果您需要在不进行替换的情况下进行采样,以消除重复项,您可以创建一个
randomEntityIndex
的NSSet
>NSNumber
对象,而不只是随机采样int
。在这种情况下,从有序的
NSSet
中进行采样,在将NSNumber
对象从包中取出时将其删除,并减少myEntityCount
以便从集合中随机选择一个NSNumber
对象。This may not be exactly how you implement this, but hopefully it will get you started.
Somewhere in your header or at the top of your implementation file:
Elsewhere in your implementation:
If you need to sample without replacement, to eliminate duplicates, you might create an
NSSet
ofrandomEntityIndex
NSNumber
objects, instead of just sampling randomint
s.In this case, sample from an ordered
NSSet
, removeNSNumber
objects as you pull them out of the bag, and decrementmyEntityCount
for the purposes of picking a randomNSNumber
object from the set.我已经为此进行了很多搜索,本质上 Coredata 不会为您提供任何随机行,而且它也不是故意的。你必须创建自己的。
这就是我想到的,假设我们使用 NSPredicate 并且没有主唯一键,这是我认为开销最小的最佳答案。
NSFetchRequest
类型设置为NSManagedObjectID
。关闭一切,以尽量减少开销。来自接收到的
NSManagedObjectID
数组。获取随机数量的对象。这是一个很好的解决方案: 获取 n 个随机对象(例如 4 ) from nsarray现在您已经获得了所需数量的随机
NSManagedObjectID
(或多或少是随机的)NSManagedObjectContext objectWithID:
来获取对象。I've searched around a lot for this, essentially Coredata won't give you any random rows, and it is not meant to. You have to create your own.
This is what I came up with up with, assuming we are using an
NSPredicate
and theres no primary Unique key, this is the best possible answer i think with the least overhead.NSFetchRequest
type toNSManagedObjectID
. Turn everything off, to minimize overhead.From the received
NSManagedObjectID
array. get your random number of objects. this is a good solution: Get n random objects (for example 4) from nsarrayNow you've got random
NSManagedObjectIDs
of your desired count, (which are more or less random)NSManagedObjectContext objectWithID:
to get the objects.这就是我根据 Corey Floyd 的答案在 Swift 中所做的,给定一个 Verb 实体。
一些手动测试给了我令人满意的随机结果。
This is what I did in Swift based on Corey Floyd's answer, given a
Verb
entity.A few manual tests give me satisfactory random results.
如果您无论如何都要获取所有对象,则不需要第一个请求来获取对象计数。你可以使用类似的东西:
If you are fetching all the objects anyway, there is no need for the first request to get the object count. You can just use something like:
如果您需要随机化受谓词限制的子集表行中的提取(例如“where some
到目前为止,我拥有的最佳解决方案(我不需要获取所有或大量行)是基于主键使用随机选择(当然,需要表中的主键,最好没有任何缺失值)。
Solution suggested by Core will not work if you need to randomize the fetch within a subset table rows restricted by a predicate (e.g. "where something < something").
The best solution I have so far (where I do not need to fetching all or large number of rows) is using randomly selecting based on a primary key (off course the requires a primary key in the table, preferably w/o any missing values).