我是Rx的新手,并且一直在努力建立一个解析服务的Bonjour发现客户端。这非常简单,但我想尝试使用RxSwift。
由于发现的NSNetService对象需要在解析之前保留,我必须进行嵌套的订阅调用,外部的一个用于发现,内部用于解析......但有些东西告诉我这不是最好的方式。
import UIKit
import RxSwift
class BonjourClient: NSObject {
let disposeBag = DisposeBag()
var servicesArray = [NSNetService]()
func startBrowsing() {
let browser = NSNetServiceBrowser()
browser.rx_netServiceBrowserDidFindServiceMoreComing
.subscribeNext { (service: NSNetService) in
self.servicesArray.append(service)
self.servicesArray.last!.rx_netServiceDidResolveAddress
.subscribeNext { (sender: NSNetService) in
print("Resolved \(sender.name)")
let data = sender.TXTRecordData()
let dict: [String: NSData?] = NSNetService.dictionaryFromTXTRecordData(data!)
for (key, value) in dict {
print("\(key) : \(String(data: value!, encoding: NSUTF8StringEncoding)!)")
}
}.addDisposableTo(self.disposeBag)
self.servicesArray.last!.resolveWithTimeout(5)
}.addDisposableTo(disposeBag)
browser.searchForServicesOfType("_amzn-wplay._tcp.", inDomain: "local.")
NSRunLoop.currentRunLoop().run()
}
}
我的代理类如下:
import UIKit
import RxSwift
import RxCocoa
class RxNSNetServiceBrowserDelegateProxy: DelegateProxy, NSNetServiceBrowserDelegate, DelegateProxyType {
static func currentDelegateFor(object: AnyObject) -> AnyObject? {
let browser: NSNetServiceBrowser = object as! NSNetServiceBrowser
return browser.delegate
}
static func setCurrentDelegate(delegate: AnyObject?, toObject object: AnyObject) {
let browser: NSNetServiceBrowser = object as! NSNetServiceBrowser
browser.delegate = delegate as? NSNetServiceBrowserDelegate
}
}
class RxNSNetServiceDelegateProxy: DelegateProxy, NSNetServiceDelegate, DelegateProxyType {
static func currentDelegateFor(object: AnyObject) -> AnyObject? {
let service: NSNetService = object as! NSNetService
return service.delegate
}
static func setCurrentDelegate(delegate: AnyObject?, toObject object: AnyObject) {
let service: NSNetService = object as! NSNetService
service.delegate = delegate as? NSNetServiceDelegate
}
}
extension NSNetServiceBrowser {
public var rx_delegate: DelegateProxy {
return proxyForObject(RxNSNetServiceBrowserDelegateProxy.self, self)
}
public var rx_netServiceBrowserDidFindServiceMoreComing: Observable<NSNetService> {
return rx_delegate.observe("netServiceBrowser:didFindService:moreComing:")
.map { params in
let service = params[1] as! NSNetService
return service
}
}
}
extension NSNetService {
public var rx_delegate: DelegateProxy {
return proxyForObject(RxNSNetServiceDelegateProxy.self, self)
}
public var rx_netServiceDidResolveAddress: Observable<NSNetService> {
return rx_delegate.observe("netServiceDidResolveAddress:")
.map { params in
return params[0] as! NSNetService
}
}
}
如果我在flatMap
来电后使用browser.rx_netServiceBrowserDidFindServiceMoreComing
而不是subscribeNext
,则该服务无法解决,因为我无法将其保留在flatMap
内的数组中出于逃避我的原因,主要是从未接触过Rx。我是否一定要使用嵌套调用?
我的问题的简短版本是上述作品,但似乎对我来说很复杂。任何想法都将不胜感激。
答案 0 :(得分:3)
您可以使用scan
来避免嵌套订阅。它会将NSNetService
中的每个rx_netServiceBrowserDidFindServiceMoreComing
添加到数组中。请注意,在这种情况下,您不必将servicesArray
存储为成员变量,除非您出于其他原因需要它。
然后您可以使用flatMap
,如下所示:
browser.rx_netServiceBrowserDidFindServiceMoreComing
.scan([NSNetService]()) { (services: [NSNetService], service: NSNetService) in
return services + [service]
}.flatMap { (services: [NSNetService]) in
return services.last!.rx_resolveWithTimeout(5)
}.subscribeNext { (sender: NSNetService) in
print("Resolved \(sender.name)")
let data = sender.TXTRecordData()
let dict: [String: NSData?] = NSNetService.dictionaryFromTXTRecordData(data!)
for (key, value) in dict {
print("\(key) : \(String(data: value!, encoding: NSUTF8StringEncoding)!)")
}
}.addDisposableTo(disposeBag)
这需要在NSNetService
扩展名中添加方法,因为您必须从Observable
返回flatMap
:
extension NSNetService {
//existing methods omitted
public func rx_resolveWithTimeout(timeout: NSTimeInterval) -> Observable<NSNetService> {
self.resolveWithTimeout(timeout)
return rx_netServiceDidResolveAddress.filter {
$0 == self
}
}
}