RxSwift combine observable with conditional - conditional-statements

I'm trying to combine observables and I want them to run in sequence (e.g., perform step 1, if some condition is met then perform step 2, if some condition is met then perform step 3). The only way I've found to do this is to add the conditions to each step, which I'm not a fan of: Here's a sample of my current solution:
enum Status {
case unknown, exists, missing
}
func refresh() -> Observable<Status> {
return checkLocalStatus()
.flatMapLatest { $0 == .exists ? Observable.just($0) : self.attemptRemoteStatusOverride() }
.flatMapLatest { $0 == .exists ? Observable.just($0) : self.attemptRemoteStatusUpdate() }
}
private func checkLocalStatus() -> Observable<Status> {
return Observable.create { observer in
// Regarding Maxim Volgin's comment, here I'm converting a closure to an
// observable... why not use Observable.create?
self.cache.status { (status) in
guard status != .exists else {
observer.onNext(status) // .exists
observer.onCompleted()
}
/* I don't want this condition to be here */
if ignoreRemote {
// status is !exists and we should ignore remote, throw error
observer.onError(Errors.remoteDisabled)
}
observer.onNext(.missing)
observer.onCompleted()
}
}
}
private func attemptRemoteStatusOverride() -> Observable<Status> {
return remote.statusOverride()
}
private func attemptRemoteStatusUpdate() -> Observable<Status> {
return Observable.create { observer in
// Regarding Maxim Volgin's comment, here I'm converting a closure to an
// observable... why not use Observable.create?
self.remote.updateStatus { (status, error) in
guard error == nil else {
observer.onError(error!)
}
observer.onNext(status)
observer.onCompleted()
}
}
}
I'd like to do something like:
func refresh() -> Observable<Status> {
return checkLocalStatus()
.if({ $0 != .exists && !ignoreRemote },
then: { self.attemptRemoteStatusOverride() },
else: { return $0 })
.if({ $0 != .exists },
then: { self.attemptRemoteStatusUpdate() },
else: { return $0 })
}
or
func refresh() -> Observable<Status> {
return checkLocalStatus()
.flatMapLatest(if: { $0 != .exists && !ignoreRemote }) { self.attemptRemoteStatusOverride() }
.flatMapLatest(if: { $0 != .exists }) { self.attemptRemoteStatusUpdate() }
}
I haven't been able to find anything like what I'm attempting, so I assume I'm going about this wrong. Does anyone have suggestions or alternatives on how to go about this route of combining observables? I've seen examples using combineLatest and returning some results based on the result of something else, but I want to perform each step only if a condition is met. combineLatest would perform each step (every time) and then I would return the result(s) of some steps based on the output of other steps. I also started looking into writing a custom operator, but can't figure a way to do it.
Update: I've changed to the following and plan to write a method to remove duplication:
func refresh() -> Observable<Status> {
return checkLocalStatus()
.flatMapLatest { status -> Observable<Status>
guard status != .exists && !ignoreRemote else {
return Observable.just(status)
}
return self.attemptRemoteStatusOverride()
}
.flatMapLatest { status -> Observable<Status>
guard status != .exists && !ignoreRemote else {
return Observable.just(status)
}
return self.attemptRemoteStatusUpdate()
}
}

Maybe you need some version of flatMapLatest function with conditions? You can make some function that does what you want with the syntax you want:
extension Observable {
func flatMapLatest(condition: #escaping (E) -> Bool, then: #escaping (E) -> Observable, otherwise: #escaping () -> Observable) -> Observable {
let observable = self.shareReplayLatestWhileConnected()
let observableCondition = observable.map({ condition($0) }).shareReplayLatestWhileConnected()
let observableThen: Observable<E> = observableCondition
.filter({ $0 })
.withLatestFrom(observable)
.flatMapLatest({ then($0) })
.shareReplayLatestWhileConnected()
let observableOtherwise: Observable<E> = observableCondition
.filter({ !$0 })
.withLatestFrom(observable)
.flatMapLatest({ _ in otherwise() })
.shareReplayLatestWhileConnected()
return Observable<Observable<E>>
.from([observableThen, observableOtherwise])
.merge()
}
}
and use it
func refresh() -> Observable<Status> {
let condition = { (status: Status) -> Bool in
return status == .exists
}
let then = { (status: Status) -> Observable<Status> in
return Observable.just(status)
}
return checkLocalStatus()
.flatMapLatest(condition: condition, then: then, otherwise: self.attemptRemoteStatusOverride)
.flatMapLatest(condition: condition, then: then, otherwise: self.attemptRemoteStatusUpdate)
}

Related

im having difficulty getting downloaded json response into my data model and accessing it in code

