我有一个HTTPService返回一个Observable<NSData>.我的目标是将该服务组合到另一个服务中,ServiceA该服务会根据我的用例转换该数据.Observable.create在RxSwift中使用2.0.0-rc.0就ServiceA可以了.我的问题是如何正确处理从订阅返回的一次性HTTPService.
如果我什么都不做,我得到编译时警告result of call is unused:http://git.io/rxs.ud.我从阅读中理解,如果我什么都不做,那可能就行了.(xs下面提到的是let xs: Observable<E> ....
如果xs以可预测的方式终止,其中包含Completed或Error消息,则不处理订阅Disposable不会泄漏任何资源,但它仍然是首选方式,因为以这种方式,元素计算在可预测的时刻终止.
所以这就是我目前正在解决的问题,也是我想知道我是否正确地做到这一点,或者我是否误解了一些问题.
public struct ServiceA{
public static func changes() -> Observable<ChangeSet>{
return Observable.create{ observable in
// return's Observable<NSData>
let request = HTTPService.get("https://httpbin.org/get")
let disposable = request.subscribe(
onNext: { data in
// Do more work to transform this data
// into something meaningful for the application.
// For example purposes just …Run Code Online (Sandbox Code Playgroud) 我在演示中学习了一个例子来创建一个UITableView并渲染单元格.
在我看来,items是viewModel,我想通过使用Alamofire或其他库来请求跨网络的一些数据.当我收到回复时,如何更新相关单元格的文本?
换句话说,我想将viewModel绑定到Cells.当模型的数据发生变化时,单元格的内容可能会自动更改.
我有一个想法是:为单元格的内容创建一个Observable序列(绑定到单元格).当服务器响应数据时,它调用函数tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Top).但这似乎不是一种优雅或好的方法.
所以,希望有些人可以帮助我:)
let items = Observable.just([
"First Item",
"Second Item",
"Third Item"
])
items
.bindTo(tableView.rx_itemsWithCellIdentifier("Cell", cellType: UITableViewCell.self)) { (row, element, cell) in
cell.textLabel?.text = "\(element) @ row \(row)"
/* to do some binding or something else ? */
}
.addDisposableTo(disposeBag)
tableView
.rx_modelSelected(String)
.subscribeNext { value in
DefaultWireframe.presentAlert("Tapped `\(value)`")
}
.addDisposableTo(disposeBag)
Run Code Online (Sandbox Code Playgroud) 我在RXSwift中有一个委托包装器
func tableView(tableView: UITableView,movedRowAtIndexPath sourceIndexPath: NSIndexPath,toIndexRowPath destinationRowIndexPath: NSIndexPath)
Run Code Online (Sandbox Code Playgroud)
它们看起来像
public var rx_itemRowMoved: ControlEvent<ItemMovedEvent> {
let source: Observable<ItemMovedEvent> = rx_delegate.observe("tableView:movedRowAtIndexPath:toIndexRowPath:")
.map { a in
return ((a[1] as! NSIndexPath), (a[2] as! NSIndexPath))
}
return ControlEvent(events: source)
}
Run Code Online (Sandbox Code Playgroud)
但我有代表返回值
func selectionViewForTableView(tableView: UITableView,destinitionCell cell:UITableViewCell,toIndexRowPath destinationRowIndexPath: NSIndexPath) -> UIView
Run Code Online (Sandbox Code Playgroud)
我如何为这个委托实现包装器?
我正在使用RxSwift来获取一些网络数据,而我在执行每次迭代数组的请求时遇到了麻烦.这是我的想法:
(代码简化)
var arrayObj = networkClient.request(getObjsEndpoint)
.fetchObjLocationDetails(withNetworkClient: networkClient)
Run Code Online (Sandbox Code Playgroud)
(代码简化)
extension ObservableType where E == [Obj]? {
func fetchObjsLocationDetails(withNetworkClient networkClient: NetworkClient) -> Observable<[Obj]?> {
return flatMap { Objs -> Observable<[Obj]?> in
guard let unwrappedObjs = Objs as [Obj]? else { return Observable.just(nil) }
let disposeBag = DisposeBag()
var populatedObjs = [Obj]()
unwrappedObjs.forEach { obj in
let getLocationDetailsEndpoint = WeDriveParkAPI.getLocation(id: String(obj.id))
networkClient.request(getLocationDetailsEndpoint)
.observeOn(MainScheduler.instance)
.subscribe(onNext: { json in
guard let populatedObj = Obj.fromJSON(json) as Obj? else { return }
populatedObjs …Run Code Online (Sandbox Code Playgroud) 在RxSwift/RxCocoa中,您可以为委托(例如UIScrollViewDelegate或CLLocationManagerDelegate)创建一个反应式包装器,以便为某些委托方法启用Rx可观察序列.
我正在尝试为该UIApplicationDelegate方法实现此功能applicationDidBecomeActive:
到目前为止我尝试的非常简单,类似于DelegateProxyRxCocoa中包含的子类.
我创建了我的DelegateProxy子类:
class RxUIApplicationDelegateProxy: DelegateProxy, UIApplicationDelegate, DelegateProxyType {
static func currentDelegateFor(object: AnyObject) -> AnyObject? {
let application: UIApplication = object as! UIApplication
return application.delegate
}
static func setCurrentDelegate(delegate: AnyObject?, toObject object: AnyObject) {
let application: UIApplication = object as! UIApplication
application.delegate = delegate as? UIApplicationDelegate
}
}
Run Code Online (Sandbox Code Playgroud)
和Rx扩展UIApplication:
extension UIApplication {
public var rx_delegate: DelegateProxy {
return proxyForObject(RxUIApplicationDelegateProxy.self, self)
}
public var rx_applicationDidBecomeActive: Observable<Void> { …Run Code Online (Sandbox Code Playgroud) 我需要在API调用时显示进度条,并在API调用完成后隐藏它.以下是我编写的用于填充表的代码.我应该在哪里打电话来显示和隐藏被调用的API的进度?有RxSwift办法做到这一点吗?
items = fetchAllAnswers()
items.bindTo(self.myTableView.rx_itemsWithCellIdentifier("cellIdentifier", cellType: UITableViewCell.self)){ (row, element, cell) in
cell.textLabel?.text = element
}
.addDisposableTo(disposeBag)
func fetchAllAnswers() -> Observable<[String]>{
let api = Observable.create { (obsever: AnyObserver<[String]>) -> Disposable in
//progress.show()
let items = Api.getUsers()
obsever.onNext(items)
obsever.onCompleted()
//progress.hide
return AnonymousDisposable{
print("api dispose called")
}
}
return api
}
Run Code Online (Sandbox Code Playgroud) 因此,我有一些RxSwift代码,我想在其中执行一系列异步操作,所有这些操作均由可观察对象组成。flatMap是这样做的方法,并且效果很好,但是似乎无法将变量传递给我可以确定的链。最好用一些伪代码来说明
假设有3个功能
class Connection {
static func establish(address:String) -> Observable<Connection>
func sendData(data:String) -> Observable<Int> // num bytes written or something
func close() -> Observable<Void>
}
Run Code Online (Sandbox Code Playgroud)
我想将它们称为链,以便我们连接,发送然后关闭。像这样
Connection.establish(host)
.flatMap{ connection in connection.sendData("foo") }
.flatMap{ numBytes in ????.close() }
.subscribeNext{ /* all done */ }
Run Code Online (Sandbox Code Playgroud)
问题在于,flatMap它不会沿链传递其输入参数,因此传递给的闭包subscribeNext无法访问该connection对象,因此无法调用close。
我可以像下面这样做一些骇人的骇客,但我真的不愿意!
var connection:Connection?
Connection.establish(host)
.flatMap{ c in
connection = c
return c.sendData("foo")
}
.flatMap{ numBytes in connection!.close() }
.subscribeNext{ /* all done */ }
Run Code Online (Sandbox Code Playgroud)
在Rx的C#版本中,这是通过使用重载来解决的,该重载SelectMany需要第二次闭合,该闭合将2个值组合在一起(通常合并为一个Tuple),然后将该 …
我正在寻找这样的东西:
let observable = PublishSubject<String>()
observable.onNext("1")
observable.onCompleted()
_ = observable.subscribeNext { s in
print(s)
}
Run Code Online (Sandbox Code Playgroud)
所以我想在已经完成后订阅Observable并仍然获取值(或者只是最后一个值).
我有一个struct Person和person数组如下
struct Person {
let name : String
let age : Int
}
let personArray = [
Person(name : "Max", age : 32),
Person(name : "Jones", age : 42),
Person(name : "Other", age : 62)
]
Run Code Online (Sandbox Code Playgroud)
我创建了一个可观察的序列,如下所示
let seq = Observable.just(personArray)
Run Code Online (Sandbox Code Playgroud)
现在如何按相关人名过滤此数组?
例如,我想要一个包含name以'M'开头的人的可观察序列
这该怎么做 ??
passWordInputView.inputTextField.rx.controlEvent(.editingDidEnd)
.bindTo(loginButton.rx.tap)
.disposed(by: disposeBag)
Run Code Online (Sandbox Code Playgroud)
密码编辑完成后登录
但得到错误:输入'inout UIButton'不符合协议'ReactiveCompatible'
rx-swift ×10
swift ×9
ios ×5
networking ×1
observable ×1
reactivex ×1
swift3 ×1
uitableview ×1