Skip to content

Commit

Permalink
Initial draft
Browse files Browse the repository at this point in the history
  • Loading branch information
cbullinger committed Oct 20, 2023
1 parent 2e7a3ee commit 9454cdf
Show file tree
Hide file tree
Showing 22 changed files with 1,187 additions and 229 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,76 +38,6 @@ class Contact : RealmObject {
}

class DataTypesTest : RealmTest() {
@Test
fun createEmbeddedObject() {
runBlocking {
val config = RealmConfiguration.Builder(
setOf(Contact::class, EmbeddedAddress::class)
)
.build()
val realm = Realm.open(config)
realm.write {
val contact = copyToRealm(Contact())
contact.apply {
name = "Nick Riviera"
address = EmbeddedAddress().apply {
street = "123 Fake St"
city = "Some Town"
state = "MA"
postalCode = "12345"
}
}
}

val asyncCall: Deferred<Unit> = async {
val address: EmbeddedAddress = realm.query<EmbeddedAddress>().find().first()
val contact: Contact = realm.query<Contact>().find().first()
// :snippet-start: update-embedded-object
// Modify embedded object properties in a write transaction
realm.write {
// Fetch the objects
val addressToUpdate = findLatest(address) ?: error("Cannot find latest version of embedded object")
val contactToUpdate = findLatest(contact) ?: error("Cannot find latest version of parent object")

// Update a single embedded object property directly
addressToUpdate.street = "100 10th St N"

// Update multiple properties
addressToUpdate.apply {
street = "202 Coconut Court"
city = "Los Angeles"
state = "CA"
postalCode = "90210"
}

// Update property through the parent object
contactToUpdate.address?.state = "NY"
}
// :snippet-end:

// :snippet-start: overwrite-embedded-object
// Overwrite the embedded object in a write transaction
realm.write {
// Fetch the parent object
val parentObject: Contact =
realm.query<Contact>("name == 'Nick Riviera'").find().first()

// Overwrite the embedded object (deletes the existing object)
parentObject.address = EmbeddedAddress().apply {
street = "202 Coconut Court"
city = "Los Angeles"
state = "CA"
postalCode = "90210"
}
}
// :snippet-end:

}
asyncCall.cancel()
realm.close()
Realm.deleteRealm(config)
}
}

@Test
fun queryEmbeddedObject() {
Expand Down
Loading

0 comments on commit 9454cdf

Please sign in to comment.