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.