Client.GetAsync does not update my data

I am developing an au Universal application using MVVM-Light .

The page has a list of comments coming from WebService. If the current user is the author of the comment, I display FlyoutMenu, letting him “Edit” or “Delete” my comment. There is also an AppBarButton for adding a new comment:

enter image description here

My problem is that comments are never updated after the first load of this page ...

I use the LoadComments () method in ViewModel , which allows me to get comments when I come to the page, but also after editing, deleting or adding an element:

private async void LoadComments()
{
    List<Comment> commentsWS = await WebServiceGetCommentList();
    if (commentsWS != null)
        Comments = new ObservableCollection<Commentaire>(commentsWS);
}

This method calls another method, " WebServiceGetCommentList () ", which prepares a call to the WebService in the same ViewModel :

private async Task<List<Comment>> WebServiceGetCommentList()
{
    // Parameters
    List<KeyValuePair<string, string>> parametres = new List<KeyValuePair<string, string>>();
    parametres.Add(new KeyValuePair<string, string>("option", _currentUserAccount.option));
    parametres.Add(new KeyValuePair<string, string>("id_article", Article.id_article.ToString()));

    // Call WebService and deserialize
    Exception custEx = null;
    try
    {
        List<Comment> comments = await WebServices.GetCommentList(_currentUserAccount.url, parametres, "");
        return comments;
    }
    // Exceptions 
    catch (Exception e)
    {
        ...

    }
    return null;
}

Then I go to the GetComments () method in the WebServices class :

public static async Task<List<Comment>> GetCommentList(String url, List<KeyValuePair<String, String>> parametres, String protocol)
{
    // Call WebService and deserialize
    var response = await JSONParser.getJSONFromUrl(url, parametres, "");
    List<Comment> comments = new List<Comment>();
    WsResponse wsResponse = ManageWsReponse(response, Constants.WebServiceTask.GetCommentList.Value);
    try
    {
        WsResponseResult wsResponseResult = JsonConvert.DeserializeObject<WsResponseResult>(wsResponse.data.ToString());
        comments = JsonConvert.DeserializeObject<List<Comment>>(wsResponseResult.result.ToString());
        return comments;
    }
    catch (Exception e)
    {
        throw new DeserializeException("Deserialize exception", e, DateTime.Now, "Comment");
    }
}

This method calls the getJSONFromUrl () method in the JSONParser class , which runs " client.GetAsync ()

public static async Task<string> getJSONFromUrl(String url, List<KeyValuePair<String, String>> parameters, String protocol)
{
    var client = new HttpClient();

    // Preparing URI
    string sParameters = null;
    int i = 1;
    foreach (var param in parameters)
    {
        sParameters += param.Key + "=" + param.Value;
        sParameters += i != parameters.Count ? "&" : string.Empty;
        i++;
    }
    var uri = new Uri(url + "?" + sParameters);

    // Calls the WebService
    var response = await client.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead);

    // Code and results
    var statusCode = response.StatusCode;
    // EnsureSuccessStatusCode throws exception if not HTTP 200
    response.EnsureSuccessStatusCode();
    // responseText
    var responseText = await response.Content.ReadAsStringAsync();
    return responseText;
}

I can add, delete, or edit the comment with success, but when I return to this " LoadComments () " method , the changes are not taken into account, and I get the same thing as when I first called ...

GetJSONFromURL(), , var.

, URI , WebService .

= > , client.GetAsync(), , , ...

httpclient-caching, .

, ,

+4
1

. Cache-Control, - , .

- . , .

parametres.Add(new KeyValuePair<string, string>("mytmstmp", DateTime.Now.Ticks);

. , . :

"If-Modified-Since", DateTime.UtcNow.ToString(CultureInfo.InvariantCulture)
+2

Source: https://habr.com/ru/post/1609765/


All Articles