RxSwift 等待 observable 完成,并且 return observable
RxSwift wait for observable to complete, and return observable
当我使用 loadMovies 函数时,只有在 ViewController 调用函数并处理它后我才能获得所需的电影,此函数在 API
中
func loadMovies() -> Observable<[Movie]> {
let obsMovies = self.APICall(genrePath: Movies.action, structType: Movie.self)
return Observable.create{ observer -> Disposable in
obsMovies.subscribe(
onNext: { element in
observer.onNext(element)
print(element)
}
)
observer.onCompleted()
return Disposables.create()
}
}
在ViewController中我在viewDidLoad
中调用了这个函数
request.api.loadMovies()
.debug()
.subscribe(
onNext:{ data in
do {
print(data)
}
},
onError: { error in
print(error)
},
onCompleted: {
print("Completed")
},
onDisposed: {
print("Disposed")
}
)
.disposed(by: disposeBag)
并得到这个输出
ViewController.swift:55 (viewDidLoad()) -> subscribed
ViewController.swift:55 (viewDidLoad()) -> Event completed
Completed
Disposed
ViewController.swift:55 (viewDidLoad()) -> isDisposed
[MovieTest.Movie(name: "Black Panther", year: "2018", rating: "83")]
[MovieTest.Movie(name: "AVENGERS: ENDGAME", year: "2019", rating: "90")]
[MovieTest.Movie(name: "MISSION: IMPOSSIBLE -- FALLOUT", year: "2018", rating: "88")]...
我想知道如何等待函数在被获取电影之前 disposed.I 想在 tableView 中显示所有内容。我是 RxSwift 的初学者,我相信有更好的方法来实现这一点,但截至目前我还不知道。谢谢
因此有几个问题需要立即解决...obsMovies.subscribe()
return这是您忽略的一次性用品。由于它嵌入的闭包需要 return 一个一次性的,如何只 returning 订阅创建的而不是创建一个新的:
func loadMovies() -> Observable<[Movie]> {
let obsMovies = self.APICall(genrePath: Movies.action, structType: Movie.self)
return Observable.create{ observer -> Disposable in
let disposable = obsMovies.subscribe(
onNext: { element in
observer.onNext(element)
print(element)
}
)
observer.onCompleted()
return disposable
}
}
接下来,请注意 APICall(genrePath:structType:)
return 是一个 Observable<[Movie]>
那么为什么不直接 return 而不是将其包装在另一个 Observable 中呢?
func loadMovies() -> Observable<[Movie]> {
return self.APICall(genrePath: Movies.action, structType: Movie.self)
}
哇,现在我有点想知道为什么 loadMovies()
存在...
现在我已经完成了上述操作,这是我看到的输出:
ViewController.swift:55 (viewDidLoad()) -> subscribed
ViewController.swift:55 (viewDidLoad()) -> Event next([MovieTest.Movie(name: "Black Panther", year: "2018", rating: "83"), MovieTest.Movie(name: "AVENGERS: ENDGAME", year: "2019", rating: "90"), MovieTest.Movie(name: "MISSION: IMPOSSIBLE -- FALLOUT", year: "2018", rating: "88")])
[MovieTest.Movie(name: "Black Panther", year: "2018", rating: "83"), MovieTest.Movie(name: "AVENGERS: ENDGAME", year: "2019", rating: "90"), MovieTest.Movie(name: "MISSION: IMPOSSIBLE -- FALLOUT", year: "2018", rating: "88")]
ViewController.swift:55 (viewDidLoad()) -> Event completed
Completed
Disposed
2021-04-11 20:05:08.356: Stuff.swift:55 (viewDidLoad()) -> isDisposed
当我使用 loadMovies 函数时,只有在 ViewController 调用函数并处理它后我才能获得所需的电影,此函数在 API
中func loadMovies() -> Observable<[Movie]> {
let obsMovies = self.APICall(genrePath: Movies.action, structType: Movie.self)
return Observable.create{ observer -> Disposable in
obsMovies.subscribe(
onNext: { element in
observer.onNext(element)
print(element)
}
)
observer.onCompleted()
return Disposables.create()
}
}
在ViewController中我在viewDidLoad
中调用了这个函数
request.api.loadMovies()
.debug()
.subscribe(
onNext:{ data in
do {
print(data)
}
},
onError: { error in
print(error)
},
onCompleted: {
print("Completed")
},
onDisposed: {
print("Disposed")
}
)
.disposed(by: disposeBag)
并得到这个输出
ViewController.swift:55 (viewDidLoad()) -> subscribed
ViewController.swift:55 (viewDidLoad()) -> Event completed
Completed
Disposed
ViewController.swift:55 (viewDidLoad()) -> isDisposed
[MovieTest.Movie(name: "Black Panther", year: "2018", rating: "83")]
[MovieTest.Movie(name: "AVENGERS: ENDGAME", year: "2019", rating: "90")]
[MovieTest.Movie(name: "MISSION: IMPOSSIBLE -- FALLOUT", year: "2018", rating: "88")]...
我想知道如何等待函数在被获取电影之前 disposed.I 想在 tableView 中显示所有内容。我是 RxSwift 的初学者,我相信有更好的方法来实现这一点,但截至目前我还不知道。谢谢
因此有几个问题需要立即解决...obsMovies.subscribe()
return这是您忽略的一次性用品。由于它嵌入的闭包需要 return 一个一次性的,如何只 returning 订阅创建的而不是创建一个新的:
func loadMovies() -> Observable<[Movie]> {
let obsMovies = self.APICall(genrePath: Movies.action, structType: Movie.self)
return Observable.create{ observer -> Disposable in
let disposable = obsMovies.subscribe(
onNext: { element in
observer.onNext(element)
print(element)
}
)
observer.onCompleted()
return disposable
}
}
接下来,请注意 APICall(genrePath:structType:)
return 是一个 Observable<[Movie]>
那么为什么不直接 return 而不是将其包装在另一个 Observable 中呢?
func loadMovies() -> Observable<[Movie]> {
return self.APICall(genrePath: Movies.action, structType: Movie.self)
}
哇,现在我有点想知道为什么 loadMovies()
存在...
现在我已经完成了上述操作,这是我看到的输出:
ViewController.swift:55 (viewDidLoad()) -> subscribed
ViewController.swift:55 (viewDidLoad()) -> Event next([MovieTest.Movie(name: "Black Panther", year: "2018", rating: "83"), MovieTest.Movie(name: "AVENGERS: ENDGAME", year: "2019", rating: "90"), MovieTest.Movie(name: "MISSION: IMPOSSIBLE -- FALLOUT", year: "2018", rating: "88")])
[MovieTest.Movie(name: "Black Panther", year: "2018", rating: "83"), MovieTest.Movie(name: "AVENGERS: ENDGAME", year: "2019", rating: "90"), MovieTest.Movie(name: "MISSION: IMPOSSIBLE -- FALLOUT", year: "2018", rating: "88")]
ViewController.swift:55 (viewDidLoad()) -> Event completed
Completed
Disposed
2021-04-11 20:05:08.356: Stuff.swift:55 (viewDidLoad()) -> isDisposed