来自核心数据对多关系的随机实体
我正在尝试设置这样的核心数据模型: 用户->朋友->专辑->图片 (减去User,这每一个都是一对多关系)
我需要有能力随机选择X张图片;最好,我想避免必须随机选择几个朋友,然后从这几个相册中,最后是几张图片 - 我的核心数据谓词经验非常有限,因此任何帮助将不胜感激!
I'm trying to setup a core data model like this:
User-> Friends-> Albums -> Pictures
(Minus User, each of these is a to-many relationship)
I need to have the ability to randomly select X amount of pictures; preferably, I would like to avoid having to randomly choose a few Friends, then from those a few Albums and finally a few pictures- my core data predicate experience is quite limited so any help would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,没有办法在
NSFetchRequest
中进行“随机排序”。不过,这似乎是一个不错的主意,因此您可能需要询问。您可以获得的结果是不为谓词指定任何排序描述符并设置获取限制。如果没有排序描述符,返回的对象将以某种可能不可预测的顺序出现。
As far as I'm aware, there's no way to do an "order by random" in an
NSFetchRequest
. Seems like a decent idea, however, so you might want to ask for it.The closes you can get is to not specify any sort descriptors to the predicate and set a fetch limit. Without a sort descriptor, the returned objects will come in some sort of probably-non-predictable order.
嗯,我自己对此不太确定,但是您不能使用以下功能做一些事情:
也许在循环中(不要忘记从
NSSet
中删除您使用的对象,或者您可能会再次得到相同的对象)。我自己对 CoreData 还很陌生,所以如果这是完全错误的,请告诉我:)
Hm I'm not so sure about this myself, but couldn't you do something with the following feature:
Maybe in a loop (don't forget to remove the object you used from the
NSSet
or you could end up with the same object again).I'm quite new to CoreData myself, so if this is totally wrong please let me know :)