gpt4 book ai didi

java - 如何在 Realm 数据库中创建或实现自关系?

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

我有一个名为 User 的表。用户有 friend 。

public class User extends RealmObject {
@PrimaryKey
private long id;
private String name;
private int age;
private String email;
}

我正在使用 User 类将 User 及其 friend 添加到 Realm 。我的问题是,如何将用户链接到其 friend ?

最佳答案

我刚刚浏览了 Realm 文档并在那里找到了我的问题答案。

It is possible to declare recursive relationships which can be useful when >modelling certain types of data.

public class Person extends RealmObject {
private String name;
private RealmList<Person> friends;
// Other fields…
}

Use recursive relationships with care as Realm does not currently have cycle detection and you can easily end in infinite loops.

Setting the value to null for a RealmList field will clear the list. That is, the list will be empty (length zero), but no objects have been deleted. The getter for a RealmList will never return null. The returned object is always a list but the length might be zero.

关于java - 如何在 Realm 数据库中创建或实现自关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35577231/

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