Skip to content

Commit

Permalink
Initial draft
Browse files Browse the repository at this point in the history
  • Loading branch information
cbullinger committed Aug 8, 2023
1 parent c6d39c8 commit c721187
Show file tree
Hide file tree
Showing 4 changed files with 277 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import io.realm.kotlin.Realm
import io.realm.kotlin.RealmConfiguration
import io.realm.kotlin.ext.query
import io.realm.kotlin.ext.realmDictionaryOf
import io.realm.kotlin.ext.realmSetOf
import io.realm.kotlin.internal.platform.runBlocking
import io.realm.kotlin.query.RealmResults
import io.realm.kotlin.types.RealmDictionary
import io.realm.kotlin.types.RealmObject
import io.realm.kotlin.types.RealmSet
import org.mongodb.kbson.ObjectId
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

// :replace-start: {
// "terms": {
Expand All @@ -26,6 +29,17 @@ class CreateTest_Frog : RealmObject {
}
// :snippet-end:

// :snippet-start: define-realm-set
class CreateTest_Author : RealmObject {
var name: String = ""
}
class CreateTest_Book : RealmObject {
var _id: ObjectId = ObjectId()
var title: String = ""
var authors: RealmSet<CreateTest_Author> = realmSetOf()
}
// :snippet-end

class CreateTest: RealmTest() {
@Test
fun createRealmDictionaryType() {
Expand Down Expand Up @@ -106,6 +120,38 @@ class CreateTest: RealmTest() {
realm.close()
}
}

@Test
fun createRealmSet() {
runBlocking {
val config = RealmConfiguration.Builder(
schema = setOf(CreateTest_Book::class, CreateTest_Author::class)
)
.inMemory()
.directory("/tmp/")
.build()
val realm = Realm.open(config)
Log.v("Successfully opened realm: ${realm.configuration.name}")

// :snippet-start: create-set
realm.write {
copyToRealm(CreateTest_Book().apply {
title = "The Hobbit"
authors = realmSetOf()
})
}
realm.write {
val books = query<CreateTest_Book>("$0 == title", "The Hobbit").find().first()
val author = copyToRealm(CreateTest_Author().apply { name = "J.R.R. Tolkien" })
books.authors.add(author)
assertTrue(books.authors.contains(author))
assertEquals(1, books.authors.size)
}

realm.close()
}

}
}

// :replace-end:
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ import io.realm.kotlin.types.annotations.PersistedName
import io.realm.kotlin.types.annotations.PrimaryKey
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.datetime.Instant
import org.mongodb.kbson.ObjectId
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse

