locked
Best way to remove items from a huge list? RRS feed

  • Question

  • I have a quiz app which loads around 3000-5000 questions to a list from the "Lex.Db" database and sets a next question from element (0) and removes the same question at element (0) after completing the question. It goes till the end..

         I'm saving the (int)question no. in settings, whenever the user navigates away from the quiz page, now when navigated back to the page I want to remove those number of (int question no) from the new list.

    What is the easy method to achieve the above requirement?

    1. How will be the performance of the app if I use list.RemoveRange(index, count)? or Should I use another method?

    2. How will be the performance of the app if I load next question directly from the database without removing any elements?
        but maybe I have to load the database for every next question.

    Any Help?

    Dr.R.VenuKumar

    Thursday, February 6, 2014 6:29 PM

Answers

  • well there are ways to do it, the database load would not be that costly.

    you can load 20 questions first and when they reach the 10th mark, you will load 20-30, so that you always are 10 ahead. Maybe in an async manner.

    Loading small amount of data should be very fast for a database, the only thing that would matter is the network latency if you have any.

    • Proposed as answer by Khanna Gaurav Saturday, February 8, 2014 6:49 AM
    • Marked as answer by venu238 Saturday, February 8, 2014 9:57 AM
    Thursday, February 6, 2014 7:54 PM
  • I have finally decided on how to remove the items after resuming the app..

    I'll load the database by using skip((int)question no.), so that the previous questions will be removed out of the list easily.

    Thank you :)


    Dr.R.VenuKumar

    • Marked as answer by venu238 Friday, February 7, 2014 6:49 PM
    Friday, February 7, 2014 6:43 PM
  • I'm in a bit confusion on how to implement above step,  say I have 1000 qns then after completion of every 10 qns I have to load next 10 qns..than how should I resume back?

    Dr.R.VenuKumar

    You can have a count integer variable. When Next button is clicked to show next question you can initialize the count variable and when count reach 10 you can load the questions from database and reset the count variable.

    Also after every questions is answered, you can saved the number in IsolatedStorageSettings. And when you next start your application or resume you can read the last saved number and load the questions from there.

    http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj714090(v=vs.105).aspx


    Gaurav Khanna | Microsoft VB.NET MVP | Microsoft Community Contributor

    • Proposed as answer by Khanna Gaurav Saturday, February 8, 2014 2:23 PM
    • Marked as answer by venu238 Saturday, February 8, 2014 4:38 PM
    Saturday, February 8, 2014 9:47 AM

