-
Notifications
You must be signed in to change notification settings - Fork 0
Real Time Database
Database allows devices to work with the same objects by listening the same path
. When an object is listened, the library says to Rotor server your device is waiting for changes on that path
, so every time any device makes a change on that (object), the differences are calculated and replicated on all devices listening.
Database works with Java objects.
class ObjectA {
@SerializedName("value")
@Expose
String value;
public ObjectA() {
// nothing to do here
}
public ObjectA(String value) {
this.value = value;
}
public void setValue(String value) {
this.value = vaue;
}
public void getValue() {
return value;
}
}
listen(database: String, path: String, object: Reference<T>(T::class.java))
method has a simple object lifecycle interface which controls every object state.
// kotlin
var objectA: ObjectA ? = null
Database.listen("database", // database name
"/myObjects/objectA", // object path
Reference<ObjectA>(ObjectA::class.java) { // reference lifecycle
fun onCreate()
fun onUpdate(): ObjectA ?
fun onChanged(ref: ObjectA)
fun onDestroy()
fun progress(value: Int)
}
)
Called when object is not created in remote DB yet. Object is defined and synchronized with server here. This method won't be called if object already exists on server, onChange
method will be called instead.
// java
@Override
public void onCreate() {
objectA = new ObjectA("foo");
Database.sync(path);
}
Called in two situations, when some device has made changes on the same object and when listen
method is called and the object is cached. Database library pass the object up to date as parameter.
@Override
public void onChanged(ObjectA objectA) {
this.objectA = objectA;
// notify change on UI
}
Called when sync
method is invoked. Differences with the last "fresh" object passed by library are calculated and sent to server.
@Override
public ObjectA onUpdate() {
return objectA;
}
Called when remove
method is invoked. Reference is removed on database too.
@Override
public void onDestroy() {
objectA = null;
// UI changes for removed object
}
Some object updates can become too big, so server slices updates and sends them sequentially. value parameter goes from 0 to 100
@Override
public void progress(int value) {
Log.e(TAG, "loading " + path + " : " + value + " %");
}
After work with objects, changes must be synchronized with servers. Call sync(path: String)
method to sync it.
Database.sync("/myObjects/objectA");
Remove listener in server by calling unlisten(path: String)
.
Database.unlisten("/myObjects/objectA");
Remove reference in database by calling remove(path: String)
. onDestroy
will be called.
Database.remove("/myObjects/objectA");
Copyright 2018 RotorLab Organization
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.