ObservableCollection和SQlite到Async? [英] ObservableCollection and SQlite to Async?
问题描述
我对数据库和异步编程都很陌生。我正在制作一个POS应用程序,最终将有数百个客户,可能还有数千个交易。当我想进行客户搜索或查找上一张票时,我不希望我的程序
等待结果等待。
这是显示搜索结果:
<预类= "prettyprint">私人异步任务searchCritiria_TextChanging(文本框发件人,TextBoxTextChangingEventArgs参数)
{
FilteredCustomer.Clear();
if(searchCritiria.Text.Length> = 3)
{
SQLiteConnection dbConnection = new SQLiteConnection(" Customers.db");
string sSQL = null;
sSQL = @" SELECT [first],[last],[spouse],[home],[work],[cell] FROM Customers" ;;
ISQLiteStatement dbState = dbConnection.Prepare(sSQL);
while(dbState.Step()== SQLiteResult.ROW)
{
string sFirst = dbState [" first" ;]作为字符串;
string sLast = dbState [" last"] as string;
string sSpouse = dbState [" spouse"] as string;
string sHome = dbState [" home"] as string;
string sWork = dbState [" work"] as string;
string sCell = dbState [" cell"] as string;
//加载到observable collection
if(searchType.SelectedIndex == 0)// name search
{
if(sFirst.Contains(searchCritiria.Text)|| sLast。包含(searchCritiria.Text)|| sSpouse.Contains(searchCritiria.Text))
FilteredCustomer.Add(新客户{第一= sFirst,最后= sLast,配偶= sSpouse,家= SHOME,工作= sWork,电池= sCell});
}
,否则//号码查询
{
如果(sWork.Contains(searchCritiria.Text)|| sHome.Contains(searchCritiria.Text)|| sCell.Contains(searchCritiria .Text))
FilteredCustomer.Add(新客户{first = sFirst,last = sLast,spouse = sSpouse,home = sHome,work = sWork,cell = sCell});
}
}
}
}
在我的程序中,我有一些类似于此的void方法。
Ex:
public void refreshItems()
{
Items.Clear ();
SQLiteConnection dbConnection = new SQLiteConnection(" Items.db");
string deleteQuery =" DELETE FROM Items WHERE name ='" +" " +"'" ;;
dbConnection.Prepare(deleteQuery).Step();
string sSQL = @" SELECT * FROM Items" ;;
ISQLiteStatement dbState = dbConnection.Prepare(sSQL);
while(dbState.Step()== SQLiteResult.ROW)
{
string sID = dbState [" ItemID"] as string;
string sName = dbState [" name"] as string;
string sPrice = dbState [" price"] as string;
string sPosition = dbState [" category"] as string;
string sTax = dbState [" taxID"] as string;
string sMinQuantity = dbState [" minQuantity"] as string;
//加载到observable collection
Items.Add(new Item {itemID = sID,price = sPrice,name = sName,category = sPosition,minQuantity = sMinQuantity}) ;
}
}
我不知道怎么做解决这个问题。我尝试做一些研究但没有成功。任何建议将不胜感激!
嗨MartinNowak
我看到你已经获得了解决方案( 如何用
在UWP中进行SQlite异步? 强>)。
我会在这里复制解决方案,这可以帮助其他遇到同样问题的人。
让你的方法返回
任务 而不是无效。使用void方法通常是一种不好的做法,所以这会有所帮助。因此,要将您的方法转换为等待并因此异步运行,请更改方法签名,如下所示:
< span style ="padding:0in; border:1pt windowtext; color:#101094; font-family:inherit,serif">
private void searchCritiria_TextChanging(TextBox sender,TextBoxTextChangingEventArgs args)
{
FilteredCustomer.Clear();
if(searchCritiria.Text.Length> = 3)
{
SQLiteConnection dbConnection = new SQLiteConnection(" Customers.db");
string sSQL = null;
sSQL = @" SELECT
[first],[last],[spouse],[home],[work],[cell] FROM Customers" ;;
ISQLiteStatement dbState = dbConnection.Prepare(sSQL);
while(dbState.Step()== SQLiteResult.ROW)
{
string sFirst = dbState [" first"] as string;
string sLast = dbState [" last"] as string;
string sSpouse = dbState [" spouse"] as string;
string sHome = dbState [" home"] as string;
string sWork = dbState [" work"] as string;
string sCell = dbState [" cell"] as string;
//加载到可观察的集合中
if(searchType.SelectedIndex == 0)//名称搜索
{
如果(sFirst.Contains(searchCritiria.Text)|| sLast.Contains(searchCritiria.Text)|| sSpouse.Contains(searchCritiria.Text))
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; FilteredCustomer.Add(new Customer {first = sFirst,last = sLast,spouse = sSpouse,home = sHome,work = sWork,cell = sCell});
}
否则//数字搜索
{
如果(sWork.Contains(searchCritiria.Text)|| sHome.Contains(searchCritiria.Text)|| sCell.Contains(searchCritiria.Text))
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; FilteredCustomer.Add(new Customer {first = sFirst,last = sLast,spouse = sSpouse,home = sHome,work = sWork,cell = sCell});
}
}
}
return Task.CompletedTask;
}
既然你有一个简单的方法只返回一个任务,你只需在
中调用它 async 方法 等待 例如:跨度> 跨度>
<预类=" prettyprint"> 私人异步空隙searchCritiria_TextChanging(文本框发件人,TextBoxTextChangingEventArgs参数)
{ &NBSP;
等待PerformSQLTasks();
}
感谢您的理解。
最好的问候
Roy
I'm fairly new to databases and asynchronous programing. I'm making a POS app that will eventually have hundreds of customers and possibly thousands of transactions. When I want to do a customer search or look up a previous ticket I don't want my program to hang waiting on the results.
Here is the method that shows the search result:
private async Task searchCritiria_TextChanging(TextBox sender, TextBoxTextChangingEventArgs args) { FilteredCustomer.Clear(); if (searchCritiria.Text.Length >= 3) { SQLiteConnection dbConnection = new SQLiteConnection("Customers.db"); string sSQL = null; sSQL = @"SELECT [first],[last],[spouse],[home],[work],[cell] FROM Customers"; ISQLiteStatement dbState = dbConnection.Prepare(sSQL); while (dbState.Step() == SQLiteResult.ROW) { string sFirst = dbState["first"] as string; string sLast = dbState["last"] as string; string sSpouse = dbState["spouse"] as string; string sHome = dbState["home"] as string; string sWork = dbState["work"] as string; string sCell = dbState["cell"] as string; //Load into observable collection if (searchType.SelectedIndex == 0)//name search { if (sFirst.Contains(searchCritiria.Text) || sLast.Contains(searchCritiria.Text) || sSpouse.Contains(searchCritiria.Text)) FilteredCustomer.Add(new Customer { first = sFirst, last = sLast, spouse = sSpouse, home = sHome, work = sWork, cell = sCell }); } else//number search { if (sWork.Contains(searchCritiria.Text) || sHome.Contains(searchCritiria.Text) || sCell.Contains(searchCritiria.Text)) FilteredCustomer.Add(new Customer { first = sFirst, last = sLast, spouse = sSpouse, home = sHome, work = sWork, cell = sCell }); } } } }
Throughout my program I have void methods that are structured similar to this.
Ex:
public void refreshingItems() { Items.Clear(); SQLiteConnection dbConnection = new SQLiteConnection("Items.db"); string deleteQuery = "DELETE FROM Items WHERE name = '" + " " + "'"; dbConnection.Prepare(deleteQuery).Step(); string sSQL = @"SELECT * FROM Items"; ISQLiteStatement dbState = dbConnection.Prepare(sSQL); while (dbState.Step() == SQLiteResult.ROW) { string sID = dbState["ItemID"] as string; string sName = dbState["name"] as string; string sPrice = dbState["price"] as string; string sPosition = dbState["category"] as string; string sTax = dbState["taxID"] as string; string sMinQuantity = dbState["minQuantity"] as string; //Load into observable collection Items.Add(new Item { itemID = sID, price = sPrice, name = sName, category = sPosition, minQuantity = sMinQuantity }); } }
I'm not sure how to solve this issue. I tried doing some research but no success. Any advice would be greatly appreciated!
Hi MartinNowak
I saw that you have got a solution in (How to make SQlite async in UWP?).
I will copy the solution in here, this could help others who are facing the same problem.
Make your method return a Task instead of void. It's generally a bad practice to use void methods eitherways so this will help. So to convert your method to be awaitable and hence run asynchronously change the method signature as below:
private void searchCritiria_TextChanging(TextBox sender, TextBoxTextChangingEventArgs args) { FilteredCustomer.Clear(); if(searchCritiria.Text.Length >= 3) { SQLiteConnection dbConnection = new SQLiteConnection("Customers.db"); string sSQL = null; sSQL = @"SELECT [first],[last],[spouse],[home],[work],[cell] FROM Customers"; ISQLiteStatement dbState = dbConnection.Prepare(sSQL); while (dbState.Step() == SQLiteResult.ROW) { string sFirst = dbState["first"] as string; string sLast = dbState["last"] as string; string sSpouse = dbState["spouse"] as string; string sHome = dbState["home"] as string; string sWork = dbState["work"] as string; string sCell = dbState["cell"] as string; //Load into observable collection if (searchType.SelectedIndex == 0)//name search { if(sFirst.Contains(searchCritiria.Text) || sLast.Contains(searchCritiria.Text) || sSpouse.Contains(searchCritiria.Text)) FilteredCustomer.Add(new Customer {first = sFirst,last = sLast,spouse = sSpouse, home = sHome, work = sWork, cell = sCell}); } else//number search { if(sWork.Contains(searchCritiria.Text)|| sHome.Contains(searchCritiria.Text) || sCell.Contains(searchCritiria.Text)) FilteredCustomer.Add(new Customer { first = sFirst, last = sLast,spouse = sSpouse, home = sHome, work = sWork, cell = sCell }); } } } return Task.CompletedTask; }
Now that you have a neat method that simply returns a task, you can simply call it in an async method with await eg:
private async void searchCritiria_TextChanging(TextBox sender, TextBoxTextChangingEventArgs args) { await PerformSQLTasks(); }Thanks for your understanding.
Best Regards
Roy
这篇关于ObservableCollection和SQlite到Async?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!