设置获取以搜索超类?
我有一个 NSFetch 可以在超类中搜索对象,效果很好。有4个小类。我想让它搜索 4 个子类中的 3 个。如果没有应用内购买,则 4 个子类之一无法在应用中显示,因此我不希望通过搜索看到它。
有什么想法吗?
I have a NSFetch that searches for objects in the Superclass which works fine. There are 4 subclasses. I want to have it search for 3 out of the 4 subclasses instead. One of the 4 subclasses can't be displayed in the app without an in app purchase so I don't want it visible via a search.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来您正在
NSFetchRequest
中设置实体,并且希望阻止某个子实体显示。我认为最好的选择就是自己过滤获取结果,因为 NSPredicate 无法测试实体类型。执行 NSFetchRequest 后,您可以构造一个新数组,该数组仅包含第一个数组中不属于该特定子类的元素。当然,如果您将获取请求限制为一定数量的结果(例如“分页”您的数组),那么情况会变得更加复杂。在这种情况下,您可以获取比您需要的更多的对象,前提是某些对象将被过滤掉,并且如果您过滤掉了太多对象,以致只剩下小于页面大小的对象,则可以执行第二次获取并继续。是的,这很复杂。另一种解决方案是在超类中存储一个布尔值,指示该对象是否需要 IAP,然后始终将一个子类的值设置为
YES
,将所有其他子类的值设置为NO
。这样您就可以在NSPredicate
中测试布尔值。Sounds like you're setting the entity in an
NSFetchRequest
and you want to prevent a certain sub-entity from showing up. I think your best bet is simply to filter the fetch results yourself, since NSPredicate can't test the entity type. Once you've executed your NSFetchRequest you can construct a new array consisting only of elements of the first array that aren't a member of that particular subclass. Granted, if you're limiting the fetch request to a set number of results (e.g. "paging" your array), it's going to get a bit more complicated. In that case, you can fetch more objects than you need on the assumption that some will be filtered out, and if you filter out so many that you're left with less than a page size, you can execute a second fetch and continue. Yes, it's complicated.An alternative solution is to store a boolean on your superclass which indicates whether the object requires IAP, and then always set this to
YES
for one subclass andNO
for all others. That way you can test the boolean in yourNSPredicate
.