简体   繁体   中英

RxSwift subscribe block not called

I'm playing around with RxSwift and I'm stuck with a simple toy programm. My program essentially contains a model class and a viewcontroller. The model contains an observable that gets updated on the main queue after an asynchronous network call, the viewcontroller subscribes in viewDidLoad(). The AppDelegate initializes the model and passes it to ViewController and triggers the network request.

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". 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. A breakpoint in the dispatch_async block gets hit, but it seems that galleryCount lost its observer. 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. Instead I created a new one... facepalm

Here are some useful snippets for subscribe in RxSwift (in Japanese)

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为我解决了问题

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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