// :replace-start: {
// "terms": {
Expand Down Expand Up @@ -222,25 +224,26 @@ class SchemaTest: RealmTest() {
}
}
@Test
@kotlin.test.Ignore // ignored until bugfix in 1.7.1 release
fun createRealmSetTypes() {
runBlocking {
val config = RealmConfiguration.Builder(setOf(Frog2::class, Snack::class))
.inMemory()
.directory("/tmp/")
.build()
val realm = Realm.open(config)
Log.v("Successfully opened realm: ${realm.configuration.path}")

// :snippet-start: add-item-to-realm-set
realm.write {
// Create a Frog object named 'Kermit' that will have a RealmSet of favorite snacks
val frog = this.copyToRealm(Frog2().apply {
val frog = copyToRealm(Frog2().apply {
name = "Kermit"
})
// Get the RealmSet of favorite snacks from the Frog object we just created
val set = frog.favoriteSnacks

// Create a Snack object for the Frog to add to Kermit's favorite snacks
val fliesSnack = this.copyToRealm(Snack().apply {
val fliesSnack = copyToRealm(Snack().apply {
name = "flies"
})

Expand All @@ -252,21 +255,20 @@ class SchemaTest: RealmTest() {

// :snippet-start: add-all-to-realm-set
realm.write {
val myFrog: Frog2 = realm.query<Frog2>("name = 'Kermit'").first().find()!!
val myFrog = query<Frog2>("name = 'Kermit'").first().find()!!
val set = findLatest(myFrog)!!.favoriteSnacks

val cricketsSnack = this.copyToRealm(Snack().apply {
val cricketsSnack = copyToRealm(Snack().apply {
name = "crickets"
})
val earthWormsSnack = this.copyToRealm(Snack().apply {
val earthWormsSnack = copyToRealm(Snack().apply {
name = "earthworms"
})
val waxWormsSnack = this.copyToRealm(Snack().apply {
val waxWormsSnack = copyToRealm(Snack().apply {
name = "waxworms"
})

set.addAll(setOf(cricketsSnack, earthWormsSnack, waxWormsSnack))
println("$set") // :remove:
assertEquals(4, set.size) // :remove:
// :uncomment-start:
//}
Expand All @@ -283,14 +285,23 @@ class SchemaTest: RealmTest() {
// :snippet-end:

// :snippet-start: remove-item-from-set
val fliesSnack = realm.query<Snack>("name = 'flies'").first().find()
val fliesSnack = query<Snack>("name = 'flies'").first().find()

set.remove(fliesSnack)
// :snippet-end:
assertFalse(set.contains(fliesSnack))
assertEquals(3, set.size)

}
delay(1000)
realm.write {
val myFrog: Frog2 = query<Frog2>("name = 'Kermit'").find().first()
val set = myFrog.favoriteSnacks
// :snippet-start: remove-multiple-items-from-set
set.removeAll(set)
set.removeAll(myFrog.favoriteSnacks)
// :snippet-end:
// test set is empty
assertEquals(0, set.size)
}


Expand All @@ -301,6 +312,7 @@ class SchemaTest: RealmTest() {
?.asFlow()
?.collect() {
// Listen for changes to the RealmSet

}
}
// :snippet-end:
Expand All @@ -309,5 +321,53 @@ class SchemaTest: RealmTest() {
Realm.deleteRealm(config)
}
}

@Test
fun createRealmSetTypes2() {
runBlocking {
val config = RealmConfiguration.Builder(setOf(Frog2::class, Snack::class))
.directory("/tmp/")
.inMemory()
.build()
val realm = Realm.open(config)
Log.v("Successfully opened realm: ${realm.configuration.path}")

// :snippet-start: add-item-to-realm-set
realm.write {
// Create a Frog object named 'Kermit' that will have a RealmSet of favorite snacks
val frog = copyToRealm(Frog2().apply {
name = "Kermit"
})
// Get the RealmSet of favorite snacks from the Frog object we just created
val set = frog.favoriteSnacks
set.removeAll(set)

// Create a Snack object for the Frog to add to Kermit's favorite snacks
val fliesSnack = copyToRealm(Snack().apply {
name = "flies"
})
val cricketsSnack = copyToRealm(Snack().apply {
name = "crickets"
})
val earthWormsSnack = copyToRealm(Snack().apply {
name = "earthworms"
})
val waxWormsSnack = copyToRealm(Snack().apply {
name = "waxworms"
})

set.addAll(setOf(fliesSnack, cricketsSnack, earthWormsSnack, waxWormsSnack))

// :snippet-start: remove-multiple-items-from-set
set.removeAll(set)
// :snippet-end:
// test set is empty
assertEquals(0, set.size)
}

realm.close()
Realm.deleteRealm(config)
}
}
}
// :replace-end:
1 change: 1 addition & 0 deletions source/sdk/kotlin/sync.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Device Sync - Kotlin SDK
Handle Sync Errors </sdk/kotlin/sync/handle-sync-errors>
Set the Client Log Level </sdk/kotlin/sync/log-level>
Stream Data to Atlas </sdk/kotlin/sync/stream-data-to-atlas>
Sync Data in the Background </sdk/kotlin/sync/background-sync>
Partition-Based Sync </sdk/kotlin/sync/partition-based-sync>

.. contents:: On this page
Expand Down
Loading

0 comments on commit c721187

Please sign in to comment.