Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chaining does not seem to be working #120

Closed
martheli opened this issue Jul 16, 2017 · 3 comments
Closed

Chaining does not seem to be working #120

martheli opened this issue Jul 16, 2017 · 3 comments

Comments

@martheli
Copy link

martheli commented Jul 16, 2017

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
Copy link
Owner

duemunk commented Jul 16, 2017 via email

@martheli
Copy link
Author

martheli commented Jul 16, 2017

@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
Copy link
Owner

duemunk commented Jul 17, 2017

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

@duemunk duemunk closed this as completed Jul 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants