Skip to main content

Setup development

Development Environment Requirements

  • SDK: Android API 26 or higher
  • JDK: JDK 11 or higher
  • IDE: Android Studio
  • AVD: Pleos Connect Emulator
info

Refer to the guide on installing the Pleos Connect Emulator at Setup Pleos Connect Emulator.

Project Setup

SDK Dependency Setup

  1. Add the following to the project-level settings.gradle.
allprojects {
repositories {
google()
mavenCentral()
maven(url="https://connect-nexus.developers.umos.ai/repository/maven-releases")
}
}
  1. Add Sync SDK library dependency to the app-level gradle file.
dependencies {
// Add the dependency for the Sync SDK library
implementation("ai.pleos.sync:syncSDK:2.1.0")
}

Application Permission Declaration

The following permissions are required to use the Sync SDK.

AndroidManifest.xml
<manifest>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="pleos.car.permission.SYNC_DATA" />
</manifest>

Initialization

  1. Create SyncConfigsProvider. SyncConfigsProvider configures connection and authentication information based on the config file located in the project's assets folder.
  2. Initialize SyncDatabase using initialize(), and register DatabaseListener for asynchronous initialization.
  3. Once DatabaseListener executes and InitializationState becomes isReady, you can use SyncDatabase.
val configsProvider = SyncConfigsProvider.createFromConfigFile(applicationContext).getOrThrow()

SyncDatabase.initialize(application, configsProvider) {
if (it.isReady()) {
syncDatabase = SyncDatabase.getInstance("databaseName")
} else {
//TODO: handle failure
}
}
info

Database 생성 요청

Playground > Select Project > API & SDK > Sync SDK > Apply for database

info

SyncConfigsProvider 사용 방법

After applying for the database, download the configuration file and add it to the project's assets folder. Place the downloaded configuration file in the assets folder of the project.
SyncConfigsProvider created using createFromConfigFile() reads it and configures appropriate connection and authentication information.

Fetching Document

To read and write data in the database, you need to create a document object through a SyncDatabase instance. At this time, you must use the getInstance() function, and the database name must match the name created in Playground.

val database = SyncDatabase.getInstance("databaseName")
val document = syncDatabase.document("documentName")

Reading Data from the Database

Data can be read using the value() helper method of the Document. For example, to deserialize data into a custom structure, you can use it as follows.

[Document data]
{
"name": "developer",
"age": 20,
"isMale": true,
"company": {
"name": "42dot",
"address": "anywhere on earth",
"wanted": [
{
"role": "Front-end",
"count": 10
},
{
"role": "Back-end",
"count": 10
}
]
}
}
data class Wanted(val role:String, val count:Int)
data class Company(val name:String, val address:String, val wanted:List<Wanted>?)
data class FullUserProfile(val name:String, val age: Int, val isMale: Boolean, val company:Company?)

val fullProfile: FullUserProfile? = appDoc?.value()

To receive remote data or handle real-time data, register a DocumentListener using addDocumentListener().

appDoc.addDocumentListener(listener : DocumentListener {
override fun onDocumentChanged(document: SyncDocument, fromRemote: Boolean) {
val fullProfile: FullUserProfile? = document.value()
println("latest user profile : $fullProfile")
}

override fun onDocumentError(document: SyncDocument, err: SyncError) {
// TODO: Handle data sync error
}
})

Saving Data to the Database

You can add or modify data in the desired format through the Document's setKeyValue() method.

//Update field value
appDoc?.setKeyValue("Mr.Famous", "name")?.onFailure { e ->
//TODO: handle error
}
//Insert a new field
appDoc?.setKeyValue("I'm a newbie", "new")?.onFailure { e ->
//TODO: handle error
}
//Insert a new object
appDoc?.setKeyValue(mapOf("hello" to "world"), "newObject")?.onFailure { e ->
//TODO: handle error
}
//Insert a new array
appDoc?.setKeyValue(listOf(1, 2, 3), "newArray")?.onFailure { e ->
//TODO: handle error
}

If you specify the path of the subnode in the key of setKeyValue(), you can modify the data of the subnode.

//Update field value
appDoc?.setKeyValue("NewCompany", "company.name")?.onFailure { e ->
//TODO: handle error
}
//Insert a new field
appDoc?.setKeyValue(2019, "company.since")?.onFailure { e ->
//TODO: handle error
}