Skip to main content

SyncDocument

SyncDatabase stores a logical document object. When the data is changed by the user, it is automatically saved to SyncDatabase with the given name.

Summary

Public methods
String
key()

Returns the path of the current node.

SyncDocument
child(key: String)

Returns the child node with the given name.

SyncDocument
paths(vararg key: String)

Returns the child node of the given path based on the current node.

T?

<T> value(clazz: Class<T>)

Returns the data of the current node deserialized into the given type.

ResultCompat<Boolean>

setKeyValue(value:Any, key: String? = null)

Sets the given value to the specified child node or the current node.

ResultCompat<Boolean>

unsetKeyValue(key: String)

Deletes the item at the given path in the current node.

ResultCompat<Boolean>

insertToArray(pos: Int , vararg value: Object)

If the current node is an array, adds the given items at the specified position.

ResultCompat<Boolean>

appendToArray(vararg value: Object)

If the current node is an array, adds the given item at the end.

ResultCompat<Boolean>

updateInArray(pos: Int , vararg value: Object)

If the current node is an array, deletes the specified number of items from the given position.

ResultCompat<Boolean>

deleteInArray(pos: Int, count: Int = 1)

If the current node is an array, deletes the specified number of items from the given position.

void
addDocumentEventListener(listener: DocumentEventListener)

Registers a callback to receive document data change and error events.

void
removeDocumentEventListener(listener: DocumentEventListener)

Removes the callback from the current document.

Public properties
SyncDatabase
val database: SyncDatabase

Returns the singleton object SyncDatabase.

SyncDocument?

val root: SyncDocument?

Returns the top-level node.

SyncDocument?

val parent: SyncDocument?

Returns the parent node.

String?
val jsonString: String?

Returns the serialized JSON string.

caution

SyncDocument is automatically managed by SyncDatabase. Do not call the constructor directly.


Public methods

key()
fun key(): String

Returns the path of the current node.

Returns

String

Path of the current node

child()
fun child(key: String): SyncDocument

Returns the child node with the given name.

Parameters

String

Child node name
Returns
SyncDocumentChild node

paths()
fun paths(vararg key: String): SyncDocument

Returns the child node of the given path based on the current node.

Parameters

vararg String

Array of child node paths
Returns
SyncDocumentChild node

value()
fun <T> value(clazz: Class<T>): T?

Returns the data of the current node deserialized into the given type.

Parameters

Class<T>

POJO class to serialize
Returns

T?

Deserialized object
info

If some variables of the POJO class cannot be found in the serialized JSON string, null is returned.


setKeyValue()
fun setKeyValue(value: Object, key: String = null): ResultCompat<Boolean>

Sets the given value to the specified child node or the current node.

Parameters

@Nullable Object

Set up data

String

Child node path
Returns

ResultCompat<Boolean>

ResultCompat

unsetKeyValue()
fun unsetKeyValue(key: String): ResultCompat<Boolean>

Deletes the item at the specified path from the current node.

Parameters
StringChild node path
Returns

ResultCompat<Boolean>

ResultCompat

insertToArray()
fun insertToArray(pos: Int, vararg value: Object): ResultCompat<Boolean>

If the current node is an array, adds the given items at the specified position.

Parameters

Int

Position to add

vararg Object

Array of values to add
Returns

ResultCompat<Boolean>

ResultCompat

updateInArray()
fun updateInArray(pos: Int, vararg value: Object): ResultCompat<Boolean>

If the current node is an array, overwrites the given items at the specified position.

Parameters

Int

Position to overwrite

vararg Object

Array of values to overwrite
Returns

ResultCompat<Boolean>

ResultCompat

deleteInArray()
fun deleteInArray(pos: Int, count: Int = 1): ResultCompat<Boolean>

If the current node is an array, removes the specified number of items starting from the given position.

Parameters

Int

Position to remove

Int

Number of items to remove
Returns

ResultCompat<Boolean>

ResultCompat

addDocumentEventListener()
fun addDocumentEventListener(listener: DocumentEventListener)

Registers a callback to receive document data change and error events.

Parameters
DocumentEventListenerDocumentEventListener

removeDocumentEventListener()
fun removeDocumentEventListener(listener: DocumentEventListener)

Removes the callback from the current document.

Parameters
DocumentEventListenerDocumentEventListener