//首頁加載的數(shù)據(jù)的遷移
class func loadHomeData(sinceId: Int64,maxId: Int64,finished: @escaping ([[String : Any]]?) -> ()) {
let res = checkoutStatus(sinceId: sinceId, maxId: maxId)
//if let result ',' 可以獲取到賦值的結果
if let result = res, result.count > 0 {
//有緩存數(shù)據(jù) 返回緩存數(shù)據(jù)
//TODO: 待返回
finished(result)
return
}
//沒有緩存數(shù)據(jù), 請求網(wǎng)絡數(shù)據(jù)
let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
let parameters = ["access_token" : HMUserAccountViewModel.shared.access_token,
"max_id" : "\(maxId)",
"since_id" : "\(sinceId)"]
HMNetworkTools.shared.request(method: .GET, urlString: urlString, parameters: parameters, finished: { (responseObject,error) -> () in
if error != nil {
//告訴外界請求失敗 在閉包中如何完成失敗的回調
//TODO: 完成失敗的回調
finished(nil)
return
}
//成功
let dict = responseObject as! [String : Any]
//嘗試轉換為字典類型的數(shù)組
guard let array = dict["statuses"] as? [[String : Any]] else {
finished(nil)
return
}
//回調的網(wǎng)絡 請求的結果
finished(array)
//存儲到本地
cacheStauts(array: array)
})
}
//1.檢查本地是否有緩存數(shù)據(jù)
class func checkoutStatus(sinceId: Int64,maxId: Int64) -> [[String : Any]]? {
//查詢的sql
guard let userId = HMUserAccountViewModel.shared.userAccount?.uid else {
return nil
}
//會將數(shù)據(jù)庫中所有的緩存數(shù)據(jù)都查找到
var sql = "SELECT status FROM T_Status WHERE userId = \(userId) "
//拼接sql
//如果sinceId > 0 標識需要查找新數(shù)據(jù)
if sinceId > 0 {
//向上查找
sql += "AND statusId > \(sinceId) "
}
if maxId > 0 {
//向下查找
sql += "AND statusId < \(maxId) "
}
//排序 降序
sql += "ORDER BY statusId DESC "
//限制條數(shù)
sql += "LIMIT 100"
print(sql)
//實例化數(shù)據(jù)
var array = [[String : Any]]()
HMSQLiteTools.shared.queue.inDatabase { (db) in
guard let res = db!.executeQuery(sql, withArgumentsIn: nil) else {
//在閉包中不能夠return
return
}
//逐條獲取數(shù)據(jù)
while res.next() {
//獲取status 二進制數(shù)據(jù)
let jsonData = res.data(forColumn: "status")
//轉換為json 字典
let dict = try! JSONSerialization.jsonObject(with:jsonData!, options: [])
array.append(dict as! [String : Any])
}
}
return array
}
//2. 如果本地有緩存數(shù)據(jù) 就直接返回本地的緩存數(shù)據(jù),給用戶節(jié)省流量,讓用戶在沒有網(wǎng)絡的情況可以瀏覽之前的內(nèi)容
//3. 沒有緩存數(shù)據(jù)就請求網(wǎng)絡數(shù)據(jù) 返回網(wǎng)絡數(shù)據(jù)
//4. 獲取網(wǎng)絡數(shù)據(jù)之后就應該立即存儲網(wǎng)絡數(shù)據(jù)
/*
statusId
status
userId
*/
class func cacheStauts(array: [[String : Any]]) {
//獲取userId
guard let userId = HMUserAccountViewModel.shared.userAccount?.uid else {
print("用戶沒有登錄呢")
return
}
//sql
//如果在對應的主鍵下已經(jīng)存在數(shù)據(jù) 就執(zhí)行替換的操作
let sql = "INSERT OR REPLACE INTO T_Status (statusId,status,userId) VALUES (?,?,?)"
HMSQLiteTools.shared.queue.inTransaction { (db, rollback) in
for item in array {
let statusId = item["id"]!
//數(shù)據(jù)庫中能夠存儲的數(shù)據(jù)不能夠是Foundation 需要將字典數(shù)據(jù)轉換二進制數(shù)據(jù)存儲到數(shù)據(jù)庫中
//如果是字典數(shù)據(jù) 在讀取的時候會有問題
let jsonData = try! JSONSerialization.data(withJSONObject: item, options: [])
let res = db!.executeUpdate(sql, withArgumentsIn: [statusId,jsonData,userId])
if !res {
//不成功就執(zhí)行回滾
rollback?.pointee = true
return
}
}
}
}
swift網(wǎng)絡請求與數(shù)據(jù)保存
最后編輯于 :
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門忌怎,熙熙樓的掌柜王于貴愁眉苦臉地迎上來籍滴,“玉大人,你說我怎么就攤上這事榴啸∧醵瑁” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵鸥印,是天一觀的道長勋功。 經(jīng)常有香客問我,道長库说,這世上最難降的妖魔是什么狂鞋? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮潜的,結果婚禮上骚揍,老公的妹妹穿的比我還像新娘。我一直安慰自己啰挪,他們只是感情好信不,可當我...
- 文/花漫 我一把揭開白布纤掸。 她就那樣靜靜地躺著,像睡著了一般浑塞。 火紅的嫁衣襯著肌膚如雪借跪。 梳的紋絲不亂的頭發(fā)上,一...
- 文/蒼蘭香墨 我猛地睜開眼辛掠,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了释牺?” 一聲冷哼從身側響起萝衩,我...
- 正文 年R本政府宣布锯梁,位于F島的核電站即碗,受9級特大地震影響,放射性物質發(fā)生泄漏陌凳。R本人自食惡果不足惜剥懒,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望合敦。 院中可真熱鬧初橘,春花似錦、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至夜只,卻和暖如春垒在,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背扔亥。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 手機的優(yōu)勢是攜帶方便嘹朗,可以隨時打開师妙,而且手機通常總是處于聯(lián)網(wǎng)狀態(tài)的屹培,所以網(wǎng)絡支持對于手機很重要。而且Java的網(wǎng)絡...
- 網(wǎng)絡請求框架Alamofire 源碼地址Swift 2.3 Alamofire3.0版本支持 iOS 8Swif...
- 本文章來為各位介紹一篇關于Swift HTTP網(wǎng)絡操作庫SwiftHTTP使用詳解1(配置媒吗,及數(shù)據(jù)請求)的例子,希...
- 登錄請求解析 請求體: 賬號密碼: 將內(nèi)容轉換成 Data 后臺對數(shù)據(jù)類型的需要乙埃,不用太糾結 ?? 登錄請求的U...
- 上篇主要是runtime的理論基礎(地址請戳這里)闸英,但是具體到項目中的時候很少會想到使用runtime來解決問題,...