i have the following code in which i'm trying to download exchange rates into my app to use in currency conversion.
The data fetch seems to work ok, as does the json decoding model, but i'm unable to get the data through the Rates variable
import SwiftUI
struct ExchangeRates: Codable {
var conversionRates: [String: Double]?
init(conversionRates:[String:Double]) {
self.conversionRates = conversionRates
}
enum CodingKeys: String, CodingKey {
case conversionRates = "conversion_rates"
}
}
class DownloadingData:ObservableObject{
#Published var Rates:ExchangeRates = ExchangeRates.init(conversionRates: ["test" : 0])
init() {
datatask()
}
func datatask() {
guard let url = URL(string: "https://v6.exchangerate-api.com/v6/********************/latest/GBP") else {return}
URLSession.shared.dataTask(with: url){(data,response,error) in
guard let data = data else {
print("no data")
return
}
guard error == nil else {
print("error :\(String(describing: error))")
return
}
guard let response = response as? HTTPURLResponse else {
print("invalid response")
return
}
guard response.statusCode >= 200 && response.statusCode < 300 else {
print("status code should be 2xx, but is \(response.statusCode)")
return
}
guard let rates = try? JSONDecoder().decode(ExchangeRates.self, from: data) else {return}
print(rates) **// this works and prints out data**
DispatchQueue.main.async {
[weak self] in
self?.Rates = rates
print(self?.Rates) **// this works and prints out the data**
}
print(self.Rates) **// this doesnt print out anything**
}.resume()
print(Rates) **// this doesnt print out anything**
}
}
i can't seem to get the data into the Rates Variable
any guidance please
thanks
here is a sample of the console output :
ExchangeRates(conversionRates: Optional(["test": 0.0]))
Optional(test3.ExchangeRates(conversionRates: Optional(["BZD": 2.7002, "PHP": 68.7948, "PGK": 4.725, "BND": 1.8176, "HNL": 32.8885, "TND": 3.7553, "BDT": 115.2218, "SBD": 10.6866, "NIO": 47.4824, "XDR": 0.963, "IDR": 19213.9064, "XCD": 3.6453, "CAD": 1.7152, "UGX": 4778.6135,])
you could try this, using your ExchangeRates struct:
class DownloadingData: ObservableObject{
#Published var rates = ExchangeRates(conversionRates: ["test" : 0])
init() {
datatask()
}
func datatask() {
guard let url = URL(string: "https://v6.exchangerate-api.com/v6/********************/latest/GBP") else {return}
URLSession.shared.dataTask(with: url){(data,response,error) in
guard let data = data else {
print("no data")
return
}
guard error == nil else {
print("error :\(error)")
return
}
guard let response = response as? HTTPURLResponse else {
print("invalid response")
return
}
guard response.statusCode >= 200 && response.statusCode < 300 else {
print("status code should be 2xx, but is \(response.statusCode)")
return
}
guard let exRates = try? JSONDecoder().decode(ExchangeRates.self, from: data) else {return}
print(exRates)
DispatchQueue.main.async {
self.rates = exRates // <--- here
print(self.rates) // <--- here
}
// print(self.rates) // <--- NEVER here
}.resume()
}
}
EDIT-1: with completion closure:
class DownloadingData: ObservableObject{
#Published var rates = ExchangeRates(conversionRates: ["test" : 0])
init() {
datatask() { isDone in
print(self.rates) // <--- here OK
}
}
func datatask(completion: #escaping(Bool) -> ()) { // <--- here
guard let url = URL(string: "https://v6.exchangerate-api.com/v6/********************/latest/GBP") else {return}
URLSession.shared.dataTask(with: url){(data,response,error) in
guard let data = data else {
print("no data")
return
}
guard error == nil else {
print("error :\(error)")
return
}
guard let response = response as? HTTPURLResponse else {
print("invalid response")
return
}
guard response.statusCode >= 200 && response.statusCode < 300 else {
print("status code should be 2xx, but is \(response.statusCode)")
return
}
guard let exRates = try? JSONDecoder().decode(ExchangeRates.self, from: data) else {return}
print(exRates) // <--- here OK
DispatchQueue.main.async {
self.rates = exRates
print(self.rates) // <--- here OK
completion(true) // <--- here return completion
}
}.resume()
}
}
Declare rates – please with starting lowercase letter –  as empty dictionary
#Published var rates = [String:Double]()
In the struct delete the init method and declare the dictionary also non-optional
struct ExchangeRates: Decodable {
let conversionRates: [String: Double]
enum CodingKeys: String, CodingKey {
case conversionRates = "conversion_rates"
}
}
in the DispatchQueue closure assign the value of conversionRates to rates
DispatchQueue.main.async { // no weak self needed
self.rates = rates.conversionRates
}
In the view enumerate the dictionary rates

Change color With Vuejs

I have this object taken from an API, i want to change the color when status change i tried to do this :
<b-badge :variant="variant">{{ $t(contract.status) }}</b-badge>
script:
computed: {
...mapGetters(["getTeammates", "isCompleted"]),
variant () {
if (status == "pending") {
return "warning";
} else if (status == "confirmed") {
return "success";
} else if (status == "waiting_for_approval"){
return "danger";
} else {
return "dark";
}
},
},
I don't know why it doesn't work,
the color is always dark.
status is not defined in the computed method variant.
Based on your code I guess it should be this.contract.status
variant () {
if (this.contract.status == "pending") {
return "warning";
} else if (this.contract.status == "confirmed") {
return "success";
} else if (this.contract.status == "waiting_for_approval"){
return "danger";
} else {
return "dark";
}
},
Bonus: If you want an advice, I would suggest to replace all those if with a switch statement.

Kotlin Stream usage

I have a code like below
items.forEach { item ->
request += getDetails(item.propertyId, item.destinationIds)
count++
if( count == bulkSize) {
save(request)
request = ""
count = 0
}
}
if(!request.isEmpty()) {
save(request)
}
How can I use streaming api to make the code less verbose ?
You can do it like this:
items.chunked(bulkSize) { chunk ->
save(chunk.joinToString(separator = "") { item ->
getDetails(item.propertyId, item.destinationIds)
})
}

RxSwift & alamofire, How to send http request after previous request arrived?

I need to send http request in RxSwift & Alamofire circumstance and send it synchronously which means ...
send(1)...response(1)
-------------------- send(2)...response(2)
-----------------------------------------send(3)...response (3)
and This is my code
Observable.from(devicesArray)
.concatMap { (device) in
return HTTPRequest.deleteDevice(withDevice: device)
}.subscribe({ (event) in
log.debug("Remove device successfully")
}).disposed(by: self.disposeBag)
and deleteDevice is
func deleteDevice(withDevice device:Device) -> Single<String> {
return Alamofire.request("http://example.com/\(device.deviceId)", method: .delete, parameters: nil, headers: self.headers()).rx
.responseJSON()
.retry(self.retryMaxAttempCount)
.asSingle()
.observeOn(SerialDispatchQueueScheduler(qos: .default))
.map({ (response) in
guard let json = response.value as? [String: Any] else {
throw HTTPManagerError.invalidResponse
}
guard let resultCode = json["resultCode"] as? String else {
throw HTTPManagerError.invalidResponse
}
if resultCode == "0000" || resultCode == "0101" {
return resultCode
} else {
throw HTTPManagerError.invalidResponse
}
})
.observeOn(MainScheduler.instance)
}
Now every single HTTPRequest.deleteDevice function requested in parallel.
So How could I make this request wait until a previous request get finished?
Thanks.
The key here is to make an array of observables and concat them:
// first we create a deleteDevice observable for each item.
// Remember, they don't actually make a network call until subscribed to and concat only subscribes to each once the previous one is done.
Observable.concat(
devicesArray.map { deleteDevice(withDevice: $0) }
.map { $0.asObservable() } // because concat doesn't exist for Singles.
.map { $0.catchErrorJustReturn("\($0) not deleted.") } // make sure that all deletes will be attempted even if one errors out.
)
// the above will emit a string every time a delete finishes.
// `.toArray()` will gather them together into an array.
.toArray()
.subscribe(onNext: { results in
})
.disposed(by: disposeBag)

check the status of the app moving or stationary

i want track my app status, i.e app is moving or stationary, I am using this module for location
https://github.com/mauron85/react-native-background-geolocation
for check the status of location i used this function
BackgroundGeolocation.on('stationary', (stationaryLocation) => {
console.log("stationaryLocation:"+JSON.stringify(stationaryLocation))
});
but it is not showing any response when i am stationary, can any one give me suggestion that how to resolve this.
Any help much appreciated.
Instead of using GeoLocation, you can use CMMotionActivityManager to track this.
public func getCurrentActivity(onRun:#escaping (_ activity: String?) -> Void) {
if(CMMotionActivityManager.isActivityAvailable()){
let mainQ = OperationQueue.main
self.activityManager?.startActivityUpdates(to: mainQ, withHandler: { (data: CMMotionActivity!) -> Void in
DispatchQueue.main.async(execute: {
var action = ""
if(data.stationary == true){
action = "Stationary"
} else if (data.walking == true){
action = "Walking"
} else if (data.running == true){
action = "Running"
} else if (data.automotive == true){
action = "Automotive"
} else if (data.cycling == true){
action = "cycling"
} else {
action = "Stationary"
}
onRun(action)
})
})
}
}