Program crashing and freezing when calling GetAsync from TextChanged event handler

Attempting to make a simple movie searching app using the moviedb api. When I hard code my search term and call GetAsync in the OnAppearing method it works perfectly. When I attempt to call it in a TextChanged event handler and passing the users search string to the query, the app hangs and never gets beyond the call to GetAsync. Here is my code:

``public partial class MovieSearch : ContentPage
{
private HttpClient _client = new HttpClient();
private const string URL = "movie?api_key=9d9cb312367f0f2deaa383f9a8fd64d2&query=";//not the full link

public MovieSearch()
{

    InitializeComponent();
}

async void SearchBar_TextChanged(object sender, Xamarin.Forms.TextChangedEventArgs e)
{
    if (e.NewTextValue == null)
        return;
    if (String.IsNullOrEmpty(e.NewTextValue))
        return;

    var response = await _client.GetAsync(URL + e.NewTextValue);
    var content = await response.Content.ReadAsStringAsync();
    var root = JsonConvert.DeserializeObject<RootObject>(content);
    movieListView.ItemsSource = root.results;
    movieListView.IsVisible = root.results.Any();
    label.IsVisible = !movieListView.IsVisible;
}

}``

Answers

  • shaghayeghshaghayegh USMember ✭✭

    u want to get specific row whit call getasync?

  • ciano1000ciano1000 Member ✭✭

    @shaghayegh said:
    u want to get specific row whit call getasync?

    No, the REST api I'm calling with Get async returns a json string that has a root object and then an array of movies. Below is an example of the json returned. My above method works if its called within OnAppearing instead of SearchText_changed handler, something about SearchText_Changed Handler is blocking my GetAsync call.

    { "page": 1, "total_results": 234, "total_pages": 12, "results": [ { "vote_count": 871, "id": 984, "video": false, "vote_average": 7.5, "title": "Dirty Harry", "popularity": 8.259, "poster_path": "/2TADKc4GMMPdMrLXj0xJVgTvZJM.jpg", "original_language": "en", "original_title": "Dirty Harry", "genre_ids": [ 28, 80, 53 ], "backdrop_path": "/t3OZS8yMs0NL4YgWWc04eXHTn1X.jpg", "adult": false, "overview": "When a madman dubbed 'Scorpio' terrorizes San Francisco, hard-nosed cop, Harry Callahan – famous for his take-no-prisoners approach to law enforcement – is tasked with hunting down the psychopath. Harry eventually collars Scorpio in the process of rescuing a kidnap victim, only to see him walk on technicalities. Now, the maverick detective is determined to nail the maniac himself.", "release_date": "1971-12-21" }, { "vote_count": 440, "id": 25941, "video": false, "vote_average": 6.8, "title": "Harry Brown", "popularity": 7.391, "poster_path": "/68V1ClrCT7ik7UDiAkIDliRaAXf.jpg", "original_language": "en", "original_title": "Harry Brown", "genre_ids": [ 53, 80, 18, 28 ], "backdrop_path": "/szj3uqOGDu8Uwvwo0Si42BCxI04.jpg", "adult": false, "overview": "An elderly ex-serviceman and widower looks to avenge his best friend's murder by doling out his own form of justice.", "release_date": "2009-11-11" },

  • JohnHardmanJohnHardman GBUniversity mod
    edited September 2018

    @ciano1000 - regarding "Program crashing and freezing when calling GetAsync from TextChanged event handler" - can you clarify what actually happens please. Does it crash (throw an unhandled exception) or does it freeze (UI thread blocked, deadlock, infinite loop etc)?

    If it's an unhandled exception (crash), can you provide the exception details.
    If it's an unresponsive UI, does this happen if SearchBar_TextChanged is only called once, and does it happen both when debugging and when executing without breakpoints, stepping etc?

    BTW - in the following code, you do not need to do the first check as the IsNullOrEmpty call also checks for null.

        if (e.NewTextValue == null)
            return;
        if (String.IsNullOrEmpty(e.NewTextValue))
            return;
    
Sign In or Register to comment.