swiftrecursionalamofireswift5promisekit

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.


Solution

  • 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)
    
            }
    
        }