Forum Xamarin Xamarin.iOS

Background tasks and update query.

phaniphani USMember

Hello, I have 3 buttons on my App screen. Clicking on 1st and 2nd each launch a Dialog View Controller (DVC). This DVC has 3 controls (a search bar to enter text,a button below search bar and a tableview that shows the results of the search text). So as user types in my searchbar,searchbar.textchanged event is fired and results are listed in the table view. All this is on screen A. I have another screen B. I click the 3rd button on screen A to take me to screen B. Here i have many customised checkboxes. Each checkbox has a name associated with it. I select one and it should update/insert the value associated into SQLite DB.

The search functionality occurs on a new task.Like this

            searchBar.TextChanged += (object sender, UISearchBarTextChangedEventArgs e) => {
                            string strSearchText = searchBar.Text;

                            var context = TaskScheduler.FromCurrentSynchronizationContext ();
                            var task = Task.Factory.StartNew (() => 
        //search . SELECT queries that populate two datatables and I merge both into one. That datatable i convert into list and pass it as Source for my tableview.
           }).ContinueWith (t => 

                        tblSource = new CustomerTableSource(lstCustomer,pVCOBJ,searchSourceTag);

                            tblMain.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
                            tblMain.Source =  tblSource;//new CustomerTableSource(lstCustomer);
                            RectangleF rect = tblMain.Bounds;
                            tblMain.Frame = new RectangleF(rect.X,rect.Y+110f,rect.Width,rect.Height + 220f);

                            Console.WriteLine("The height of the table view is:"+tblMain.ContentSize.Height);


                            Console.WriteLine("the search text is:"+strSearchText);

                        catch(Exception ex)
                    }, context);


Now when i move to screen B, i am still seeing this console line "The height of the table view is:45255". So on the next screen when i try to access the DB and do some operation it is giving me "database is locked" exception. I have used SqliteConnection.SetConfig (SQLiteConfig.Serialized);
early in the program.Somehow my DB is getting locked. Is there a way to know whether DB is locked or not? I used same database connection throughout the app .Still the same exception. I tried using a connection where and when required and close them and dispose them. Still the same exception.

So is my reasoning correct? The background task is still doing something (as i keep seeing the tableview console line). Is that someway locking my DB? If you need more info, please ask. Thanks.


  • ChrisHonselaarChrisHonselaar NLBeta ✭✭✭

    You seem to be using the database from multiple threads, which is likely the source of your issues. Make sure you open and close the connection within the same thread always. If you expect concurrent requests, you may need to put a lock statement around the entire connection open/query/close cycle. Either that or queue your queries
    to be executed from one single thread. More complex, but can give better performance.

    But before diving into multithreaded query strategies, you may want to take a step back and evaluate if it's necessary to execute queries from several threads in the first place. In this case you may want to use a simple timer mechanism and a signal field to ensure that you only start searching again if the user has stopped typing for a few hundred millisecs or so, and only once any previous query has finished.

Sign In or Register to comment.