Understanding RxSwift Observable Chain
This is my a simplified version of my code:
var myObservable: Observable<MyEnum>
var modelObservable: Observable<Model?>
myObservable = myButton.rx.tap.asSignal()
.asObservable()
.flatMapLatest {
getModel()
}.map { model in
print("this is called")
return model.prop == true ? MyEnum.first : MyEnum.second
}
func getModel() -> Observable<Model?> {
if let model = self.model.value {
return Observable.just(model)
}
createModel()
return modelObservable
}
myObservable.subscribe(onNext: { (enum) in
switch enum {
case .first:
self.presentFirst()
case .second:
self.presentSecond()
}
}).disposed(by: bag)
I was expecting this code to mean that whenever myButton is tapped, this code would run and print "this is called", however, "this is called" is printed also when myOtherObservable is triggered, even when myButton is not tapped. Why does this happen? This makes me think I don't understand Rx. Also, how would I make it behave so that it only runs when the myButton is tapped?
swift observable reactive-programming rx-swift
|
show 14 more comments
This is my a simplified version of my code:
var myObservable: Observable<MyEnum>
var modelObservable: Observable<Model?>
myObservable = myButton.rx.tap.asSignal()
.asObservable()
.flatMapLatest {
getModel()
}.map { model in
print("this is called")
return model.prop == true ? MyEnum.first : MyEnum.second
}
func getModel() -> Observable<Model?> {
if let model = self.model.value {
return Observable.just(model)
}
createModel()
return modelObservable
}
myObservable.subscribe(onNext: { (enum) in
switch enum {
case .first:
self.presentFirst()
case .second:
self.presentSecond()
}
}).disposed(by: bag)
I was expecting this code to mean that whenever myButton is tapped, this code would run and print "this is called", however, "this is called" is printed also when myOtherObservable is triggered, even when myButton is not tapped. Why does this happen? This makes me think I don't understand Rx. Also, how would I make it behave so that it only runs when the myButton is tapped?
swift observable reactive-programming rx-swift
What ismyOtherObservable
, and how is it related tomyButton
(i.e. why are you trying toflatMapLatest
it in the first place?)
– Alexander
Jan 3 at 20:30
Give this a read first, and let me know if you still have questions medium.com/@navdeepsingh_2336/…
– Alexander
Jan 3 at 20:34
Hey @Alexander okay I updated it to be more in line with what I'm doing. When the button is tapped, I ultimately want to navigate somewhere, but not before getting the current model or fetching a new one.
– natecraft1
Jan 3 at 21:02
1
I have no experience with Swift, but are you subscribing tomyObservable
anywhere? Somewhere you will need to subscribe to it to then perform the navigation.
– GregL
Jan 3 at 21:21
This code doesn't print anything because there aren't anysubscribe
s in it. Post a minimal runnable example. And tell us how the actual output differed from what you expected.
– Daniel T.
Jan 4 at 1:46
|
show 14 more comments
This is my a simplified version of my code:
var myObservable: Observable<MyEnum>
var modelObservable: Observable<Model?>
myObservable = myButton.rx.tap.asSignal()
.asObservable()
.flatMapLatest {
getModel()
}.map { model in
print("this is called")
return model.prop == true ? MyEnum.first : MyEnum.second
}
func getModel() -> Observable<Model?> {
if let model = self.model.value {
return Observable.just(model)
}
createModel()
return modelObservable
}
myObservable.subscribe(onNext: { (enum) in
switch enum {
case .first:
self.presentFirst()
case .second:
self.presentSecond()
}
}).disposed(by: bag)
I was expecting this code to mean that whenever myButton is tapped, this code would run and print "this is called", however, "this is called" is printed also when myOtherObservable is triggered, even when myButton is not tapped. Why does this happen? This makes me think I don't understand Rx. Also, how would I make it behave so that it only runs when the myButton is tapped?
swift observable reactive-programming rx-swift
This is my a simplified version of my code:
var myObservable: Observable<MyEnum>
var modelObservable: Observable<Model?>
myObservable = myButton.rx.tap.asSignal()
.asObservable()
.flatMapLatest {
getModel()
}.map { model in
print("this is called")
return model.prop == true ? MyEnum.first : MyEnum.second
}
func getModel() -> Observable<Model?> {
if let model = self.model.value {
return Observable.just(model)
}
createModel()
return modelObservable
}
myObservable.subscribe(onNext: { (enum) in
switch enum {
case .first:
self.presentFirst()
case .second:
self.presentSecond()
}
}).disposed(by: bag)
I was expecting this code to mean that whenever myButton is tapped, this code would run and print "this is called", however, "this is called" is printed also when myOtherObservable is triggered, even when myButton is not tapped. Why does this happen? This makes me think I don't understand Rx. Also, how would I make it behave so that it only runs when the myButton is tapped?
swift observable reactive-programming rx-swift
swift observable reactive-programming rx-swift
edited Jan 4 at 2:32
natecraft1
asked Jan 3 at 18:33
natecraft1natecraft1
1,10742646
1,10742646
What ismyOtherObservable
, and how is it related tomyButton
(i.e. why are you trying toflatMapLatest
it in the first place?)
– Alexander
Jan 3 at 20:30
Give this a read first, and let me know if you still have questions medium.com/@navdeepsingh_2336/…
– Alexander
Jan 3 at 20:34
Hey @Alexander okay I updated it to be more in line with what I'm doing. When the button is tapped, I ultimately want to navigate somewhere, but not before getting the current model or fetching a new one.
– natecraft1
Jan 3 at 21:02
1
I have no experience with Swift, but are you subscribing tomyObservable
anywhere? Somewhere you will need to subscribe to it to then perform the navigation.
– GregL
Jan 3 at 21:21
This code doesn't print anything because there aren't anysubscribe
s in it. Post a minimal runnable example. And tell us how the actual output differed from what you expected.
– Daniel T.
Jan 4 at 1:46
|
show 14 more comments
What ismyOtherObservable
, and how is it related tomyButton
(i.e. why are you trying toflatMapLatest
it in the first place?)
– Alexander
Jan 3 at 20:30
Give this a read first, and let me know if you still have questions medium.com/@navdeepsingh_2336/…
– Alexander
Jan 3 at 20:34
Hey @Alexander okay I updated it to be more in line with what I'm doing. When the button is tapped, I ultimately want to navigate somewhere, but not before getting the current model or fetching a new one.
– natecraft1
Jan 3 at 21:02
1
I have no experience with Swift, but are you subscribing tomyObservable
anywhere? Somewhere you will need to subscribe to it to then perform the navigation.
– GregL
Jan 3 at 21:21
This code doesn't print anything because there aren't anysubscribe
s in it. Post a minimal runnable example. And tell us how the actual output differed from what you expected.
– Daniel T.
Jan 4 at 1:46
What is
myOtherObservable
, and how is it related to myButton
(i.e. why are you trying to flatMapLatest
it in the first place?)– Alexander
Jan 3 at 20:30
What is
myOtherObservable
, and how is it related to myButton
(i.e. why are you trying to flatMapLatest
it in the first place?)– Alexander
Jan 3 at 20:30
Give this a read first, and let me know if you still have questions medium.com/@navdeepsingh_2336/…
– Alexander
Jan 3 at 20:34
Give this a read first, and let me know if you still have questions medium.com/@navdeepsingh_2336/…
– Alexander
Jan 3 at 20:34
Hey @Alexander okay I updated it to be more in line with what I'm doing. When the button is tapped, I ultimately want to navigate somewhere, but not before getting the current model or fetching a new one.
– natecraft1
Jan 3 at 21:02
Hey @Alexander okay I updated it to be more in line with what I'm doing. When the button is tapped, I ultimately want to navigate somewhere, but not before getting the current model or fetching a new one.
– natecraft1
Jan 3 at 21:02
1
1
I have no experience with Swift, but are you subscribing to
myObservable
anywhere? Somewhere you will need to subscribe to it to then perform the navigation.– GregL
Jan 3 at 21:21
I have no experience with Swift, but are you subscribing to
myObservable
anywhere? Somewhere you will need to subscribe to it to then perform the navigation.– GregL
Jan 3 at 21:21
This code doesn't print anything because there aren't any
subscribe
s in it. Post a minimal runnable example. And tell us how the actual output differed from what you expected.– Daniel T.
Jan 4 at 1:46
This code doesn't print anything because there aren't any
subscribe
s in it. Post a minimal runnable example. And tell us how the actual output differed from what you expected.– Daniel T.
Jan 4 at 1:46
|
show 14 more comments
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54027908%2funderstanding-rxswift-observable-chain%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54027908%2funderstanding-rxswift-observable-chain%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What is
myOtherObservable
, and how is it related tomyButton
(i.e. why are you trying toflatMapLatest
it in the first place?)– Alexander
Jan 3 at 20:30
Give this a read first, and let me know if you still have questions medium.com/@navdeepsingh_2336/…
– Alexander
Jan 3 at 20:34
Hey @Alexander okay I updated it to be more in line with what I'm doing. When the button is tapped, I ultimately want to navigate somewhere, but not before getting the current model or fetching a new one.
– natecraft1
Jan 3 at 21:02
1
I have no experience with Swift, but are you subscribing to
myObservable
anywhere? Somewhere you will need to subscribe to it to then perform the navigation.– GregL
Jan 3 at 21:21
This code doesn't print anything because there aren't any
subscribe
s in it. Post a minimal runnable example. And tell us how the actual output differed from what you expected.– Daniel T.
Jan 4 at 1:46