duemunk / Async

Syntactic sugar in Swift for asynchronous dispatches in Grand Central Dispatch
MIT License
4.59k stars 316 forks source link

Chaining does not seem to be working #120

Closed martheli closed 7 years ago

martheli commented 7 years ago

My code is as follows:

  let backgroundBlock = Async.utility {

                    for serial in serialarray

                    {

                   self.getVersion(serial: serial)

                    }

                }

                backgroundBlock.main
                    {
                     print (serialarray)
                     print (versionarray)

                SwiftSpinner.hide()
                }

If I understand this right, getVersion should run in the background until it completes fully and then the second block should run and print out both of my arrays. Instead the first array gets printed but the second array comes up blank. I added a print inside of my getVersion function to see if it was working and I can see all the values being populated so it looks like the block is not waiting to complete before it prints the array out. Am I doing something wrong?

The getVersion function is as follows:

Alamofire.request("url=\(serial)", headers: httpheader).validate().responseData { response in

        switch response.result {

        case .success:

            if let data = response.data {

                let xml = SWXMLHash.parse(data)

                var count: Int = 0

                var stop = false

                for elem in xml["deviceSoftwares"]["deviceSw"].all

                {

                     for elem2 in elem["swAttrs"].all

                     {

                        if stop == false

                        {
                             count += 1

                        if (elem2["value"].element!.text) == "com.fiberlink.maas360.fpl"
                        {
                           stop = true
                        }

                        }

                    }
                }

                let position = (count/5)

                if stop == true

                {
                let version = xml["deviceSoftwares"]["deviceSw"][position]["swAttrs"][2]["value"].element!.text

                    versionarray.append(version)
                }

                else

                {
                    versionarray.append("N/A")

                }

            }

        case .failure(_):

            let errorCode = response.response?.statusCode
            let errorString = String(describing: errorCode!)
            print("Error " + errorString)

        }

        }
duemunk commented 7 years ago

You don’t store the result if getVersion to versionarray.

On 16 Jul 2017, at 09.08, martheli notifications@github.com wrote:

My code is as follows:

let backgroundBlock = Async.utility {

                for serial in serialarray

                {

               self.getVersion(serial: serial)

                }

            }

            backgroundBlock.main
                {
                 print (serialarray)
                 print (versionarray)

            SwiftSpinner.hide()
            }

If I understand this right, getVersion should run in the background until it completes fully and then the second block should run and print out both of my arrays. Instead the first array gets printed but the second array comes up blank. I added a print inside of my getVersion function to see if it was working and I can see all the values being populated so it looks like the block is not waiting to complete before it prints the array out. Am I doing something wrong?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

martheli commented 7 years ago

@duemunk I added the getVersion function to my original post. Versionarray gets populated in the getVersion function.

I also tried the following:

 let group = AsyncGroup()

                group.utility {

                    for serial in serialarray

                    {

                   self.getVersion(serial: serial)

                    }

                }

                group.wait()

                     print (serialarray)
                     print (versionarray)

                   SwiftSpinner.hide()

Both prints are executing before getVersion finishes its loop to populate the array.

duemunk commented 7 years ago

This is not an issue for Async. Please refer to GCD documentation and/or StackOverflow.