我是一个快速的新手,我正在一个Swift Mac应用程序作为演示项目。应用程序将股票代码存储在sqlite表中,获取股票价格,计算值,然后最终在表格视图中显示结果。 我正在寻找提高执行速度时提取数据填充我的表视图。所以我使用了Dispatch Queue,如下所示。问题在于股票价格和股票价格列(在异步收盘时计算)总是空的。我究竟做错了什么?该功能getStocksData返回NSMutableArray的是数据源为我的表视图Swift Mac App:加快执行与调度队列
func getStocksData() -> NSMutableArray {
sharedInstance.database!.open()
let resultSet: FMResultSet! = sharedInstance.database!.executeQuery("select stock_id,symbol,company,qty from stocks ", withArgumentsIn: [])
let stocksDBRowsArray : NSMutableArray = NSMutableArray()
if (resultSet != nil) {
while resultSet.next() {
let stockInfo : StockInfo = StockInfo()
stockInfo.StockID = resultSet.string(forColumn: "stock_id")!
stockInfo.Symbol = resultSet.string(forColumn: "symbol")!
stockInfo.StockCompany = resultSet.string(forColumn: "company")!
stockInfo.Qty = resultSet.string(forColumn: "qty")!
//create queue with unique label to fetch stock price
let queue=DispatchQueue(label:stockInfo.Symbol)
queue.async {
//code to fetch stock price goes here
.....
stockInfo.StockPrice=stockPrice
stockInfo.StockValue=stockPrice*stockInfo.Qty
}
stocksDBRowsArray.add(stockInfo)
}
}
sharedInstance.database!.close()
return stocksDBRowsArray
}
BTW,FMDatabase在多线程环境中使用是危险的。您应该改用FMDatabaseQueue。 –