All replies

  • If you are loading all the 3000-5000 question when page load, it is better to remove questions which are already answers. List.RemoveRange seems to be correct option. But loading so much questions in memory may not be a good idea.

    It would be better to load only few questions (may be 10 at a time) instead of all the questions. Or load question directly from database everytime. Probably this would be better than loading so may questions in memory. 


    Gaurav Khanna | Microsoft VB.NET MVP | Microsoft Community Contributor


    • Edited by Khanna Gaurav Thursday, February 6, 2014 7:08 PM
    • Proposed as answer by Khanna Gaurav Saturday, February 8, 2014 6:49 AM
    Thursday, February 6, 2014 6:59 PM
  • Doesn't it slow down the performance if I load the database everytime for a new question? 

    Dr.R.VenuKumar


    • Edited by venu238 Thursday, February 6, 2014 7:35 PM
    Thursday, February 6, 2014 7:25 PM
  • well there are ways to do it, the database load would not be that costly.

    you can load 20 questions first and when they reach the 10th mark, you will load 20-30, so that you always are 10 ahead. Maybe in an async manner.

    Loading small amount of data should be very fast for a database, the only thing that would matter is the network latency if you have any.

    • Proposed as answer by Khanna Gaurav Saturday, February 8, 2014 6:49 AM
    • Marked as answer by venu238 Saturday, February 8, 2014 9:57 AM
    Thursday, February 6, 2014 7:54 PM
  • Lex.db is very fast! I guess loading each question from the database would be best bet for me because in Lex.Db the indexes are held in memory so that data can be queried and then the full objects retrieved as needed..

    "Lex.db is able to automatically serialize the data that we need to store in the file system and to keep in memory just the keys and the indexes that are needed to perform operations on the file. For this reason it’s really fast and often performances are superior than using SQL Lite"

    What do you say?


    Dr.R.VenuKumar

    Thursday, February 6, 2014 8:18 PM
  • I have finally decided on how to remove the items after resuming the app..

    I'll load the database by using skip((int)question no.), so that the previous questions will be removed out of the list easily.

    Thank you :)


    Dr.R.VenuKumar

    • Marked as answer by venu238 Friday, February 7, 2014 6:49 PM
    Friday, February 7, 2014 6:43 PM
  • If you dealing with items when app resume then you can also refer Fast App Resume for Windows Phone

    http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj735579(v=vs.105).aspx
    http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff967547(v=vs.105).aspx


    Gaurav Khanna | Microsoft VB.NET MVP | Microsoft Community Contributor

    Saturday, February 8, 2014 6:54 AM
  • well there are ways to do it, the database load would not be that costly.

    you can load 20 questions first and when they reach the 10th mark, you will load 20-30, so that you always are 10 ahead. Maybe in an async manner.

    Loading small amount of data should be very fast for a database, the only thing that would matter is the network latency if you have any.

    I'm in a bit confusion on how to implement above step,  say I have 1000 qns then after completion of every 10 qns I have to load next 10 qns..than how should I resume back?

    Dr.R.VenuKumar

    Saturday, February 8, 2014 7:24 AM
  • If i'm not wrong, Fast app resume will just loads the previous instance of an app instead of launching a new instance of app when app is suspended and then resumed. It doesn't save the state after exiting the app.

    Next about "preserve and restore app state", maybe I have to save the questions list in the ApplicationDataObject along with current question to resume the app..as my list is very huge it may use much memory..


    Dr.R.VenuKumar

    Saturday, February 8, 2014 8:13 AM
  • I'm in a bit confusion on how to implement above step,  say I have 1000 qns then after completion of every 10 qns I have to load next 10 qns..than how should I resume back?

    Dr.R.VenuKumar

    You can have a count integer variable. When Next button is clicked to show next question you can initialize the count variable and when count reach 10 you can load the questions from database and reset the count variable.

    Also after every questions is answered, you can saved the number in IsolatedStorageSettings. And when you next start your application or resume you can read the last saved number and load the questions from there.

    http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj714090(v=vs.105).aspx


    Gaurav Khanna | Microsoft VB.NET MVP | Microsoft Community Contributor

    • Proposed as answer by Khanna Gaurav Saturday, February 8, 2014 2:23 PM
    • Marked as answer by venu238 Saturday, February 8, 2014 4:38 PM
    Saturday, February 8, 2014 9:47 AM
  • Thanks for your idea!, There is no problem of network latency as I have local database..

    Below is the code which is working so well for me, if u feel like doing any changes please suggest me


                if (SbQuestions.Count > 0)
                {
                    SbQuestions.RemoveAt(0);
                    if (SbQuestions.Count > 0)
                    {
                        this.NextSbQuestion();
                    }
                    else if (SbQuestions.Count == 0)
                    {
                        using (var db = new DbInstance("Questions"))
                        {
                            db.Map<Question>()
                            .Automap(i => i.Qn_ID, true)
                            .WithIndex("SubjectID", i => i.Qn_SubjectID)
                            .WithIndex("TopicID", i => i.Qn_TopicID);
                            db.Initialize();
    
                           //int "QnNo" is incremented in the nextquestion()
    
                            SbQuestions = db.Table<Question>().Skip(QnNo).Take(2).ToList();
                        }
                        if (SbQuestions.Count > 0)
                        {
                            this.NextSbQuestion();
                        }
                        else
                        {
                            this.StopQuestions();
                        }
                    }
                }
    
                else if (SbQuestions.Count == 0)
                {
                    using (var db = new DbInstance("Questions"))
                    {
                        db.Map<Question>()
                        .Automap(i => i.Qn_ID, true)
                        .WithIndex("SubjectID", i => i.Qn_SubjectID)
                        .WithIndex("TopicID", i => i.Qn_TopicID);
                        db.Initialize();
    
                        SbQuestions = db.Table<Question>().Skip(QnNo).Take(2).ToList();
                    }
                    if (SbQuestions.Count > 0)
                    {
                        this.NextSbQuestion();
                    }
                    else
                    {
                        this.StopQuestions();
                    }
                }


    Dr.R.VenuKumar

    Saturday, February 8, 2014 9:57 AM