Atlas Device SDK は非推奨です。 詳細については、 の廃止ページを参照してください。
このページの例について
このページの例では、 ProjectとTaskの 2 つの Realm オブジェクトタイプを持つプロジェクト管理アプリのデータモデルを使用します。 Projectには 0 個以上のTasksがあります。
これらの 2 つのクラス、 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()
新しいオブジェクトの作成
トランザクションで realm.createObject()を使用して、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 ) }
You can also insert objects into a realm from JSON. Realm supports creating objects from String, JSONObject, and InputStream types. Realm ignores any properties present in the JSON that are not defined in the Realm object schema.
次の例は、 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) } }