jianweiwangs / rxswiftmvvm Goto Github PK
View Code? Open in Web Editor NEWRxSwift MVVM Moya HandyJSON
RxSwift MVVM Moya HandyJSON
大神考虑到列表翻页的功能吗?homeAction在翻页的时候无法进行复用哦。重复调用HomeAction会直接崩溃。
接口中除了解析到[Storie]数组外,一般接口中都返回code,msg等额外信息,如何解析呢?每个接口都弄一个包含相同code,msg+额外Obj,感觉这样也不现实。
项目中最外层结构大致如下:
class Msg : HandyJSON {
var code : Int? = 0
var msg : String? = ""
var obj : HandyJSON?
var list : [HandyJSON]?
required init() {
}
}
homeAction = Action<Void, [Storie]>.init(workFactory: {[unowned self] in
self.model.request(API.Main, FirstModel.self)
.map{$0.stories ?? []}
.do(onNext: {
self.data = $0
})
})
extension Observable {
func map<T: HandyJSON>(_ type: T.Type) -> Observable<T> {
return map { response in
guard let response = response as? Moya.Response else {
throw RxSwiftMoyaError.ResponseError
}
guard (200...209) ~= response.statusCode else {
throw RxSwiftMoyaError.RequestFailedError
}
guard let json = try? JSONSerialization.jsonObject(with: response.data, options: JSONSerialization.ReadingOptions.init(rawValue: 0)) as! [String: Any] else {
throw RxSwiftMoyaError.ResponseError
}
let object = JSONDeserializer<T>.deserializeFrom(dict: json)
guard let model = object else {
throw RxSwiftMoyaError.ParseJSONError
}
return model
}
}
}
我的代码如下,但是列表就是没有数据展示?实在不解
class ViewController: UIViewController{
var viewModel : MyViewModel? = MyViewModel()
let disposeBag = DisposeBag()
var tableView : UITableView! = {
var tableView = UITableView(frame: UIScreen.main.bounds, style: .plain)
tableView.rowHeight = 44.0
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
tableView.tableFooterView = UIView()
return tableView
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(tableView)
self.viewModel?
.homeAction?
.execute(())
.bind(to: tableView.rx.items(cellIdentifier: "cell", cellType: UITableViewCell.self)){
index, model, cell in
cell.textLabel?.text = model.nick_name
}
//.disposed(by:disposeBag)
}
var homeAction: Action<Void, [BaseUser]>?
let model = Model()
var data: [BaseUser]?
init() {
initialize()
}
func initialize() {
homeAction = Action<Void, [BaseUser]>.init(workFactory: {[unowned self] in
var params : Dictionary<String, Any> = [:]
params["lp"] = ""
params["page"] = "1"
params["age_min"] = "-1"
params["age_max"] = "-1"
params["height_min"] = "-1"
params["height_max"] = "-1"
params["prov"] = ""
params["city"] = ""
params["user_id"] = "1"
return self.model.request(UserAPI.homeList(params), Msg<BaseUser>.self)
.debug()
.map{$0.list ?? []}
.do(onNext: {
self.data = $0
})
})
请教翻页问题:想实现列表翻页功能,但是接口返回的list 如何追加到self.data上呢?如何将追加的最新列表数据显示到列表上呢?
homeAction = Action<Void, Msg<BaseUser>>.init(workFactory: {[unowned self] in
var params : Dictionary<String, Any> = [:]
params["lp"] = self.lp
params["page"] = "1"
params["age_min"] = "-1"
params["age_max"] = "-1"
params["height_min"] = "-1"
params["height_max"] = "-1"
params["prov"] = ""
params["city"] = ""
params["user_id"] = "1"
return self.model.request(UserAPI.homeList(params), Msg<BaseUser>.self)
.debug()
.filter({ [unowned self] (value) -> Bool in
Loger.log(msg: "\(value.code!) \(value.msg!)")
if value.code == 1 {
self.lp = value.lp ?? ""
self.refreshStatus.value = RefreshStatus.DropDownSuccess
//处理返回的数据,给dataSource赋值
if (value.list?.count)! >= 0 {
self.refreshStatus.value = RefreshStatus.PullSuccessHasMoreData
} else {
self.refreshStatus.value = RefreshStatus.PullSuccessNoMoreData
}
self.data = value.list ?? []
return true
}
self.refreshStatus.value = RefreshStatus.DropDownSuccess
return true
})
})
self.viewModel?
.homeAction?
.execute(())
.filter({ (value) -> Bool in
Loger.log(msg: "\(value.code!) \(value.msg!)")
if value.code == 1
{
return true
}
return false
})
.map{$0.list ?? []}
.bind(to: tableView.rx.items(cellIdentifier: "cell", cellType: UITableViewCell.self)){
index, model, cell in
cell.textLabel?.text = model.nick_name
}
.disposed(by:disposeBag)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.