因为bmob的数据只支持异步读取,而提供的查询方法又是可以逃逸的闭包,造成APP端运行的时候,数据还是空的.折腾了几天,终于想到一个借用.plist文件解决方案: 启动 -> 读云数据库,写入本地plist文件 -> 在Table页面读取.plist文件
- 首页ViewController:
override func viewDidLoad() {
super.viewDidLoad()
let query:BmobQuery = BmobQuery(className: "Others") //读取云端数据表名
query.findObjectsInBackground { (array, error) in
var i = 0
//使用appData.plist文件存云端数据内容
let plistPath = Bundle.main.path(forResource: "appData", ofType: "plist")
let itemArray = NSMutableArray(contentsOfFile: plistPath!)
//使用number.plist文件存云端数据表数据条数
let plistPath2 = Bundle.main.path(forResource: "number", ofType: "plist")
let itemArray2 = NSMutableArray(contentsOfFile: plistPath2!)
//写入数据表的数据条数,用于在table页面设置条目数
let dict2 = itemArray2?[0] as! NSDictionary
dict2.setValue((array?.count)!, forKey: "num")
itemArray2?.write(toFile: plistPath2!, atomically: true)
//读取云端数据写入appData.plist文件
while( i < (array?.count)! ) {
let obj = array?[i] as! BmobObject
let dict = itemArray?[i] as! NSDictionary
var name2 = (obj.object(forKey: "app_name") as? String)!
var url2 = (obj.object(forKey: "app_url") as? String)!
dict.setValue(name2, forKey: "app_name")
dict.setValue(url2, forKey: "app_url")
itemArray?.write(toFile: plistPath!, atomically: true)
i = i + 1
}
}
}
- Table页面ViewController
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CellApp", for: indexPath)
cell.textLabel!.text = nil
//从appData.plist文件里读取数据条目数
let plistPath = Bundle.main.path(forResource: "appData", ofType: "plist")
let itemArray = NSMutableArray(contentsOfFile: plistPath!)
let row = indexPath.row
let rowDict = itemArray?[row] as! NSDictionary
cell.textLabel?.text = rowDict["app_name"] as? String
cell.accessoryType = .none
return cell
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//从number.plist文件里读取数据条目数
let plistPath2 = Bundle.main.path(forResource: "number", ofType: "plist")
let itemArray2 = NSMutableArray(contentsOfFile: plistPath2!)
let xx = itemArray2?[0] as! NSDictionary
return xx["num"]! as! Int
}
number.plist文件结构:
appData.plist文件结构: