0

I am facing some processing speed issues with my wp8 app.

I have some questions and answers in my post call. I tried with whole question and just ID but it is processed in same time.

if (ansNo.Visibility == Visibility.Visible || ansYes.Visibility == Visibility.Visible)
{
    if (ansNo.IsChecked == true || ansYes.IsChecked == true)
    {
        string token = Globals.token;
        int counter = Globals.counter;
        Globals.counter = Globals.counter + 1;
        quizStatus.Text = "";
        if (ansNo.IsChecked == true)
        {
            values.Add(new KeyValuePair<string, string>(quesId.Text, "f"));
        }
        else
        {
            values.Add(new KeyValuePair<string, string>(quesId.Text, "t"));
        }
        getQuiz(token, Globals.counter);
        ansYes.IsChecked = false;
        ansNo.IsChecked = false;
    }
    else
    {
        quizStatus.Text = "Please Select An Answer";
    }
}
else
{
    calculateType(values);
}   

getquiz gets the next question and quesId holds the id for question and calculatetype is the function where I am facing issues.

Here is calculatetype:

public async Task calculateType(List<KeyValuePair<string, string>> values) 
{
    //quizText.Text = values.ToString();
    quizText.Text = "Calculating Type...";
    nextButton.Visibility = Visibility.Collapsed;
    var httpClient = new HttpClient(new HttpClientHandler());
    HttpResponseMessage response = await httpClient.PostAsync("http://107.170.34.31:3000/calculateusertype", new FormUrlEncodedContent(values));
    response.EnsureSuccessStatusCode();
    var typeResponse = await response.Content.ReadAsStringAsync();
    JToken r = JToken.Parse(typeResponse);


    userResult.Visibility = Visibility.Visible;
    ansYes.Visibility = Visibility.Collapsed;
    ansNo.Visibility = Visibility.Collapsed;
    ContentPanel.Visibility = Visibility.Collapsed;
    userType.Text = r["type"].ToString();
}

here is my getquestion function

public async Task getQuiz(String token,int counter)
    {
        if (counter > 0)
        {
            JToken p = JToken.Parse(Globals.responseString);
            int arryCount = p["questions"].Count() - 1;
            if (counter <= arryCount)
            {
                //quizText.Text = p["questions"][counter]["_id"].ToString();
                quizText.Text = p["questions"][counter]["question"].ToString();
                quesId.Text = p["questions"][counter]["_id"].ToString();
            }
            else 
            {
                quizText.Text = "You Have Completed The Test. Click Next to Get Your Type.";
                ansYes.Visibility = Visibility.Collapsed;
                ansNo.Visibility = Visibility.Collapsed;
            }
        }
        else
        {
            var values = new List<KeyValuePair<string, string>>
                {
                    new KeyValuePair<string, string>("token", token)
                };

            var httpClient = new HttpClient(new HttpClientHandler());
            HttpResponseMessage response = await httpClient.PostAsync("http://107.170.34.31:3000/quiz", new FormUrlEncodedContent(values));
            response.EnsureSuccessStatusCode();
            Globals.responseString = await response.Content.ReadAsStringAsync();

            JToken o = JToken.Parse(Globals.responseString);
            Globals.quizcount = o["questions"].Count();
            Globals.answers = new string[Globals.quizcount];
            //o = JObject.Parse(responseString);
            if (Globals.responseString != "")
            {

                //quizText.Text = o["questions"][counter]["_id"].ToString();
                quizText.Text = o["questions"][counter]["question"].ToString();
                quesId.Text = o["questions"][counter]["_id"].ToString();
            }
        }
    }

Any suggestions to make this faster?

nOmi
  • 297
  • 3
  • 22
  • Are you running this on the UI thread? Are you getting all the questions/answers in one go, or loading them remotely on demand? – Rowland Shaw Mar 05 '14 at 13:12
  • yes i am runing that on ui thread. and loading questions/answer on demand. when user answer the answer is saved in a key value pair after the complete quiz i just pass the values to post call – nOmi Mar 05 '14 at 13:24

1 Answers1

0

I recommend you start off by doing some profiling, to make sure you fix the right problems.

That said, it's most likely a result of hitting the web service on demand. Your web service APIs should be "chunky, not chatty". In other words, try loading all the questions (and answers) when the user starts the quiz.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • i have update my question. i am doing same as u r saying. for first question i just make a call and afterward i just parse the response string from that call. I am getting issue after the quiz in submitting the answers. – nOmi Mar 05 '14 at 14:05