Support Topics Documentation Slack YouTube Blog

How to output relationships data when updated in real time?

I am making an android application. And I need to update data in real time with relationships. How can i do this? In the backend, the column with relations was removed from the checkbox.

class AlarmBackendlessHelpers {
private val alarmData: IDataStore = Backendless.Data.of(Alarm::class.java)

var alarmList: ArrayList<Alarm> = ArrayList()
private val rtHandlers: EventHandler<Alarm> = alarmData.rt()

var ownerId: String = UserIdStorageFactory.instance().storage.get()


fun enableRealTime(
    adapter: AlarmsRecyclerViewAdapter
) {
    rtHandlers.addCreateListener("ownerId = '$ownerId'", object : AsyncCallback<Alarm> {
        override fun handleResponse(response: Alarm?) {
            if (response != null) {
                alarmList.add(response)
                adapter.setArrayList(alarmList)
                Log.e("back", response.toString())
            }
        }

        override fun handleFault(fault: BackendlessFault?) {
            Log.e("back", fault!!.message)
        }
    })
    rtHandlers.addUpdateListener("ownerId = '$ownerId'", object : AsyncCallback<Alarm> {
        override fun handleResponse(response: Alarm?) {
            for (i in alarmList.indices) {
                if (alarmList[i].objectId == response?.objectId) {
                    if (response != null) {
                        alarmList[i] = response
                    }
                }
            }
            adapter.setArrayList(alarmList)
            Log.e("back", response!!.toString())
        }

        override fun handleFault(fault: BackendlessFault?) {
            Log.e("back", fault!!.message)
        }
    })

    rtHandlers.addDeleteListener("ownerId = '$ownerId'", object : AsyncCallback<Alarm> {
        override fun handleResponse(response: Alarm?) {
            val iterator: MutableIterator<*> = alarmList.iterator()
            while (iterator.hasNext()) {
                val alarm: Alarm = iterator.next() as Alarm
                if (alarm.objectId == response?.objectId) {
                    iterator.remove()
                }
            }
            Log.e("back", response!!.toString())
            adapter.setArrayList(alarmList)
        }

        override fun handleFault(fault: BackendlessFault?) {
            Log.e("back", fault!!.message)
        }
    })
}

fun getAlarm(
    adapter: AlarmsRecyclerViewAdapter
) {
    val queryBuilder = DataQueryBuilder.create()
    queryBuilder.setPageSize(100)
    queryBuilder.setSortBy("created")
    queryBuilder.addRelated("groupUsers")
    queryBuilder.whereClause = "ownerId = '$ownerId'"
    alarmData.find(queryBuilder, object : AsyncCallback<List<Alarm>> {
        override fun handleResponse(response: List<Alarm>?) {
            // pBar.progressBar(false, mBinding.progressBar, mBinding.alarmListView)
            if (response != null) {
                alarmList = ArrayList(response)
                adapter.setArrayList(alarmList)
                Log.e("back", response.toString())
            }
        }

        override fun handleFault(fault: BackendlessFault?) {

        }
    })
}

fun getAlarmRing(
    objectId: String
): Alarm {
    var mAlarm = Alarm()
    alarmData.findById(objectId, object : AsyncCallback<Alarm> {
        override fun handleResponse(response: Alarm?) {
            // pBar.progressBar(false, mBinding.progressBar, mBinding.alarmListView)
            if (response != null) {
                mAlarm = response
                Log.e("back", response.toString())
            }
        }

        override fun handleFault(fault: BackendlessFault?) {
        }
    })
    return mAlarm
}

}

Hi @Vital_Yarosh

Can you please specify what exactly do you want to achive?

The auto load function was removed. Now if you want to retrieve the objects with relations you should read about Single Step or Two Steps Retrieval.

If you want to update the relations, take a look at this documentation:

Best Regards,
Maksym

When i use addUpdateListener method the data comes in no relationship as there is no whereClause parameter. How can i fix this?

Yes, it’s not available. Instead you should use Two Steps Retrieval to retrieve the relations for the object you receive inside the listener.