O
O
Oleg2017-02-04 16:52:27
Multithreading
Oleg, 2017-02-04 16:52:27

How to properly organize the application (interactions between threads and UI)?

Good afternoon. I ask for help in organizing the application. The application should display the value of the tags received from the OPC-DA server, in real time in a simple list (tag name - current value). Now done through IntentService and BroadcastReceiver. The problem of passing values ​​from the service to the ListView, now there are about 100 values..
By clicking on the button, the IntentService is called and data is received in it.. Below is the onHandleIntent code:

protected void onHandleIntent(Intent workIntent) {
        // Gets data from the incoming Intent
        String dataString = workIntent.getDataString();
        try{
            server.connect();
            Group group = server.addGroup("root");
            branches = new ArrayList<Branch>(server.getTreeBrowser().browse().getBranches().size());
            getBranchTree(server.getTreeBrowser().browse());
            tags = new ArrayList<Leaf>();
            for (Branch b: branches){
                for(Leaf l:b.getLeaves()){
                    tags.add(l);
                }
            }
            Log.i(LOG_TAG,"TAGS CREATED/ WE CAN READ VALUES FROM SERVER");
            subscribe();

        }catch(Exception ex){
            Log.e(LOG_TAG,ex.getMessage());
        }
    }

///////////////////////////////////////////////
void subscribe() {
        if (access != null) {
            try {
                access.unbind();
            } catch (Exception ex) {
                Log.w(LOG_TAG, ex.getMessage(), ex);
            } finally {
                access = null;
            }
        }
        try {
            access = new SyncAccess(server, 1000);
            for(Leaf l: tags){
                access.addItem(l.getItemId(), new DataCallback() {
                    ItemObject ItemObj = new ItemObject();
                    public void changed(final Item item, final ItemState itemState) {
                        // посылаем промежуточные данные
                        Intent updateIntent = new Intent();
                        updateIntent.setAction("ACTION_UPDATE");
                        updateIntent.addCategory(Intent.CATEGORY_DEFAULT);
                        //JIVariant jVar = itemState.getValue();
                        try{
                            ItemObj.ItemId = "awdasd";//item.getId().toString();
                            ItemObj.ItemQuality = 192;//itemState.getQuality().intValue();
                            ItemObj.ItemTimeStamp = "adas";//itemState.getTimestamp().toString();
                            ItemObj.ItemValue = "value";//itemState.getValue().toString();
                            updateIntent.putExtra("ACTION_UPDATE_OUT", ItemObj);
                        }
                        catch(Exception ex){
                            Log.e(LOG_TAG,ex.getMessage());
                        }
                        sendBroadcast(updateIntent);

                        if (itemState.getQuality ()==192) {
                          //  responseIntent.putExtra(EXTRA_KEY_OUT, extraOut);
                            System.out.println(String.format("Item: %s, Value: %s, Timestamp: %tc, Quality: %d", item.getId(), itemState.getValue(), itemState.getTimestamp(), itemState.getQuality()));
                            Log.i(LOG_TAG, itemState.getValue().toString());
                        }
                    }
                });
            }
            access.bind();

        } catch (Exception ex) {
            Log.w(LOG_TAG, ex.getMessage(), ex);
        }
    }

in the subscribe function, I receive data: I pass it through ItemObj(Parcelable)/ I would like to transfer not only data of the main type, but also others (name - string and value - JIVariant ..). How can such a transfer be organized?
In this way, one tag is transmitted
Code in BroadcastReceiver
public class UpdateBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            //int update = intent.getIntExtra("ACTION_UPDATE_OUT", 0);
            ItemObject Item = (ItemObject)intent.getParcelableExtra("ACTION_UPDATE_OUT");
            Tag t = new Tag(Item.ItemId,Item.ItemValue,Item.ItemQuality);
            tags.add(t);
            tagAdapter.notifyDataSetChanged();
        }
    }

Is it possible to pass the entire list to be displayed in a ListView?
Perhaps there are other ways to work after receiving data from the server?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmtm, 2017-02-04
@Dmtm

How can such a transfer be organized?

a data layer independent of the UI (if fast, then variables in the Application) and no Parсelable is needed,
only a change notification through the broadcast, server responses are written in the Application, the
consumer receives a notification and takes them there, if it does not receive, but has just been created - still takes them there
PS: I would use just a Thread with an infinite loop for requests and an EventBus for notifications
PPS: ListView is dead, only RecycledView
PPPS: in theory, where is the real time - is there a socket? not requests

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question