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

Can you handle when internet disconnect? #46

Open
lexuanquynh opened this issue Sep 13, 2020 · 1 comment
Open

Can you handle when internet disconnect? #46

lexuanquynh opened this issue Sep 13, 2020 · 1 comment

Comments

@lexuanquynh
Copy link

Describe the bug
Can you handle when I disconnect internet and try login button or other API request. The loading will forever and user can not touch any more.

To Reproduce
Steps to reproduce the behavior:

  1. Go to 'Login screen'. Turn of internet connection
  2. Fill data and Click on 'Login button'
  3. See error: loading forever
@manhpham90vn
Copy link

func request(_ token: Target) -> Observable<Moya.Response> {
        let actualRequest = provider.rx.request(token)
        return online
//            .ignore(value: false)  // Wait until we're online
            .take(1)        // Take 1 to make sure we only invoke the API once.
            .flatMap { _ in // Turn the online state into a network request
                return actualRequest
                    .filterSuccessfulStatusCodes()
                    .do(onSuccess: { (response) in
                    }, onError: { (error) in
                        if let error = error as? MoyaError {
                            switch error {
                            case .statusCode(let response):
                                if response.statusCode == 401 {
                                    // Unauthorized
                                    if AuthManager.shared.hasValidToken {
                                        AuthManager.removeToken()
                                        Application.shared.presentInitialScreen(in: Application.shared.window)
                                    }
                                }
                            case .underlying(let error, _):
                                if let error = error as? AFError {
                                    switch error {
                                    case .sessionTaskFailed(let error as NSError):
                                        if error.domain == NSURLErrorDomain && error.code == NSURLErrorTimedOut {
                                            print("timeout")
                                        } else if error.domain == NSURLErrorDomain && error.code == NSURLErrorDataNotAllowed {
                                            print("no connect")
                                        }
                                    default:
                                        break
                                    }
                                }
                            default:
                                break
                            }
                        }
                    })
        }
    }

try this function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants