我的应用有2个问题:
1 /在请求Api并在我的应用程序中获取Json后,我需要使用Realm Data存储这些数据(此时我的Realm对象并未存储任何属性)。
2 /如果用户没有互联网连接,我还需要重用存储在Realm中的数据。)
非常感谢 杰
我的ApiRequest:
class OpenWeatherApiHelper {
static func getForecasts(completionHandler: @escaping ([Forecast]) -> ()) {
Alamofire.request("http://openweathermap.org/data/2.5/forecast?id=6455259&appid=b6907d289e10d714a6e88b30761fae22").response { response in
var forecasts = [Forecast]()
// print("Request: \(String(describing: response.request))") // original url request
// print("Response: \(String(describing: response.response))") // http url response
// print("Data: \(String(describing: response.data))") // response data
// print("Error: \(String(describing: response.error))") // response Error
do {
if let data = response.data
, let myJsons = try JSON(data: data).dictionary
, let lists = myJsons["list"]?.array {
let myQueue = DispatchQueue(label: "ForecastQueue", attributes: .concurrent)
myQueue.async {
for (index, list) in lists.enumerated() {
if index % 8 == 0
, let dt = list["dt"].int
, let main = list["main"].dictionary
, let temp = main["temp"]?.int
, let tempMin = main["temp_min"]?.double
, let tempMax = main["temp_max"]?.double
, let pressure = main["pressure"]?.double
, let seaLevel = main["sea_level"]?.double
, let grndLevel = main["grnd_level"]?.double
, let tempKf = main["temp_kf"]?.int
, let humidity = main["humidity"]?.int
, let weathers = list["weather"].array
, let clouds = list["clouds"].dictionary
, let wind = list["wind"].dictionary
, let dtTxt = list["dt_txt"].string
, let all = clouds["all"]?.int
, let speed = wind["speed"]?.double
, let deg = wind["deg"]?.double
{
for weather in weathers {
if let id = weather["id"].int
, let main = weather["main"].string
, let forecastDescription = weather["description"].string
, let icon = weather["icon"].string {
let forecast = Forecast(dt: dt, temp: temp, tempMin: tempMin, tempMax: tempMax, pressure: pressure, seaLevel: seaLevel, grndLevel: grndLevel, humidity: humidity, tempKf: tempKf, dtTxt: dtTxt, id: id, main: main, icon: icon, speed: speed, deg: deg, all: all, forecastDescription: forecastDescription)
forecasts.append(forecast)
}
}
}
}
DispatchQueue.main.async {
completionHandler(forecasts)
}
}
}
} catch let error {
print(error.localizedDescription)
completionHandler(forecasts)
}
}
}
}
我的班级对象:
class Forecast {
var dt: Int
var temp: Int
var tempMin: Double
var tempMax: Double
var pressure: Double
var seaLevel: Double
var grndLevel: Double
var humidity: Int
var tempKf: Int
var dtTxt: String
var id: Int
var main: String
var icon: String
var speed: Double
var deg: Double
var all: Int
var forecastDescription: String
init(dt: Int, temp: Int, tempMin: Double, tempMax: Double, pressure: Double, seaLevel: Double, grndLevel: Double, humidity: Int, tempKf: Int, dtTxt: String, id: Int, main: String, icon: String, speed: Double, deg: Double, all: Int, forecastDescription: String) {
self.dt = dt
self.temp = temp
self.tempMin = tempMin
self.tempMax = tempMax
self.pressure = pressure
self.seaLevel = seaLevel
self.grndLevel = grndLevel
self.humidity = humidity
self.tempKf = tempKf
self.dtTxt = dtTxt
self.id = id
self.main = main
self.icon = icon
self.speed = speed
self.deg = deg
self.all = all
self.forecastDescription = forecastDescription
}
}
My Realm Class Object:
class RealmForecast: Object {
@objc dynamic var dt: Int = 0
@objc dynamic var temp: Int = 0
@objc dynamic var tempMin: Double = 0.0
@objc dynamic var tempMax: Double = 0.0
@objc dynamic var pressure: Double = 0.0
@objc dynamic var seaLevel: Double = 0.0
@objc dynamic var grndLevel: Double = 0.0
@objc dynamic var humidity: Int = 0
@objc dynamic var tempKf: Int = 0
@objc dynamic var dtTxt: String = ""
@objc dynamic var id: Int = 0
@objc dynamic var main: String = ""
@objc dynamic var icon: String = ""
@objc dynamic var speed: Double = 0.0
@objc dynamic var deg: Double = 0.0
@objc dynamic var all: Int = 0
@objc dynamic var forecastDescription: String = ""
}
在我的VC中:
func updateDisplay() {
OpenWeatherApiHelper.getForecasts { forecasts in
self.forecasts = forecasts
self.forecastTableView.reloadData()
}
}
func insert(forecast: RealmForecast) {
let realm = try! Realm()
try! realm.write({
let realmForecast = RealmForecast()
realmForecast.all = forecast.all
realmForecast.dt = forecast.dt
realmForecast.temp = forecast.temp
realmForecast.tempMin = forecast.tempMin
realmForecast.tempMax = forecast.tempMax
realmForecast.pressure = forecast.pressure
realmForecast.seaLevel = forecast.seaLevel
realmForecast.grndLevel = forecast.grndLevel
realmForecast.humidity = forecast.humidity
realmForecast.tempKf = forecast.tempKf
realmForecast.dtTxt = forecast.dtTxt
realmForecast.id = forecast.id
realmForecast.main = forecast.main
realmForecast.icon = forecast.icon
realmForecast.speed = forecast.speed
realmForecast.deg = forecast.deg
realmForecast.all = forecast.all
realmForecast.forecastDescription = forecast.forecastDescription
realm.add(realmForecast)
})
}
override func viewDidLoad() {
super.viewDidLoad()
updateDisplay()
let realmForecast = RealmForecast()
insert(forecast: realmForecast)
}
}