Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initiating a step from a completion inside of a sink for Future Published (Combine) #193

Closed
maxgarmar opened this issue Jul 7, 2023 · 5 comments

Comments

@maxgarmar
Copy link

maxgarmar commented Jul 7, 2023

Hi Guys,

I am working in a new applications trying your library and there is something really weird.
I am using combine to make async calls to rest api (specifically Future Publisher).
When I complete a call with and return the value it goes to the completion code of the sink, example like the following

repositoryManager.fetchAuthSession()
                    .receive(on: DispatchQueue.main)
                    .sink( receiveCompletion: { [self] completion in
                    switch completion {
                    case .finished:
                        break
                    case .failure:
                        logger.w("Error fetchAuthSession expected because User SignedOut or the app just started")
                        // show onboarding or login
                        if userDataManager.getOnBoardingShownUserDefaults() ?? false {
                            logger.i("Redirecting -> login")
                            navigateToLogin()
                        } else {
                            logger.i("Redirecting -> Presentation Slide")
                            navigateToPresentationSlide()
                        }
                    }
                }, receiveValue: { [self]  in
                    logger.i("FetchAuthSession Success! Token stored")
                    // go to home or whatever
                }).store(in: &subscriptions)

when I go to the sink I need to react and do some redirection to a new Screen
as you can see I am using the receive(on: DispatchQueue.main) so the completion code runs on the main thread, what is suppose to be the right for your Step to run like the following:

 func navigateToLogin() {
            self.steps.accept(AppStep.loginIsRequired)
    }

but interesting enough, if I do the code like above, the flow does not react to the step and the navigation does not work.
Now if I do this:

 func navigateToLogin() {
        DispatchQueue.main.asyncAfter(deadline: .now() + 1) { [unowned self] in
            self.steps.accept(AppStep.loginIsRequired)
        }
    }

Then the flow is triggered perfectly and it navigates to the next screen without changing a single line more in the code.

That tells like ... the code does not run in the main thread? but actually it does because I made this

guard Thread.isMainThread else {
        fatalError("Code is not executing on the main thread")
    }
    self.steps.accept(navigationStep)

to make sure I was on the main thread, so it was but still RxFlow does not react.

Any suggestion? am I doing something wrong or unexpected?

Thanks and appreciate your work for this library.

Cheers

Maximo

@mgray88
Copy link
Collaborator

mgray88 commented Jul 7, 2023

My understanding of Combine is limited, other than its similarity to RxSwift in functionality. The red flag here in my opinion is the .now() + 1. That sounds to me like maybe RxFlow isn't ready to accept steps at the time you attempt to send them. Have you looked at #134? I myself faced a similar issue a while back; I ended up just working around it.

Can you create a small sample project that recreates the issue?

@maxgarmar
Copy link
Author

maxgarmar commented Jul 8, 2023

Hey @mgray88

Thanks for coming back so quick.

Yes, I checked that issue but not sure if it was related and the workaround is not clear.
Could you please share with me the workaround you did in the meantime I can prepare some example? I’m bit busy lately.
The workaround I did only works when I make async.after + delay. If I don’t make a delay, sometimes does work and sometimes doesn’t.

Thanks

@mgray88
Copy link
Collaborator

mgray88 commented Jul 8, 2023

It sounds quite similar to the other issue, especially when you mention the sometimes works, sometimes doesn't when there is no delay. Our workaround to the original issue in #134 was to use main.async which then caused the "sometimes works" problem.

@ssaluk's workaround posted in this comment was to move logic to viewDidAppear since it seemed the issue surrounded rxVisible

@github-actions
Copy link

github-actions bot commented Sep 7, 2023

This issue has not received any recent updates.
We encourage you to check if this is still an issue after
the latest release and if you find that this is still a problem,
please leave a comment below and auto-close will be canceled.

@github-actions github-actions bot added the stale label Sep 7, 2023
@github-actions
Copy link

This issue has automatically been closed due to inactivity.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Sep 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants