Avatar

Please consider registering
guest

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —




— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

sp_Feed Topic RSS sp_TopicIcon
Android Binding, LiveData and datachangelistener
April 29, 2019
14:25, EEST
Avatar
hcvoort
Member
Members
Forum Posts: 5
Member Since:
June 21, 2016
sp_UserOfflineSmall Offline

Dear all,

I am using the Prosys library for quit some time now with great satisfaction. Recently I switched to using LiveData (Android architecture components) to use viewmodels and separate business logic from UI. It appears that if I call the “viewmodel.setVariable()” from within the onDataChange (MonitoredDataItemListener) the viewmodel.setVariable() is called but the change in value is never observed. Some code to explain:

public class OpcUaViewModel extends ViewModel {

private MutableLiveData integratorOutput;

public MutableLiveData getIntegratorOutput() {

if (integratorOutput == null) {
integratorOutput = new MutableLiveData();
setIntegratorOutput(0.0);
}

return integratorOutput;
}

public void setIntegratorOutput(Double d){

integratorOutput.setValue(d);

}

}

This OpcUaViewModel object is declared in OnCreate as

OpcUaViewModel viewModel = ViewModelProviders.of(this).get(OpcUaViewModel.class);

Then in onDataChange(MonitoredDataItem monitoredDataItem, DataValue dataValue, final DataValue dataValue1) the following is called:
viewModel.setIntegratorOutput(dataValue1.getValue().doubleValue());

Hope somebody can point me in the right direction on this!

Carsten

April 29, 2019
15:02, EEST
Avatar
Bjarne Boström
Moderator
Moderators
Forum Posts: 983
Member Since:
April 3, 2012
sp_UserOfflineSmall Offline

Hi,

I have one idea, but that’s about it. I would consult the documentation of LiveData.

Generally all UI toolkits are single-threaded and all updates must be done in the context of a “Platform” Thread.

The onDataChange is called on the PublishTask Thread of the UaClient, therefore at some point before updating the UI typically there would be a “PlatformThread”.runLater/invokeLater etc. call.

The documentaion of the LiveData would say this (so it would seem all updated must be done in the main thread):
https://developer.android.com/topic/libraries/architecture/livedata#update_livedata_objects

“Note: You must call the setValue(T) method to update the LiveData object from the main thread. If the code is executed in a worker thread, you can use the postValue(T) method instead to update the LiveData object.”

April 29, 2019
15:10, EEST
Avatar
hcvoort
Member
Members
Forum Posts: 5
Member Since:
June 21, 2016
sp_UserOfflineSmall Offline

Bjarne,

Your great! postValue solved the issue. I already figured it must be a thread issue, but did not thought about postValue().
Thanks alot 🙂

Carsten

Forum Timezone: Europe/Helsinki

Most Users Ever Online: 518

Currently Online:
13 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

hbrackel: 135

pramanj: 86

Francesco Zambon: 81

rocket science: 77

ibrahim: 75

Sabari: 62

kapsl: 57

gjevremovic: 49

Xavier: 43

fred: 41

Member Stats:

Guest Posters: 0

Members: 680

Moderators: 16

Admins: 1

Forum Stats:

Groups: 3

Forums: 15

Topics: 1467

Posts: 6259

Newest Members:

sagarchau, elviralangwell4, Donnavek, Eddiefauth, DonaldPooma, fidelduke938316, Jan-Pfizer, DavidROunc, fen.pang@woodside.com, aytule

Moderators: Jouni Aro: 1009, Otso Palonen: 32, Tuomas Hiltunen: 5, Pyry: 1, Petri: 0, Bjarne Boström: 983, Heikki Tahvanainen: 402, Jukka Asikainen: 1, moldzh08: 0, Jimmy Ni: 26, Teppo Uimonen: 21, Markus Johansson: 42, Niklas Nurminen: 0, Matti Siponen: 321, Lusetti: 0, Ari-Pekka Soikkeli: 5

Administrators: admin: 1