简体   繁体   中英

RXSwift: Subscriber never gets call back

I have this function:

    func makeRepoRequest() -> Single<[String: Any]> {
        return Single<[String: Any]>.create {[weak self] observer in
            guard let something = self?.temp else {
                let disposeBag = DisposeBag()
                self?.getRepo("364").subscribe(onSuccess: { content in
                    observer(.success(content))
                }, onError: { error in
                    observer(.error(error))
                }).disposed(by: disposeBag)
                return Disposables.create()
            }
            observer(.success(something))
            return Disposables.create()

        }
    }

is subscribe to this function:

    func getRepo(_ repo: String) -> Single<[String: Any]> {
        return Single<[String: Any]>.create { single in
            print(repo)
            let url = "https://api.github.com/repositories?since=\(repo)"
            print(url)
            let task = URLSession.shared.dataTask(with: URL(string:url)!) { data, _, error in
                if let error = error {
                    single(.error(error))
                    return
                }
                guard let data = data,
                    let json = try? JSONSerialization.jsonObject(with: data, options: []),
                    let result = json as? [String: Any] else {
                        let error = NSError(domain: "Decoding", code: 0, userInfo: nil)
                        single(.error(error))
                        return
                }

                single(.success(result))
            }
            task.resume()
            return Disposables.create()
        }
    }

but for some reason the subscription it never gets a call back. Any of you knows why the subscription never gets a call back?

I'll really appreciate your help.

Your makeRepoRequest() is defined incorrectly. The disposable you create inside the closure should be the one that you return. There shouldn't be any disposeBag in there, also you need to unwrap self and make sure something is emitted if self doesn't exist, also if you are going to keep a cache in temp you really should assign to it...

func makeRepoRequest() -> Single<[String: Any]> {
    return Single<[String: Any]>.create { [weak self] observer in
        guard let this = self else {
            observer(.error(MyError.missingSelf))
            return Disposables.create()
        }
        guard !this.temp.isEmpty else {
            return this.getRepo("364").subscribe(onSuccess: { content in
                this.temp = content
                observer(.success(content))
            }, onError: { error in
                observer(.error(error))
            })
        }
        observer(.success(this.temp))
        return Disposables.create()

    }
}

However, since you are just emitting content with no changes, you don't even need to use .create(_:) . So something like this:

func makeRepoRequest() -> Single<[String: Any]> {
    if !temp.isEmpty {
        return getRepo("364")
            .do(onSuccess: { [weak self] in self?.temp = $0 })
    }
    else {
        return Single.just(temp)
    }
}

Lastly, you aren't properly canceling your network request in your getRepo(_:) method. It should end with return Disposables.create { task.cancel() }

I suggest you read up more on Disposables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM