{"action":"created","issue":{"url":"https://api.github.com/repos/ReactiveKit/ReactiveKit/issues/48","repository_url":"https://api.github.com/repos/ReactiveKit/ReactiveKit","labels_url":"https://api.github.com/repos/ReactiveKit/ReactiveKit/issues/48/labels{/name}","comments_url":"https://api.github.com/repos/ReactiveKit/ReactiveKit/issues/48/comments","events_url":"https://api.github.com/repos/ReactiveKit/ReactiveKit/issues/48/events","html_url":"https://github.com/ReactiveKit/ReactiveKit/issues/48","id":141303134,"number":48,"title":"Operation .Success never called when using `flatMap`: what am I doing wrong here? ","user":{"login":"iandundas","id":1131967,"avatar_url":"https://avatars.githubusercontent.com/u/1131967?v=3","gravatar_id":"","url":"https://api.github.com/users/iandundas","html_url":"https://github.com/iandundas","followers_url":"https://api.github.com/users/iandundas/followers","following_url":"https://api.github.com/users/iandundas/following{/other_user}","gists_url":"https://api.github.com/users/iandundas/gists{/gist_id}","starred_url":"https://api.github.com/users/iandundas/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/iandundas/subscriptions","organizations_url":"https://api.github.com/users/iandundas/orgs","repos_url":"https://api.github.com/users/iandundas/repos","events_url":"https://api.github.com/users/iandundas/events{/privacy}","received_events_url":"https://api.github.com/users/iandundas/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2016-03-16T15:07:57Z","updated_at":"2016-03-17T08:25:39Z","closed_at":null,"body":"Hi! Would be very grateful if anyone could let me know what I'm doing wrong here:\r\n\r\n[github demo project](https://github.com/iandundas/ReactiveKitOperationHelp/blob/master/FlatmapExample/AppDelegate.swift)\r\n\r\nThe below code is a simple Operation, which is called via a `flatMap`. The Operation simply calls `observer.success()`. I'd expect it to call `print(\"success\")` - but this is never reached. If, however, I swap the line of code within the Operation to call `.next(\"..\")`, this does reach the `observe` block.\r\n\r\nCould anyone tell me why `case .success` doesn't get called in the` observe` block?\r\n\r\n```swift\r\nimport ReactiveKit\r\n\r\n@UIApplicationMain\r\nclass AppDelegate: UIResponder, UIApplicationDelegate {\r\n\r\n    var window: UIWindow?\r\n\r\n    var disposable: DisposableType! // just in case\r\n    \r\n    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {\r\n        \r\n        func successOperation() -> Operation<String, NSError>{\r\n            return Operation{observer in\r\n                \r\n                // observer.next(\"this *does* reach the observe block\")\r\n                \r\n                observer.success() // this never reaches the observe block\r\n                \r\n                return SimpleDisposable()\r\n            }\r\n        }\r\n        \r\n        let a = ActiveStream<String>()\r\n        let b = a.flatMap(OperationFlatMapStrategy.Latest) { whatever -> Operation<String, NSError> in\r\n            return successOperation()\r\n        }\r\n        \r\n        disposable = b.observe { event in\r\n            switch event{\r\n            case .Success:\r\n                print(\"success\") // never called\r\n                \r\n            default:\r\n                print(\"anything else\")\r\n            }\r\n        }\r\n        \r\n        a.next(\"sending something\")\r\n        \r\n        return true\r\n    }\r\n}\r\n```\r\n\r\nNote, if I just observe the Operation directly, it does work:\r\n\r\n```swift\r\ndisposable = successOperation().observe { event in\r\n    switch event{\r\n    case .Success:\r\n        print(\"success\") // this IS called now.\r\n        \r\n    default:\r\n        print(\"anything else\")\r\n    }\r\n}\r\n```"},"comment":{"url":"https://api.github.com/repos/ReactiveKit/ReactiveKit/issues/comments/197757387","html_url":"https://github.com/ReactiveKit/ReactiveKit/issues/48#issuecomment-197757387","issue_url":"https://api.github.com/repos/ReactiveKit/ReactiveKit/issues/48","id":197757387,"user":{"login":"srdanrasic","id":1660804,"avatar_url":"https://avatars.githubusercontent.com/u/1660804?v=3","gravatar_id":"","url":"https://api.github.com/users/srdanrasic","html_url":"https://github.com/srdanrasic","followers_url":"https://api.github.com/users/srdanrasic/followers","following_url":"https://api.github.com/users/srdanrasic/following{/other_user}","gists_url":"https://api.github.com/users/srdanrasic/gists{/gist_id}","starred_url":"https://api.github.com/users/srdanrasic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/srdanrasic/subscriptions","organizations_url":"https://api.github.com/users/srdanrasic/orgs","repos_url":"https://api.github.com/users/srdanrasic/repos","events_url":"https://api.github.com/users/srdanrasic/events{/privacy}","received_events_url":"https://api.github.com/users/srdanrasic/received_events","type":"User","site_admin":false},"created_at":"2016-03-17T08:25:39Z","updated_at":"2016-03-17T08:25:39Z","body":"That possible only if you expect the button to be tapped just once. If you can guaranty that, that you can do something like:\r\n\r\n```swift\r\n  let b = a.flatMap(OperationFlatMapStrategy.Latest) { _ in\r\n    Operation<Int, NoError>.succeeded(with: 0)\r\n  }.take(1).flatMap(OperationFlatMapStrategy.Latest) { _ in\r\n    return successOperation()\r\n  }\r\n```\r\n\r\nFirst we flatMap stream to a dummy operation that just sends zero on each event. We'll ignore that. Ideally we'd use Void instead of Int, but that's compiler limitation we have to live with until I make some improvements in next version.\r\n\r\nThen we take(1) which just takes first event from the stream (first button tap) and completes.\r\n\r\nFinally we apply your flatMap to, now, our Int Operation that completes after first event.\r\n\r\nIf the button is allowed to be tapped multiple times then it's in its nature never to complete and you should consider different approaches. If I'm understanding, you want to react only after all event from an operation are sent? Maybe you could use collect operator?\r\n\r\n```swift\r\nlet b = a.flatMap(OperationFlatMapStrategy.Latest) { whatever -> Operation<String, NSError> in\r\n  return successOperation().collect()\r\n}\r\n```\r\n\r\nThat will make `b.observeNext` fire next only when each operation completes with all events it has produced in an array."}}