Realm中的关系数据库?

我想在iOS Realm DB中创build简单的关系数据库 。 如何链接用户ID和那里愿望清单产品类似于SQL中的关系数据库。 像下面的图像的例子: 示例关系数据库图像

我知道我可以创build2个独立的Realm模型(表格),用time_stamp存储用户ID,在Wishlist的第二个表中存储用户ID,每个Wishlist产品或用户ID和一个Wishlist,用户有一个Wishlist数组。 现在我想存储所有用户有多个愿望清单。 这意味着每次用户进入APP时,我都必须查询每个愿望清单,看它是否适用于这个特定的愿望清单。 问题出在Wishlist表中,它没有任何主键和不允许创build没有主键的模型的领域。

Realm是否支持外键概念? 并且复合键的使用是复杂的。 有没有更有效的方法来做到这一点?

您可以使用RLMArray,其中RLMArray是Realm中用于定义多对多关系的容器types。 正如在这里的 Objective-C官方Realm文档中提到的那样。 也通过这个链接,它可能会帮助RLMArray文档

RLMArray被用于领域中的关系数据库概念。 你可以这样尝试:

#import <Realm/Realm.h> @class User; // User model @interface User : RLMObject @property NSString *name; @property NSString *user_id; @property RLMArray<Watchlist *><Watchlist> *watchlist; @end RLM_ARRAY_TYPE(Dog) // define RLMArray<Dog> // Watchlist model @interface Watchlist : RLMObject @property NSString *id; @property NSInteger *activity; @property NSInteger *cost; @end RLM_ARRAY_TYPE(Watchlist) // define RLMArray<Person> // Implementations @implementation User @end // none needed @implementation Watchlist @end // none needed 

从领域读取数据:

 RLMResults *watchlistDB = [Watchlist allObjects]; WatchlistDB = [realm_data objectAtIndex:index]; RLMArray *realm_array = WatchlistDB.watchlist;