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

issue wrapping async call inside Signal #207

Open
tomerciucran opened this issue Jan 22, 2019 · 0 comments
Open

issue wrapping async call inside Signal #207

tomerciucran opened this issue Jan 22, 2019 · 0 comments

Comments

@tomerciucran
Copy link

I am wrapping my network requests into signals like this:

func request<Response>(_ endpoint: Endpoint<Response>) -> Signal<Response, ResponseError> {
        return Signal<Response, ResponseError> { observer in
            let request = self.manager.request(
                self.url(path: endpoint.path),
                method: httpMethod(from: endpoint.method),
                parameters: endpoint.parameters,
                encoding: endpoint.encoding
            )
            request
                .validate()
                .responseData() { response in
                    let result = response.result.flatMap(endpoint.decode)
                    switch result {
                    case .success(let value):
                        observer.next(value)
                        observer.completed()
                    case .failure(let error):
                        observer.failed(ResponseError(reason: error.localizedDescription,
                                                      statusCode: response.response?.statusCode ?? 0))
                    }
            }
            return BlockDisposable {
                request.cancel()
            }
        }
    }

The issue I am having is that when I am observing this signal, the callback is triggered twice .next with value and .completed. I don't want it to be called twice because obviously I am executing code to update the UI inside the callback.
Sometimes the value is null (for some requests I expect 200 with empty response), so I cannot check the value.
If I don't call .completed after .next, the issue disappears but I not sure if it would cause a memory issue because the signal is being disposed in the case of a .failed or .completed event as seen here:

public func on(_ event: Event<Element, Error>) {
    lock.lock(); defer { lock.unlock() }
    guard !disposable.isDisposed else { return }
    if let observer = observer {
      observer(event)
      if event.isTerminal {
        disposable.dispose()
      }
    }
  } 

Any suggestions?

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

1 participant