0

I am trying to wait on information to load from my connection helper object, in my view where I am displaying it. I a not sure how to handle the call in my view.

It is built off this question: Waiting for two different REST/Network calls to complete

class ConnectionAPI
{
func getABStructWrapper() -> ABStructWrapper
{
    ConnectionHandler()
    return self.structWrapper
}

//let loader = from Oauth2/p2_OAuth2 pod to load JSON
//other general func and var’s 
func ConnectionHandler()
{

    let dg = DispatchGroup()

    switch self.RequestType
    {
    case aRequest:
        //if I make a single call here, works fine 
    case bRequest:
        //if I make a single call here, works fine 
    case abRequest:
        //when making requests for both type a and b, can’t get it to work
        dg.enter()

        loader.perform(request: self.userDataRequest)
        { response in
            do
            {
                let json = try response.responseJSON()
                self.structWrapper.aArray = self.loadAArray(dict: json)
            }
            catch let error {
                self.didCancelOrFail(error)
            }

           dg.leave()
        }

        dg.enter()

        loader.perform(request: self.userDataRequest)
        { response in
            do
            {
                let json = try response.responseJSON()
                self.structWrapper.bArray = self.loadBArray(dict: json)                }
            catch let error {
                self.didCancelOrFail(error)
            }

          dg.leave()
        }

       //This works! but want to do this from a view
       //commented out in xcode:
        dg.notify(queue: .main) 
        {

          for x in self.structWrapper.aArray                  
           {
             print(x.displayTime + ":" + x.units)
           }
           for y in self.structWrapper.bArray
           {
            print(y.displayTime + ":" + String(y.someNumber) 
           }
            print(“tasks complete”)
         }

    }

}
}

From my view:

Func getValues
{

        let dg = DispatchGroup()
        dg.enter()
        self.StructWrapper = ConnectionAPI.getABStructWrapper()
        dg.leave()
        dg.notify(queue: .main) 
       {
            //will print, but requests are not complete, arrays are empty, even though breakpoints hit in loading them.
          for x in self.structWrapper.aArray                  
           {
             print(x.displayTime + ":" + x.units)
           }
           for y in self.structWrapper.bArray
           {
            print(y.displayTime + ":" + String(y.someNumber) 
           }
            print(“tasks complete”)
        }
}
BriOnH
  • 945
  • 7
  • 18

1 Answers1

0

In you request class you need completion handler

class ConnectionAPI {

   static let shared = ConnectionAPI()

   func ConnectionHandler(completion:@escaping(_ finished:Bool)->void) {

      //
      //

     dg.notify(queue: .main) {

       completion(true)
     }

   }

}

Note you need to handle error cases and send

completion(false)

//

You don't need any DispatchGroup in the view as you already did them in the networking class

func getValues() {
     ConnectionAPI.shared.ConnectionHandler {  (finished) in

       if finished {
          print(ConnectionAPI.shared.aArray)
       }
     }
  }
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87