我試圖檢查應用程序的啓動是否連接互聯網。 當我切換我的Wifi並檢查應用程序周圍的應用程序時,應用程序仍嘗試連接到互聯網10秒鐘。如果用戶沒有連接到互聯網,那麼我應該顯示一條警告,請連接到互聯網。我不是localDataStore
。 在我看來,我必須在queryForTable
添加互聯網檢查聲明。檢查互聯網連接queryForTable解析
func baseQuery() -> PFQuery {
let query = PFQuery(className: "homeClass") // Replace with the class name for the Parse data you're interested in.
// Just like with a regular PFQuery you can filter and sort.
query.orderByAscending("SNo")
return query
}
override func queryForTable() -> PFQuery {
if Reachability.isConnectedToNetwork() == true {
print("Internet connection OK")
tableView.reloadData()
self.baseQuery()
} else {
print("Internet connection FAILED")
let alert = UIAlertView(title: "No Internet Connection", message: "Make sure your device is connected to the internet.", delegate: nil, cancelButtonTitle: "OK")
alert.show()
let destinationVC:MainViewController = self.storyboard?.instantiateViewControllerWithIdentifier("NewView") as! MainViewController
navigationController?.pushViewController(destinationVC , animated: true)
}
}
更新
func baseQuery() -> PFQuery {
let query = PFQuery(className: "homeClass")
if Reachability.isConnectedToNetwork() != true {
print("Internet connection FAILED")
} else {
print("Internet connection OK")
query.orderByAscending("SNo")
return query
}
return PFQuery()
}
override func queryForTable() -> PFQuery {
return self.baseQuery()
}
我想此代碼,它仍然試圖從互聯網上獲取數據。
這個我必須創建一個新的文件?當我運行我的代碼時,執行的第一個函數是'func queryForTable() - > PFQuery' –
如果你的函數依賴於網絡連接,那麼在運行查詢之前,你需要運行這樣的事情。 – user3069232