Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOCSP-30345: Update Kotlin SDK's Update page #3059

Merged
merged 3 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Initial draft
  • Loading branch information
cbullinger committed Oct 20, 2023
commit 9454cdf74a4a5bb0d3ddd8eb660aec4b1bcc9021
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