denley / courier

A delivery service for Android Wear. Courier uses the DataApi and MessageApi to deliver objects between devices simply and cleanly.
Apache License 2.0
80 stars 5 forks source link

Using Delete Data.. Facing Issue #25

Closed ccrazycoder closed 9 years ago

ccrazycoder commented 9 years ago

I've set receiver for Data Api in My Activity as listed.

Courier.deleteData(AppointmentListActivity.this, Constants.GET_APPOINTMENT);
Courier.startReceiving(AppointmentListActivity.this);
Courier.deliverMessage(AppointmentListActivity.this, Constants.GET_APPOINTMENT, pageindex);

Logic after Data received is written as below

@ReceiveData(Constants.GET_APPOINTMENT)
    void onDataReceived(ArrayData objResponse, String nodeId) { 
// All business logic is written here.
}

Issue is if I'm writing delete data line my receiver doesn't work once I destroy the activity and navigating to the same again. I wants to use deleteData as it doesn't gives me updated data on onDataReceived first time. it gives me updated data on second time.

Code on Destroy Activity

 @Override
    protected void onDestroy() {
        super.onDestroy();
        Courier.stopReceiving(this);
    }
denley commented 9 years ago

It's not entirely clear to me what the issue it.

In your example I think that the onDataReceived method should never get called when you initialise (with Courier.startReceiving), since the data is deleted just before the initialization.

Your call to Courier.deliverMessage will not cause the onDataReceived method to be called because "Data" and "Messages" are two separate streams. You can annotate a method with @ReceiveMessages(Constants.GET_APPOINTMENT) to receive callbacks from Courier.deliverMessage events

ccrazycoder commented 9 years ago

Ultimately What I want is to get Updated Data identifier.. I want listed method to be called only if there is any updated data in the Data channel.. or any Identifier that I can get..

The issue is that when I am launching the Activity and call Courier.startReceiving(AppointmentListActivity.this); my @ReceiveData is called automatically. and its called again with updated data after I'll finish my webservice all on handheld device.

denley commented 9 years ago

Thanks for the clarification. I understand the issue now, that the @ReceiveData annotated method is being called even when no data exists. I'll look into it.

denley commented 9 years ago

I've had a look into it, and I think that the library is working properly. It doesn't call the @ReceiveData annotated methods when there is no data in the path.

The issue you are seeing is occurring because Courier.deleteData occurs asynchronously, so the data item is not yet deleted by the time the Courier.startReceiving method is called. This is unavoidable, since the DataApi.deleteDataItems method in the Google Play Services API needs to be called off the main thread.

I suggest you restructure you code like so, to 'consume' the data items as they are received rather than when initially loading the page (keep in mind, though, that deleting the data item will cause the method to be invoked again, with null values):

    @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // ...

        Courier.startReceiving(AppointmentListActivity.this);
        Courier.deliverMessage(AppointmentListActivity.this, Constants.GET_APPOINTMENT, pageindex);
    }

    @Override protected void onDestroy() {
        super.onDestroy();
        Courier.stopReceiving(this);
    }

    @ReceiveData(Constants.GET_APPOINTMENT)
    void onDataReceived(ArrayData objResponse, String nodeId) {
        if(isNull(objResponse)) return;

        // ... (business logic)

        // Consume the data item
        Courier.deleteData(AppointmentListActivity.this, Constants.GET_APPOINTMENT);
    }