CRUD - 创建 - Java SDK
Atlas Device SDK 已弃用。 有关详细信息,请参阅弃用页面。
关于本页中的示例
本页上的示例使用具有两种 Realm 对象类型的项目管理应用的Realm 数据模型: Project
和Task
。 Project
具有零个或多个Tasks
。
请参阅 Project
和 Task
这两个类的模式,如下所示:
ProjectTask.java
import org.bson.types.ObjectId; import io.realm.RealmObject; import io.realm.annotations.PrimaryKey; import io.realm.annotations.RealmClass; import io.realm.annotations.Required; public class ProjectTask extends RealmObject { public ObjectId _id; public String name; public String assignee; public int progressMinutes; public boolean isComplete; public int priority; public String _partition; }
Project.java
import org.bson.types.ObjectId; import io.realm.RealmList; import io.realm.RealmObject; import io.realm.annotations.PrimaryKey; import io.realm.annotations.RealmClass; import io.realm.annotations.Required; public class Project extends RealmObject { public ObjectId _id; public String name; public RealmList<ProjectTask> tasks = new RealmList<>(); }
ProjectTask.kt
import io.realm.RealmObject import io.realm.annotations.PrimaryKey import io.realm.annotations.Required import org.bson.types.ObjectId open class ProjectTask( var _id: ObjectId = ObjectId(), var name: String = "", var assignee: String? = null, var progressMinutes: Int = 0, var isComplete: Boolean = false, var priority: Int = 0, var _partition: String = "" ): RealmObject()
Project.kt
import io.realm.RealmList import io.realm.RealmObject import io.realm.annotations.PrimaryKey import io.realm.annotations.Required import org.bson.types.ObjectId open class Project( var _id: ObjectId = ObjectId(), var name: String = "", var tasks: RealmList<ProjectTask> = RealmList(), ): RealmObject()
创建新对象
在ACID 事务中使用 域 ()可在 Realm 中创建域Realm 对象的持久实例。 然后,您可以使用访问器和赋值器用其他字段值修改返回的对象。
以下示例演示了如何使用createObject() 创建对象:
realm.executeTransaction(r -> { // Instantiate the class using the factory function. Turtle turtle = r.createObject(Turtle.class, new ObjectId()); // Configure the instance. turtle.setName("Max"); // Create a TurtleEnthusiast with a primary key. ObjectId primaryKeyValue = new ObjectId(); TurtleEnthusiast turtleEnthusiast = r.createObject(TurtleEnthusiast.class, primaryKeyValue); });
realm.executeTransaction { r: Realm -> // Instantiate the class using the factory function. val turtle = r.createObject(Turtle::class.java, ObjectId()) // Configure the instance. turtle.name = "Max" // Create a TurtleEnthusiast with a primary key. val primaryKeyValue = ObjectId() val turtleEnthusiast = r.createObject( TurtleEnthusiast::class.java, primaryKeyValue ) }
您还可以将对象从JSON插入到域中。 Realm支持从String
、 JSONObject 创建对象 和 InputStream 类型。 Realm会忽略JSON中存在的任何未在Realm 对象模式中定义的属性。
以下示例演示了如何使用createObjectFromJson()从JSON创建单个对象,或使用createAllFromJson()从JSON创建多个对象:
// Insert from a string realm.executeTransaction(new Realm.Transaction() { public void execute(Realm realm) { realm.createObjectFromJson(Frog.class, "{ name: \"Doctor Cucumber\", age: 1, species: \"bullfrog\", owner: \"Wirt\" }"); } }); // Insert multiple items using an InputStream realm.executeTransaction(new Realm.Transaction() { public void execute(Realm realm) { try { InputStream inputStream = new FileInputStream( new File("path_to_file")); realm.createAllFromJson(Frog.class, inputStream); } catch (IOException e) { throw new RuntimeException(e); } } });
// Insert from a string realm.executeTransaction { realm -> realm.createObjectFromJson( Frog::class.java, "{ name: \"Doctor Cucumber\", age: 1, species: \"bullfrog\", owner: \"Wirt\" }" ) } // Insert multiple items using an InputStream realm.executeTransaction { realm -> try { val inputStream: InputStream = FileInputStream(File("path_to_file")) realm.createAllFromJson(Frog::class.java, inputStream) } catch (e: IOException) { throw RuntimeException(e) } }