Dipen Chudasama
Dipen Chudasama

Reputation: 3093

How to call recursive function with Promise Kit?

I am stuck with somewhere to call same function again in promise and because of calling multiple time it's deallocate promise. Actually in my case I have API with multiple page request and I want to call it with promise. I was implemented it as below.

func fetchContacts() -> Promise<FPGetContactResponse?> {
            
        return Promise { seal in

            let contactrequest = FPGetContactRequest()
            contactrequest.pageNo = getAPICurrentPageNo(Api.API_CONTACTS) + 1
            contactrequest.pageSize = SMALL_PAGE_SIZE
            
               contactrequest.doGetContacts(parameter: [:], response: { (response) in
                print("Contacts Count : \(response.Contacts?.count ?? 0)")

                if(response.Contacts?.count ?? 0 != 0){
                    _ = self.fetchContacts()

                }else{
                    seal.fulfill(response)
                }
               })
               { (error) in
                   print(error.localizedDescription)
                seal.reject(error)

            }
        }
    }

In above function I check for contact count != 0 then I need to call same function again. But unfortunately it's deallocate promise.

I call promise sequence like below.

func startSyncData(handler:@escaping SyncAPIHandler){
        firstly {
            self.fetchContacts().ensure {
                handler(false,0.5,nil)
            }
        }.then { data in
            self.fetchInteractions().ensure {
                handler(false,0.7,nil)
            }
        }.then { data in
            self.fetchAddresses().ensure {
                handler(false,0.8,nil)
            }
        }.then { data in
            self.fetchLookupQuery().ensure {
            }

        }
        .done { contacts -> Void in
            //Do something with the JSON info
            print("Contacts Done")
            handler(true,0.8,nil)
        }
        .catch(policy: .allErrors) { error in
            print(error.localizedDescription)

        }

    }

Please provide me the right way to call same function again in promise.

Upvotes: 0

Views: 741

Answers (2)

Dipen Chudasama
Dipen Chudasama

Reputation: 3093

I implemented things with following solution.

func syncContacts() -> Promise<FPGetContactResponse?> {
      return fetchContacts().then{ seal -> Promise<FPGetContactResponse?> in
        if(seal?.Contacts?.count ?? 0 != 0){
            return self.syncContacts()
        }else{
            return Promise.value(seal)
        }
      }
    }

Now just call syncContacts() method in promise sequence, like below.

 func startSyncData(handler:@escaping SyncAPIHandler){
        firstly {
            self.syncContacts().ensure {
                handler(false,0.5,nil)
            }
        }.then { data in
            self.syncInterections().ensure {
                handler(false,0.7,nil)
            }
        }.then { data in
            self.syncAddresses().ensure {
                handler(false,0.8,nil)
            }
        }.then { data in
            self.syncLookupQuery().ensure {
            }

        }
        .done { contacts -> Void in
            //Do something with the JSON info
            print("Contacts Done")
            handler(true,0.8,nil)
        }
        .catch(policy: .allErrors) { error in
            print(error.localizedDescription)

        }

    }

Upvotes: 1

iUrii
iUrii

Reputation: 13818

Instead of using recursion you should return a response within your promise and check it inside next .then and call fetchContacts again if it's needed:

fetchContacts()
.then { response -> Promise<FPGetContactResponse> in
    if (response.Contacts?.count ?? 0 != 0) {
        return fetchContacts() // Make the second call
    }
    return .value(response) // Return fullfilled promise
}
.then {
    ...
}

Also you can make a special wrapper for your case using the next approach - https://github.com/mxcl/PromiseKit/blob/master/Documentation/CommonPatterns.md#retry--polling

Upvotes: 2

Related Questions