核心数据中的单向关系

发布于 2024-10-31 16:14:02 字数 216 浏览 1 评论 0原文

每家餐厅都必须有纬度和经度。但是,每个 LatitudeLongitude 对象都不能有“一家餐厅”,

因此我有一种单向关系并生成编译器警告。

有什么问题吗?

我应该使用 fetchRelationship 吗?但是 fetchRelationship 是什么?

为什么是一种方式呢?为什么它有谓词?为什么叫取呢?我阅读了文档,但似乎不明白它到底是什么。

Every restaurant must have Latitude and Longitude. However, every LatitudeLongitude object must not have "a restaurant"

So I have a one way relationship and generate a compiler warning.

What's the catch?

Should I use fetchRelationship instead? But what is fetchRelationship?

Why is it one way? Why does it have predicate? Why it is called fetch? I read the documentation and doesn't seem to get what it really is.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

马蹄踏│碎落叶 2024-11-07 16:14:02

您应该使用获取的属性(它们是满足指定获取谓词的托管对象的集合)来表示单向关系。更多信息请参见此处

更新

您可能应该在您的情况下使用属性,否则您将向 Restaurant 实体添加获取的属性,将其目标实体设置为 LatitudeLongitude 并将纬度和经度键值对存储在其 userInfo 字典中。它的获取谓词如下所示:

($FETCH_SOURCE.longitude LIKE [c] $FETCHED_PROPERTY.userInfo.longitude) AND ($FETCH_SOURCE.latitude LIKE [c] $FETCHED_PROPERTY.userInfo.latitude)

You should use fetched properties (which are an collection of managed objects satisfying a specified fetch predicate) to represent one-way relationships. More information here.

Update

You should probably use attributes in your case, otherwise you would add a fetched property to the Restaurant entity, set it's destination entity to LatitudeLongitude and store lattitude and longitude key-value pairs in its userInfo dictionary. Its fetch predicate would look like this:

($FETCH_SOURCE.longitude LIKE [c] $FETCHED_PROPERTY.userInfo.longitude) AND ($FETCH_SOURCE.latitude LIKE [c] $FETCHED_PROPERTY.userInfo.latitude)
场罚期间 2024-11-07 16:14:02

好吧,这是一个非常老的问题,但这就是我现在解决它的方法。

并非所有关系都必须被填补。保持反向(双向)关系,但不一定必须将它们链接起来。

+ (void)createRestaurantWithCompletion:(void (^)(BOOL, NSError *))completion {
    [MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
        Restaurant *localRestaurant;
        LatitudeLongitude *localLatLng1;
        LatitudeLongitude *localLatLng2;

        restaurant = [Restaurant MR_createInContext:localContext];

        localLatLng1 = [LatitudeLongitude MR_createInContext:localContext];
        localLatLng2 = [LatitudeLongitude MR_createInContext:localContext];

        restaurant.latLng = localLatLng1;
    }                 completion:completion];
}

调用 completion 时,localLatLng1localLatLng2 均已存在,其中一个链接到餐厅,另一个则不链接。
当然,这个方法在这种形式下没有任何意义,它只是为了证明你可以创建对象而不必满足它们的关系。

Z。

OK, super-old question, but here's how I'd be solving it now.

Not all relationships have to be filled. Keep your inverse (two-way) relationships but you don't necessarily have to link them.

+ (void)createRestaurantWithCompletion:(void (^)(BOOL, NSError *))completion {
    [MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
        Restaurant *localRestaurant;
        LatitudeLongitude *localLatLng1;
        LatitudeLongitude *localLatLng2;

        restaurant = [Restaurant MR_createInContext:localContext];

        localLatLng1 = [LatitudeLongitude MR_createInContext:localContext];
        localLatLng2 = [LatitudeLongitude MR_createInContext:localContext];

        restaurant.latLng = localLatLng1;
    }                 completion:completion];
}

By the time completion is called, both localLatLng1 and localLatLng2 exist, one is linked to a restaurant, one isn't.
Of course this method makes no sense in this form, it's just to prove the point that you can create objects without having to satisfy their relationships.

Z.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文