iPhone - 核心数据模型设计问题
我正在旁听斯坦福大学的 iTunesU iPhone 开发课程,并尝试使用 Core Data 来理解。
本质上,该应用程序有两个利用核心数据的选项卡: 1) 最喜欢的照片 - 这是用户选择“最喜欢”照片的位置列表(来自 Flickr)。 (如果给定位置没有“收藏”的照片,则会从此列表中删除)。 2) 最近的照片 - 这是用户最近查看的照片列表。
我最初的想法是使用两个实体:照片和位置,其中照片将具有作为位置的“whereTaken”关系,而位置将具有逆关系“照片”以包含来自该位置的所有照片对象。
然而,使用这个模型,我对如何跟踪给定位置是否有任何“收藏的”照片感到有点困惑。该作业建议在位置实体中保留一个属性,以指定该位置是否有任何“收藏的”照片。
所以——这是我的问题:
- 创建更好 单独的实体 “最喜欢的照片”和“最近的照片” 而不仅仅是“照片”?
- 如果没有,是否建议(或可能) 保留照片的子集(仅 逆向“最喜欢”的 关系)在位置?
- 或者,我是否需要迭代 NSSet 每次删除照片对象 “收藏”照片看看有没有 还有其他“最喜欢”的照片吗 左边?或者,也许记一下 收藏的照片作为属性 地点?
也许,我的想法是完全错误的——无论哪种方式,我都会感激任何关于在这种情况下设计核心数据模型的建议,无论是一般的还是具体的。
非常感谢您的时间和帮助!
I'm auditing the iTunesU iPhone development course by Stanford, and am trying to wrap my head around using Core Data.
Essentially, the application has two tabs that utilize Core Data:
1) Favorite Photos - This is a list of Locations (from Flickr) that the user has selected to "favorite" a photo for. (If there are no photos "favorited" in a given location it is removed from this list).
2) Recent Photos - This is a list of photos most recently viewed by the user.
My initial thought was to use two entities: Photo and Location, where Photo would have the a relationship "whereTaken" that would be a Location, and Location would have the inverse relationship "photos" to contain all Photo objects from that Location.
However, with this model, I get a little confused on how to keep track of whether or not a given Location has any "favorited" photos left. The assignment suggested to keep an attribute in the Location entity to specify whether or not there are any "favorited" photos in that location.
So -- here are my questions:
- Would it be better to create
separate entities for
"FavoritePhoto" and "RecentPhoto"
rather than just "Photo"? - If not, is it suggested (or possible) to
keep a subset of photos (only the
"favorited" ones in the inverse
relationship) in Location? - Or, do I need to iterate on the NSSet of
Photo objects every time I remove a
"favorited" photo to see if there
are any other "favorited" photos
left? Or, perhaps keep a count of
favorited photos as an attribute in
Location?
Maybe, I'm going about this completely wrong -- either way, I'd appreciate any kind of advice, general or specific, regarding designing a Core Data model in this case.
Thank you very much for your time and help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该阅读一些有关 NSPredicate 的内容。您可以将其附加到获取请求以过滤您想要检索的照片。创建两个不同的照片实体很可能是一个错误。您可能想要向 Photo 实体添加 BOOL 属性“isFavorite”。然后,在获取照片时,您可以使用以下内容:
您还可以使用稍微复杂的谓词来过滤最喜欢的状态和位置。
NSPredicate 编程指南
You should read up a bit on NSPredicate. You can attach this to a fetch request to filter the photos you wish to retrieve. Creating two different Photo entities is most likely a mistake. You might want to add a BOOL attribute "isFavorite" to the Photo entity. Then, when fetching the photos, you would use something like:
You can also make slightly more complex predicates to filter for both favorite status and location.
NSPredicate Programming Guide
您可以在位置实体中创建一个获取的属性,并让它仅使用适当的谓词获取“收藏夹”照片。
希望有帮助。
You can create a fetched Property in the Location entity and have it only fetch the 'favorited' photos with the appropriate predicate.
Hope that helps.