gpt4 book ai didi

ios - Realm 多对多查询(逆向也是!)

转载 作者:行者123 更新时间:2023-11-30 13:42:58 25 4
gpt4 key购买 nike

我的应用程序的数据库在 Feed 对象和 Tweet 对象之间存在多对多关系。这是为了跟踪每条推文所属的提要。如果您熟悉 Twitter,请想象一下主提要、列表提要、用户个人资料提要等。

如何使用 NSPredicate 进行查询来获取特定 Feed 中存在的 Tweet 列表(反之亦然) ,获取存在 TweetFeeds 列表)?似乎反向关系的查询在 Realm 中不起作用,那么我的选择是什么?

最佳答案

如果我正确理解你的问题,这部分文档应该会有所帮助:

Inverse Relationships Links are unidirectional. So if a to-many property Person.dogs links to a Dog instance and a to-one property Dog.owner links to Person, these links are independent from one another. Appending a Dog to a Person instance’s dogs property, doesn’t automatically set the dog’s owner property to this Person. Because manually synchronizing pairs of relationships is error prone, complex and duplicates information, Realm exposes an API to retrieve backlinks described below.

With inverse relationships, you can obtain all objects linking to a given object through a specific property. For example, calling Object().linkingObjects(_:forProperty:) on a Dog instance will return all objects of the specified class linking to the calling instance with the specified property.

我想你可以这样做:

//assuming your Tweet object has a property like "let feeds = List<Feed>()"
someTweet.linkingObjects(Feed.self, forProperty: "feeds") //should return feeds your Tweet is in

但我仍然认为我没有清楚地理解你的问题。从我的角度来看,您的第一个要求:

get a list of Tweets that exist in a specific Feed

应该有一个简单的解决方案,例如在 Feed 对象中具有如下属性:

let tweets = List<Tweet>()

希望您能进一步澄清您的情况。

关于ios - Realm 多对多查询(逆向也是!),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35344791/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com