【问题标题】:RxSwift subscribe block not called未调用 RxSwift 订阅块
【发布时间】:2015-09-28 14:43:15
【问题描述】:

我正在玩 RxSwift,但我被一个简单的玩具程序困住了。我的程序本质上包含一个模型类和一个视图控制器。该模型包含一个在异步网络调用后在主队列上更新的可观察对象,视图控制器在 viewDidLoad() 中订阅。 AppDelegate 初始化模型并将其传递给 ViewController 并触发网络请求。

class GalleryModel {

    var galleryCount: BehaviorSubject<Int>

    init() {
        galleryCount = BehaviorSubject.init(value:0)
    }

    func refresh() {
         doAsyncRequestToAmazonWithCompletion { (response) -> AnyObject! in
             var counter = 0
             //process response
             counter = 12

             dispatch_async(dispatch_get_main_queue()) {
                self.galleryCount.on(.Next(counter))
             }
             return nil
        }
    }

class ViewController: UIViewController {

    @IBOutlet weak var label: UILabel!

    var galleryModel: GalleryModel?

    override func viewDidLoad() {
        super.viewDidLoad()
        galleryModel?.galleryCount.subscribe { e in
            if let gc = e.element {
               self.label.text = String(gc)
            }
        }
   }
}

class AppDelegate: UIResponder, UIApplicationDelegate {
    var galleryModel: GalleryModel?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {        
        //do amazon setup        
        galleryModel = GalleryModel()
        if let viewController = window?.rootViewController as? ViewController {
            viewController.galleryModel = GalleryModel()
        }    
        return true
    }

    func applicationDidBecomeActive(application: UIApplication) {
        galleryModel?.refresh()
    }

标签只更新一个,它显示“0”。我预计标签会更新两次,第一次更新后显示“0”,处理网络请求后第二次更新后显示“12”。 dispatch_async 块中的断点被命中,但似乎 galleryCount 失去了它的观察者。有人知道发生了什么或如何调试吗?

最好的

【问题讨论】:

  • 你能确认GalleryModel中的On Next被调用了吗?
  • 事情/问题是 RxSwift 是如何编写的,我的意思是,一旦调用了 .on(.Error()),它就会移除观察者并且无法再次调用 .on(.Next())。这样做的唯一方法是创建viewModel 的新实例并再次调用subscribe。我真的很讨厌这个图书馆 {>_

标签: ios swift rx-swift


【解决方案1】:

以防万一有人感兴趣。这是一个重构错误,在重命名变量后,我停止将 observable 传递给 ViewController。相反,我创建了一个新的... facepalm

【讨论】:

    【解决方案2】:

    Here 是在 RxSwift 中订阅的一些有用的 sn-ps(日文)

    例如订阅不同​​的事件:

    let source: Observable<Int> = create { (observer: ObserverOf<Int>) in
        sendNext(observer, 42)
        sendCompleted(observer)
        return AnonymousDisposable {
            print("disposed")
        }
    }
    
    let subscription = source.subscribe { (event: Event<Int>) -> Void in
        switch event {
        case .Next(let element):
            print("Next: \(element)")
        case .Completed:
            print("Completed")
        case .Error(let error):
            print("Error: \(error)")
        }
    }
    

    【讨论】:

      【解决方案3】:

      Clean and Build 为我解决了问题

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多