0

I have User class which has a ID property in it. I do a Alamofire request which fulfills this class (ID value). When a button tapped, I do another Alamofire request which sends users id. I have get 7 user in first request so second request done 7th times. When button tapped, I want that process continuously so that when I tapped button It should do first request -> second request (7 times) -> first req -> second req (7 times)..... Problem is that It needs to wait for first request to be completed and also all second requests. What will be the best approach while doing this? I'm thinking of creating group but I'm not sure.

   func cont_like(){
    for var i in 0..<3{
        let group = DispatchGroup()
        let group2 = DispatchGroup()
        group2.enter()
        group.enter()
        Network.sharedInstance.like_all(completion: {
            group.leave()
        })
        group.enter()
        Network.sharedInstance.get_rec(completion: {response in
            print(response)
            group.leave()
        })

        group.notify(queue: .main){
            print("Both Req Done")
            group2.leave()
        }
    }}

Second Request:

    func like(sender_id: String, completion: @escaping () -> ()){
    if let key = api_key{
        let headers: HTTPHeaders = [
            "X-Auth-Token": key,
            "Accept": "application/json"
        ]
        Alamofire.request(baseUrl + likeURL + sender_id, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers).responseData(completionHandler: { (response) in
            let jsonData = JSON(response.data!)

            guard response.result.isSuccess else {
                print("Error while fetching tags: \(response.result.error!)")
                return
            }
            if let error = response.error{
                print("Error Occured with Request: \(error)")
            }
            if let matched = jsonData["match"].bool{
                print("Liked!: \(matched)")
                if(matched){

                }
            }else{
                print("There is an error with liking :(")
            }

        })
    }
}

First Request:

    func get_rec(completion: @escaping (String?) -> ()){
    if let key = api_key{
        let headers: HTTPHeaders = [
            "X-Auth-Token": key,
            "Accept": "application/json"
        ]
        Alamofire.request(baseUrl + recom, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers).responseData(completionHandler: { (response) in
            if response.result.isFailure{
                completion(nil)
                self.delegate?.metaError()
                return
            }else{
                do {
                    let decoder = JSONDecoder()
                    let root = try decoder.decode(Root.self, from: response.data!)
                    SharedUsers.sharedUser.populateData(sender: root.data)
                    print(SharedUsers.sharedUser.users[0].id)
                    self.delegate?.updated()
                    completion("Fetched")
                    self.delegate?.updated()

                } catch {
                    print("error: ", error)
                    completion(nil)
                    self.delegate?.metaError()
                }
            }

        })
    }
    else{
    print("Error")
    }
}

Like All Functon:

func like_all(completion: @escaping() -> ()){
        let group2 = DispatchGroup()
        group2.enter()
        for user in SharedUsers.sharedUser.users{
            if let id = user.id{
                let group = DispatchGroup()
                group.enter()
                let when = DispatchTime.now() + 0.5 // change 2 to desired number of seconds
                DispatchQueue.main.asyncAfter(deadline: when) {
                    Network.sharedInstance.like(sender_id: id) { result in
                        group.leave()
                        print(result)
                    }

                }
            }
            else{
                continue
            }
        }
        group2.leave()
        completion()
    }

EDIT: I edited the functions but still First Request are called without waiting all like requests.

Mr Some Dev.
  • 315
  • 4
  • 19
  • https://stackoverflow.com/questions/11909629/waiting-until-two-async-blocks-are-executed-before-starting-another-block – Joyal Clifford Dec 08 '17 at 10:45
  • What happens if you send all first requests asyncly and then after each first req finishes you call it's second req? Will this approach work for you? – Inder Kumar Rathore Dec 08 '17 at 10:46
  • You can achieve this using GCD using DispatchGroup in swift 3. You can get notified when all tasks are finished. – Joyal Clifford Dec 08 '17 at 10:50
  • @InderKumarRathore I though that also but think that in 3th, 4th period. liked call can be called before new class assigned so one way or another. It should wait. – Mr Some Dev. Dec 08 '17 at 11:12

1 Answers1

0

There are different approaches one is DispatchGroup

  let group = DispatchGroup()

    group.enter()
    func like(sender_id: String, completion: @escaping () -> ()){
        group.leave()
    }

    group.enter()
    func get_rec(completion: @escaping (String?) -> ()){
        group.leave()
    }

    group.notify(queue: .main) {
        print("both requests done")
    }

Another one is NSOperationQueue by addDependency .

Joyal Clifford
  • 1,212
  • 11
  • 20