【发布时间】:2013-12-17 14:35:11
【问题描述】:
我在实现以下用例时遇到问题:
Foo 通过名为myBar 的关系与Bar 具有一对一的关系。但是,多个Foo 可以有相同的Bar。
我来自 Web 应用程序背景,我会给 Foo 一个 bar_id,并使用 myBar 关系方法查询所有 Bars 与 bar_id。我知道 Core Data 不是 ORM,但我希望类似的东西是可能的。
我遇到的问题是将相同的Bar 实例设置为多个Foos 会导致以前的关系恢复为nil。
我是否别无选择,只能实现多对多关系?我真的很想只调用myBar 方法并让它返回Bar 实例,而不是一组只有1 个Bar。
我正在 RubyMotion 中实现这个项目,并在下面包含我的问题示例。 Objective-C 开发人员应该没有问题,但是这个问题更多的是原则而不是我编写的特定代码。
# assume `b` is some instance of Bar
f1 = Foo.alloc.init
f1.bar = b
# calling f1.bar getter now returns the `b` instance of Bar
f2 = Foo.alloc.init
f2.bar = b
# f2.bar getter returns the Bar instance, but *f1.bar does not* - it is now nil.
【问题讨论】:
标签: ios core-data rubymotion