Skip to content

Real Time Database

Efra Espada edited this page Jun 6, 2018 · 1 revision

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)
               
      }
)

onCreate

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);
}

onChanged

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
}

onUpdate

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;
}

onDestroy

Called when remove method is invoked. Reference is removed on database too.

@Override
public void onDestroy() {
    objectA = null;
    // UI changes for removed object
}

progress

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");
Clone this wiki locally