Async - a bit confused. Is this correct?

I am doing calling a WebService to sync data (for this discussion to get data). The WebService then updates the local SQLite DB.

The WebService call is actually 2 calls. One to get the data and one to Acknowledge that we got it.

There is a ListView that shows the data from the DB (Android Activity, but will have iOS list view too).

This is all working, but I am not sure it is the "correct" approach. In the past, I would have spawned a new thread and run DoSync() on the thread.

The Activity with the ListView.

`
private void DoSyncAsync()
{
try
{
WebService.SyncGetAsync(DoneAsyncCall);
}
catch (Exception ex)
{
ShowAllertDlg(ex.Message);
}
}

    void DoneAsyncCall(string result)
    {
        students = new List<Student>(DataRepository.Student_List());
        studentListAdapter.students = students;  

        RunOnUiThread(new Action(() =>
        {
            studentListAdapter.NotifyDataSetChanged();
        }));
    }

`

The WebService Call. Details omitted for readability. *Note, this is in a shared project.
`
public static void SyncGetAsync(Action callback)
{

        var client = new RestClient(_server);

        var request = new RestRequest("{auth}/syncGet", Method.POST);
        request.RequestFormat = DataFormat.Json;
        request.AddUrlSegment("auth", _authToken.ToString());

        client.ExecuteAsync<LSISyncDataModel>(request, (response) =>
            {
            // ***  Deal with the response and save to DB.  ***

                    // Now We send the Ack to the server.

                    request = new RestRequest("{auth}/syncAck", Method.POST);
                    request.RequestFormat = DataFormat.Json;
                    request.AddUrlSegment("auth", _authToken.ToString());

                    client.ExecuteAsync<RestResponse>(request, (resp) =>
                        {
                            callback("done!");
                        });

            });

    }

`

Posts

  • BenLevy.4265BenLevy.4265 USMember ✭✭

    Sorry, could not get code to format properly. Will post each section

  • BenLevy.4265BenLevy.4265 USMember ✭✭

    private void DoSyncAsync() { try { WebService.SyncGetAsync(DoneAsyncCall); } catch (Exception ex) { ShowAllertDlg(ex.Message); } }

  • BenLevy.4265BenLevy.4265 USMember ✭✭

    yea.... can't seem to get the code to format in here. If anyone needs, I can attach a text file.
    `

Sign In or Register to comment.