简体   繁体   English

没有调用RxSwift订阅块

[英]RxSwift subscribe block not called

I'm playing around with RxSwift and I'm stuck with a simple toy programm. 我正在玩RxSwift,我被一个简单的玩具程序困住了。 My program essentially contains a model class and a viewcontroller. 我的程序基本上包含一个模型类和一个viewcontroller。 The model contains an observable that gets updated on the main queue after an asynchronous network call, the viewcontroller subscribes in viewDidLoad(). 该模型包含一个observable,它在异步网络调用之后在主队列上更新,viewcontroller在viewDidLoad()中订阅。 The AppDelegate initializes the model and passes it to ViewController and triggers the network request. 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()
    }

The label gets updated only one, it shows "0". 标签只更新一个,显示“0”。 I expected the label to get updated twice, showing "0" after the first update and showing "12" after the second update after the processing of the network request. 我希望标签更新两次,在第一次更新后显示“0”,在处理网络请求后第二次更新后显示“12”。 A breakpoint in the dispatch_async block gets hit, but it seems that galleryCount lost its observer. dispatch_async块中的断点被命中,但似乎galleryCount丢失了它的观察者。 Anybody any idea what's happening or how to debug this? 有人知道发生了什么或如何调试这个?

Best 最好

In case reads this anyone is interested. 如果读取这个人有兴趣。 It was an refactoring error, after renaming variables I stopped passing the observable to the ViewController. 这是一个重构错误,重命名变量后我停止将observable传递给ViewController。 Instead I created a new one... facepalm 相反,我创造了一个新的... facepalm

Here are some useful snippets for subscribe in RxSwift (in Japanese) 以下是RxSwift中订阅的一些有用的片段(日语)

For example to subscribe to different events: 例如,订阅不同的事件:

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)")
    }
}

Clean and Build为我解决了问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM