If statement Swift - swift

I have a function with "on success" and "on failure" cases.
func loadBanners(nameId: String, onSuccess: #escaping (_ objectArray: [IGCBanners]) -> (), onFailure: #escaping (NSError) -> ()) {
IGCTransactionManager.sharedInstance.getBanners(
nameId: nameId,
onSuccess: { json in
if let json = json {
let res: IGCBannersRootClass = IGCBannersRootClass(fromJson: json)
self.populate(res)
if nameId == "home" {
self.populateSlideShow(res)
}
onSuccess(TransactionSession.shared.Array)
} else {
onFailure(IGCTransactionManager.sharedInstance.getErrorForNil())
}
},
When it goes on failure, I would like to check if it's the first time.
So If its first, I need to reload the success case and from second time (else) to shows invalid-failure case.
onFailure: { error in
if error == true {
self.loadBanners(nameId: "home", onSuccess: onSuccess, onFailure: onFailure)
} else {
if !self.showInvalidResponse(onSuccess: onSuccess, onFailure: onFailure) {
onFailure(error)
}
}
}
My condition error == true ,Its not the right one for sure and I think doesn't explain that this is happened for first time. Any advice or recommend ?

Thanks to HangarRash comment I solved with a counter
var failurecounter = 0
this outside the function and inside the onfailure case
self.failureCounter += 1
if self.failureCounter == 1 { ... }

Related

RxSwift+Alamofire custom mapper error handling

RxSwift one more question about error handling:
I'm using Alamofire+RxAlamofire this way:
SessionManager.default.rx.responseJSON(.post, url, parameters:params)
example:
func login() -> Observable<Int> {
let urlString = ...
let params = ...
return SessionManager.default.rx.responseJSON(.post, url, parameters:params)
.rxJsonDefaultResponse()
.map({ (data) in
data["clientId"] as! Int
})
}
....
extension ObservableType where Element == (HTTPURLResponse, Any) {
func rxJsonDefaultResponse() -> Observable<Dictionary<String, Any>> {
return self.asObservable().map { data -> Dictionary<String, Any> in
if... //error chechings
throw NSError(domain: ..,
code: ...,
userInfo: ...)
}
...
return json
}
}
}
using:
loginBtn.rx.tap
.flatMap{ _ in
provider.login()
}.subscribe(onNext: { id in
...
}, onError: { (er) in
ErrorPresentationHelper.showErrorAlert(for: er)
})
.disposed(by: bag)
So if error occurred everything works as intended: error alert shows and 'loginBtn.rx.tap' disposed, but I need it to be still alive, what's my strategy here if I want to use onError block?
You can use materialize function in rxSwift. It will convert any Observable into an Observable of its events. So that you will be listening to Observable<Event<Int>> than Observable<Int>. Any error thrown from the flatmap would be captured as error event in your subscription block's onNext and can be handled there. And your subscription would still be alive. Sample code would be as follows.
button.rx.tap.flatMap { _ in
return Observable.just(0)
.flatMap { _ -> Observable<Int> in
provider.login()
}.materialize()
}.subscribe(onNext: { event in
switch event {
case .next:
if let value = event.element {
print(value) //You will be getting your value here
}
case .error:
if let error = event.error {
print(error.localizedDescription) //You will be getting your captured error here
}
case .completed:
print("Subscription completed")
}
}) {
print("Subscription disposed")
}.disposed(by: disposeBag)
Hope it helps. You can checkout the materialize extension here.

Call own function with a completion handler

I have a method to create an account:
func createAccount (completion: #escaping (_ succes: Bool, _ message : String)->()) {
Auth.auth().createUser(withEmail: createMail(), password: createPassword()) { (result, error) in
if let _eror = error {
//something bad happning
print(_eror.localizedDescription )
if let errorCode = AuthErrorCode(rawValue: _eror._code) {
if(errorCode.rawValue == 17007) {
print("acount exist")
createAccount(completion: (Bool, String) -> ()
} else {
//call itself and try it again
}
}
} else {
//user registered successfully
print("user registered")
return completion(true, "");
}
}
}
I get an error when the software creates an account with an email that already exists, which is good (see the else statement - //call itself and try it again).
What needs to happen is that the function needs to call itself again to try it with a different email.
I tried to put createAccount(completion: (Bool, String) -> () in the else case, but that didn't work.
How can I call the createAccount() function again in the else case?
You need to pass the same paramter again
createAccount(completion:completion)

PromiseKit firstly around code, not function call

I don't want to write a separate function to return a Promise in my firstly call. I just want to write this:
firstly
{
return Promise<Bool>
{ inSeal in
var isOrderHistory = false
let importTester = CSVImporter<String>(url: url)
importTester?.startImportingRecords(structure:
{ (inFieldNames) in
if inFieldNames[2] == "Payment Instrument Type"
{
isOrderHistory = true
}
}, recordMapper: { (inRecords) -> String in
return "" // Don't care
}).onFinish
{ (inItems) in
inSeal.resolve(isOrderHistory)
}
}
}
.then
{ inIsOrderHistory in
if inIsOrderHistory -> Void
{
}
else
{
...
But I'm getting something wrong. ImportMainWindowController.swift:51:5: Ambiguous reference to member 'firstly(execute:)'
None of the example code or docs seems to cover this (what I thought was a) basic use case. In the code above, the CSVImporter operates on a background queue and calls the methods asynchronously (although in order).
I can't figure out what the full type specification should be for Promise or firstly, or what.
According to my understanding, since you are using then in the promise chain, it is also meant to return a promise and hence you are getting this error. If you intend not to return promise from your next step, you can directly use done after firstly.
Use below chain if you want to return Promise from then
firstly {
Promise<Bool> { seal in
print("hello")
seal.fulfill(true)
}
}.then { (response) in
Promise<Bool> { seal in
print(response)
seal.fulfill(true)
}
}.done { _ in
print("done")
}.catch { (error) in
print(error)
}
If you do not want to return Promise from then, you can use chain like below.
firstly {
Promise<Bool> { seal in
print("hello")
seal.fulfill(true)
}
}.done { _ in
print("done")
}.catch { (error) in
print(error)
}
I hope it helped.
Updated:
In case you do not want to return anything and then mandates to return a Promise, you can return Promise<Void> like below.
firstly {
Promise<Bool> { seal in
print("hello")
seal.fulfill(true)
}
}.then { (response) -> Promise<Void> in
print(response)
return Promise()
}.done { _ in
print("done")
}.catch { (error) in
print(error)
}

RxSwift Chaining two signals in right order

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

Swift: Async method into while loop

I want to use a async function inside a while loop but the function don't get enough time to finish and the while loop starts again and never ends.
I should implement this problem with increment variable , but what is the solution? thanks a lot.
output loops between "Into repeat" - "Into function"
var condition = true
var userId = Int.random(1...1000)
repeat {
print("Into repeat")
checkId(userId, completionHandler: { (success:Bool) -> () in
if success {
condition = false
} else {
userId = Int.random(1...1000)
}
}) } while condition
func checkId(userId:Int,completionHandler: (success:Bool) -> ()) -> () {
print("Into function")
let query = PFUser.query()
query!.whereKey("userId", equalTo: userId)
query!.findObjectsInBackgroundWithBlock({ (object:[PFObject]?, error:NSError?) -> Void in
if object!.isEmpty {
completionHandler(success:false)
} else {
completionHandler(success:true)
}
})
}
You can do this with a recursive function. I haven't tested this code but I think it could look a bit like this
func asyncRepeater(userId:Int, foundIdCompletion: (userId:Int)->()){
checkId(userId, completionHandler: { (success:Bool) -> () in
if success {
foundIdCompletion(userId:userId)
} else {
asyncRepeater(userId:Int.random(1...1000), completionHandler: completionHandler)
}
})
}
You should use dispatch_group
repeat {
// define a dispatch_group
let dispatchGroup = dispatch_group_create()
dispatch_group_enter(dispatchGroup) // enter group
print("Into repeat")
checkId(userId, completionHandler: { (success:Bool) -> () in
if success {
condition = false
} else {
userId = Int.random(1...1000)
}
// leave group
dispatch_group_leave(dispatchGroup)
})
// this line block while loop until the async task above completed
dispatch_group_wait(dispatchGroup, DISPATCH_TIME_FOREVER)
} while condition
See more at Apple document