I am using DispatchGroup to download data from 3 different APIs, once this is done I want to return the new created consolidated object from my function. Now Although DispatchGroup is working fine and I am getting data but I am not able to return it to calling function. Following is my function:
func getHowToInfo(materialNo: String) -> Observable<HowToInfo> {
return Observable.create{ observer in
let dispatchGroup = DispatchGroup()
_ = self.getMaterialInfo(materialNo: materialNo).subscribe(onNext:{ material in
let howto = HowToInfo(videos: [], documents: [], applications: [])
if (material.documentTargetId?.count)! > 0 {
dispatchGroup.enter()
_ = self.materialRepo?.API1(targetIDs: material.documentTargetId!).subscribe(onNext:{documents in
howto.documents = documents
dispatchGroup.leave()
}, onError: { (error) in
dispatchGroup.leave()
})
}
if (material.applicationDescription?.count)! > 0 {
dispatchGroup.enter()
_ = self.materialRepo?.API2(materialNo: materialNo).subscribe(onNext:{applications in
howto.applications = applications
dispatchGroup.leave()
}, onError: { (error) in
dispatchGroup.leave()
})
}
if ((material.videoApplicationTargetId?.count) != nil && (material.videoApplicationTargetId?.count)! > 0) {
dispatchGroup.enter()
_ = self.materialRepo?.API3(targetIDs: material.videoApplicationTargetId!).subscribe(onNext:{videos in
howto.videos = videos
dispatchGroup.leave()
}, onError: { (error) in
dispatchGroup.leave()
})
}else if ((material.videoSupportTargetId?.count) != nil && (material.videoSupportTargetId?.count)! > 0) {
dispatchGroup.enter()
_ = self.materialRepo?.API4(targetIDs: material.videoSupportTargetId!).subscribe(onNext:{videos in
howto.videos = videos
dispatchGroup.leave()
}, onError: { (error) in
dispatchGroup.leave()
})
}
dispatchGroup.notify(queue: .main, execute: {
print("All functions complete 👍")
observer.onNext(howto)
observer.onCompleted()
})
})
return Disposables.create()
}
}
calling function:
func loadHowToUseList(materialNo: String){
self.serviceMaterial.getHowToInfo(materialNo: materialNo).subscribe({
howToUse in
print(howToUse)
}).disposed(by: DisposeBag())
}
I am not able to get my object in subscribe method above, it never runs.
Try adding
dispatchGroup.wait()
After your lines
dispatchGroup.notify(queue: .main, execute: {
print("All functions complete 👍")
observer.onNext(howto)
observer.onCompleted()
})
And also, why don't just use Rx operators itself?
Every one of this could be an observer.onNext, then you try to observe on three events of this observable, and there is no need for onCompleted
I think you can achieve desired behaviour using combineLatest and skipWhile operators. Roughly implementation would be like this:
let api1 = Observable.of(["documents"]) //Replace with observable to download docs
let api2 = Observable.of(["applications"]) //Replace with observable to download apps
let api3 = Observable.of(["videos"]) //Replace with observable to download videos
Observable.combineLatest(api1, api2, api3){(docs, apps, videos) in
return (docs, apps, videos)
}.skipWhile{ (docs, apps, videos) in
return docs.count == 0 && apps.count == 0 && videos.count == 0
}.subscribe(onNext:{(docs, apps, videos) in
})
.disposed(by:disposeBag)
Related
Given an array of urls, is there a way to run those at once simultaneously? But in such a way so as soon as there is one success move to the next request using that successful url.
So far I tried chaining concatMap, and failed with zip.
func updateAccountInfo() -> Single<Bool> {
var disposable: Disposable? = nil
return Single<Bool>.create { observer in
do {
var urls = try self.settings.domains.value()
disposable = Observable.from(urls)
.enumerated()
.concatMap { index, url -> Single<URL> in
return self.verifyUrl(url)
}
.concatMap { url -> Single<Account> in
return self.apiManager.loadAccountInfo(from: url)
}
.observeOn(MainScheduler.instance)
.do(onNext: { (account: AccountInfo) in
// use account unfo here
disposable!.dispose()
})
.subscribe()
} catch {
observer(.error(error))
}
return Disposables.create()
}
}
Tried like so too:
disposable = Observable.from(urls)
.enumerated()
.concatMap { index, url -> Single<(Bool, URL)> in
return self.verifyUrl(url)
}
.subscribe(onNext: { reachable, url in
if reachable {
self.apiManager.loadAccountInfo(from: url)
.subscribe(onSuccess: { accountInfo in
// use account info here
}, onError: { error in
})
.disposed(by: self.bag)
disposable!.dispose()
} else {
}
}, onError: { error in
}, onCompleted: {
})
Maybe I use zip but how would I create an array of verifyUrl(url) calls? Does zip accept arrays of Observable at all?
let obs = Observable.from(urls)
.enumerated()
.concatMap { index, url -> Single<URL> in
return self.verifyUrl(url)
}
let test = Observable
.zip(obs).map { [urls] in
return [urls]
}
If I understand the question correctly, you are looking for something like this:
func example() throws {
let urls = try self.settings.domains.value()
Observable.merge(urls.map { verifyUrl($0).asObservable() })
.flatMap { [apiManager] url in
apiManager!.loadAccountInfo(from: url)
}
.observe(on: MainScheduler.instance)
.subscribe(onNext: { account in
// use account unfo here
})
.disposed(by: self.disposeBag)
}
But it's hard to tell. Your code samples are a bit jumbled. Putting all your code in a Single.create is odd. Returning a Disposables.create() from the closure when you have a disposable to return is odd. Calling dispose() on a disposable inside the do block is odd. So much weirdness... I suggest you post some code on https://codereview.stackexchange.com or look at sample code.
I currently have a set of network requests to fire.
The problem is that I need to order the results of them to fit the order I fired them.
My current code is the following:
for url in config.fieldImages.map ({ URL(string: $0)! }) {
self.getWheelFieldImage(url: url)
.takeUntil(.inclusive, predicate: { (_) -> Bool in
images.count == config.fieldImages.count - 1
})
.subscribe(onNext: { (anImage) in
images.append(anImage)
}, onError: { (error) in
completion(nil, nil, error)
}, onCompleted: {
completion(images, false, nil)
self.lastUpdate = Date()
}, onDisposed: {
})
.disposed(by: self.disposeBag)
}
I'm wondering if there is an easy way to order these results in the same order I fired them, using RxSwift.
EDIT:
I try to explain the problem better. I have this array with N URLs and I fire the requests one after the other (1,2,3,4...).
I need to have back the result from these requests in the same order (R1, R2, R3, R4, where R1 is the response from request 1 etc...) to store the images in the resulting array.
I can wait all to finish. No problem.
Without much changes in your original code you can achieve this by use using enumerated() on your urls list as:
/// Prefill your images with nil for each image
var images = Array<Int?>(repeating: nil, count: config.fieldImages.count)
for (index, url) in config.fieldImages.map ({ URL(string: $0)! }).enumerated() {
self.getWheelFieldImage(url: url)
.takeUntil(.inclusive, predicate: { (_) -> Bool in
images.count == config.fieldImages.count - 1
})
.subscribe(onNext: { (anImage) in
images[index] = anImage /// Store on proper position
}, onError: { (error) in
completion(nil, nil, error)
}, onCompleted: {
completion(images, false, nil)
self.lastUpdate = Date()
}, onDisposed: {
})
.disposed(by: self.disposeBag)
}
Probably most RxWay will be use of zip operator as:
let streams = config.fieldImages
.map { URL(string: $0)! }
.map { self.getWheelFieldImage(url: $0) }
let images = Observable.zip(streams) // Observable<[UIImage]>
.subscribe(
onNext: { [weak self] images in
completion(images, false, nil)
self?.lastUpdate = Date()
},
onError: { error in
completion(nil, nil, error)
}
)
.disposed(by: self.disposeBag)
You can read more about zip in documentation
I'm slowly getting my head around completion handlers.
Kind of working backwards if I have a firestore query if I wanted to use a completion handler i'd have to use completion() when the firestore query finishes.
But it's setting up the function that still confuses me.
So if this is a function definition that takes a closure as a parameter:
func doSomethingAsync(completion: () -> ()) {
}
I don't quite get how to go from the above func definition and implementing it for something real like a firestore query and request.
query.getDocuments(){ (querySnapshot, err) in
if let err = err
{
print("Error getting documents: \(err)")
}
else
{
if(querySnapshot?.isEmpty)!
{
print("there's no document")
completion()
}
else
{
for document in querySnapshot!.documents
{
completion()
}
}
}
}
thanks.
update
so for my example could i do something like
func getFirestoreData(userID: String, completion #escaping() -> ()){
//firestore code:
query.getDocuments(){ (querySnapshot, err) in
if let err = err
{
print("executed first")
completion()
}
else
.......
print("executed first")
completion()
}
}
To call the function i'm doing:
getFirestoreData(userID: theUserID) {
print("executes second")
}
print("executes third") after function execution.
What i'd like to happen is the programming awaits the completion() before continuing to execute.
But "executes third" happens first, then "executes first", then "executes second".
Thanks
Here is full example (With API Call)
Note that : status variable is just a key to finger out what is response from server
(0: error from server, 1: success, -1: something wrong in my code)
func logout(handlerBack: #escaping (_ error: Error?, _ status:Int?, _ error_message:String?)->())
{
Alamofire.request(url, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: nil)
.responseJSON { respons in
switch respons.result {
case .failure(let theError):
handlerBack(theError, 0, nil)
case .success(let data):
let json_data = JSON(data)
/// if couldn't fetch data
guard let status = json_data["status"].int else {
handlerBack(nil,-1, "can't find status")
return
}
/// if statuse == 0
guard status == 1 else {
handlerBack (nil, 0 , json_data["errorDetails"].string)
return
}
// that's means everything fine :)
handlerBack(nil, 1 , nil)
}
}
}
And here is the way to implement it :
// call func
self.logout { (error:error, status:Int, error_message:String) in
// if status not 1, figure out the error
guard status == 1 else {
// try to find error from server
guard let error_message = error_message else {
// else error something else
print ("Error at :: \(#function)")
// don't do anything ..just return
return
}
self.showMessageToUser(title: error_message, message: "", ch: {})
return
}
// this part do what ever you want, means every thing allright
}
UPDATE :
You are looking for something wait unit execute "First" and "Second"
in this case use DispatchGroup() here is the example :
var _dispatch_group = DispatchGroup()
getFirestoreData(userID: theUserID) {
_dispatch_group.enter()
print("executes second")
_dispatch_group.leave()
}
_dispatch_group.notify(queue: .main) {
print("executes third")
}
output is :
executes First
executes Second
executes Third
So basically I have two actions I need to execute:
first is login
second is get user profile
They have to be done in right order because getting user profile cannot be done without logging in first.
So I had bunch of code that looked like this:
func signIn(signinParameters: SignInParameters) -> Observable<SignInResult> {
return Observable<SignInResult>.create { [unowned self] observer in
self.signinParameters = signinParameters
self.apiConnector
.signIn(with: signinParameters)
.do(onNext: { [weak self] signinResult in
self!.apiConnector
.get()
.do(onNext: { user in
let realm = RealmManager.shared.newRealm()!
let realmUser = RealmUser()
realmUser.configure(with: user, in: realm)
try? realm.write {
realm.add(realmUser, update: true)
}
self!.setState(.authenticated)
observer.onNext(signinResult)
}, onError: { (error) in
observer.onError(error)
}, onCompleted: {
observer.onCompleted()
}).subscribe()
}, onError: { error in
observer.onError(error)
}, onCompleted: {
print("completed")
observer.onCompleted()
}).subscribe()
return Disposables.create()
}
I know this is not right because I cannot send onNext signal with signin result when both actions are finished. I've been reading and I figured out i need to flatmap both actions, combine them into one signal and then manipulate signinresult but I dont have a clue how to do that. So any help would be nice.
Thank you
EDIT 1:
so I've refactored code to look something like this, but there is still problem that I can't send signal when BOTH actions are finished, or am I wrong?
func signIn(signinParameters: SignInParameters) -> Observable<SignInResult> {
return Observable<SignInResult>.create { [unowned self] observer in
self.signinParameters = signinParameters
self.apiConnector
.signIn(with: signinParameters)
.do(onNext: { (result) in
}, onError: { (error) in
}, onCompleted: {
})
.flatMap({ (result) -> Observable<User> in
self.apiConnector.get().asObservable()
})
.do(onNext: { (user) in
}, onError: { (error) in
}, onCompleted: {
}).subscribe()
return Disposables.create()
}
}
Your code is not very clean and it is hard to understand what is going on (my opinion).
If you need two actions to be executed you can create two functions:
struct Parameters{}
struct Profile{}
struct User{}
func login(parameters: Parameters) -> Observable<User> {
// get user
}
func profile(user: User) -> Observable<Profile> {
// get profile
}
func serial(parameters: Parameters) -> Observable<Profile> {
return login(parameters: parameters).flatMap({ profile(user: $0) })
}
login function or profile function can be also split into smaller functions if required:
func profileStored(user: User) -> Observable<Profile?> {
// get stored profile
}
func profileRequested(user: User) -> Observable<Profile> {
// get profile from network
}
func profile(user: User) -> Observable<Profile> {
let observable = profileStored(user: user)
.shareReplayLatestWhileConnected()
let observableStored = observable
.filter({ $0 != nil })
.map({ $0! })
.shareReplayLatestWhileConnected()
let observableRequested = observable
.filter({ $0 == nil })
.flatMap({ _ in profileRequested(user: user) })
.shareReplayLatestWhileConnected()
return Observable
.of(observableStored, observableRequested)
.merge()
.shareReplayLatestWhileConnected()
}
As a result you can mix smaller functions with flatMap or any other operator.
That is how I do it. Hope it'll be helpful
I'm trying to achieve a reactive way to perform some operations:
Request a photo download
Get the download progress from next events
When completed then save that photo locally
So I started with RxSwift and implemented it like
photoController.downloadPhoto(photoItem.photo)
.doOnNext { downloadTaskInfo in
photoItem.viewState = .NetworkProgress(task: downloadTaskInfo.task, progress: downloadTaskInfo.progress)
}
.flatMapLatest { downloadTaskInfo in
return PHPhotoLibrary.savePhoto(downloadTaskInfo.buffer)
}
.observeOn(MainScheduler.instance)
.subscribe(
onError: { error in
photoItem.viewState = .NetworkFailed
},
onCompleted: {
photoItem.viewState = .Default
}
)
.addDisposableTo(disposeBag)
but the flatMapLatest doesn't do what I was expecting. I thought that flatMapLatest would allow me to grab the latest event and make another operation.
So, I decided to replace it with reduce to achieve what I had in mind but I think it's not the right operator because I don't want to join all the download progress in one variable. What I want is something where it's possible to wait for the download to complete and then get the latest to continue with other operations like saving the photo locally.
With concat I cannot receive the result from the first Observable.
I need something like
// 😅
.waitUntilDownloadFinishesAndContinueWith { downloadTaskInfo in
return PHPhotoLibrary.savePhoto(downloadTaskInfo.buffer)
}
Can someone explain me the right way to design this?
UPDATE
I decided to go with withLatestFrom but even so I'm having some problems. The downloadPhotoObservable is being disposed too soon.
let downloadPhotoObservable = photoController.downloadPhoto(photoItem.photo)
.doOnNext { downloadTaskInfo in
photoItem.viewState = .NetworkProgress(task: downloadTaskInfo.task, progress: downloadTaskInfo.progress)
}
Observable.just(photoItem)
.withLatestFrom(downloadPhotoObservable)
.map { downloadTaskInfo in
PHPhotoLibrary.savePhoto(downloadTaskInfo.buffer)
}
.observeOn(MainScheduler.instance)
.subscribe(
onError: { error in
photoItem.viewState = .NetworkFailed
},
onCompleted: {
photoItem.viewState = .Default
}
)
.addDisposableTo(disposeBag)
I'm doing something wrong for sure.
So, I found a way to achieve what I was trying to do. I decided to filter all the results and compare the final buffer length. The buffer is the next part for the photo persistence.
photoController.downloadPhoto(photoItem.photo)
.downloadProgress()
// Receive the download progress
.doOnNext { downloadTaskInfo in
photoItem.viewState = .NetworkProgress(task: downloadTaskInfo.task, progress: downloadTaskInfo.progress)
}
// Wait for the complete buffer
.filter { downloadTaskInfo in
downloadTaskInfo.contentLength == Int64(downloadTaskInfo.buffer.length)
}
// Save it locally
.flatMap { downloadTaskInfo in
PHPhotoLibrary.savePhoto(downloadTaskInfo.buffer)
}
.observeOn(MainScheduler.instance)
.subscribe(
onError: { error in
photoItem.viewState = .NetworkFailed
},
onCompleted: {
photoItem.viewState = .Default
}
)
.addDisposableTo(disposeBag)
BTW, I'm using a scan operator to recall the progress info. I created a shortcut with a custom operator called downloadProgress:
extension ObservableType where E == NetworkDataTaskInfo {
func downloadProgress() -> Observable<NetworkDownloadTaskInfo> {
let seed = NetworkDownloadTaskInfo(task: NopNetworkTask(), buffer: NSMutableData(), progress: 0, contentLength: 0)
return scan(seed, accumulator: { latestDownloadTaskInfo, currentDataTaskInfo in
var downloadedProgress: Float = 0
var contentLength: Int64 = 0
if let response = currentDataTaskInfo.response {
// Start
contentLength = response.expectedContentLength
}
else if let data = currentDataTaskInfo.data {
// Accumulate
contentLength = latestDownloadTaskInfo.contentLength
latestDownloadTaskInfo.buffer.appendData(data)
downloadedProgress = Float(latestDownloadTaskInfo.buffer.length) / Float(contentLength)
}
if contentLength <= 0 {
throw NSURLError.ZeroByteResource
}
// Accumulated info
return NetworkDownloadTaskInfo(
task: currentDataTaskInfo.task,
buffer: latestDownloadTaskInfo.buffer,
progress: downloadedProgress,
contentLength: contentLength
)
})
}
}