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

Can I call runNext() multiple times? #3

Closed
sferrini opened this issue Dec 3, 2016 · 4 comments
Closed

Can I call runNext() multiple times? #3

sferrini opened this issue Dec 3, 2016 · 4 comments

Comments

@sferrini
Copy link

sferrini commented Dec 3, 2016

I was wandering if the library was thought to being able to run the next block multiple times.
And if yes, when one should call the end() function (if the last block is called multiple times)?

@pascalbros
Copy link
Owner

Basically you can do what you want with ’runNext()’
My idea is to add a "counter" in additional data and check it, when it reaches a certain value, then call end() function.

@sferrini
Copy link
Author

sferrini commented Dec 3, 2016

The problem with this is that if I change (let's say) a boolean in the me.parameters the first iteration, the second one will override that boolean:

Me.start { (me) in
    MyAPI.posts { posts in
        for (index, post) in posts.enumerated() {
            if let post = post {
                me.parameters["post"] = post
                me.parameters["stop"] = index < (posts.count - 1) ? false : true
                me.runNext()
            }
        }
    }
}.next { (caller, me) in
    MyAPI.likes(post: caller.parameters["post"]) {
        if caller?.parameters["stop"] as! Bool == true {
            me.end()
        }
    }
}.run()

And caller?.parameters["stop"] will be always true cause the next block is called asynchronously and the for has already iterated again.

Did I get your advice in a wrong way? Thanks

@pascalbros
Copy link
Owner

What if:

next { (caller, me) in
    let shouldStop = caller!.parameters["stop"]

    MyAPI.likes(post: caller.parameters["post"]) {
        if shouldStop as! Bool == true {
            me.end()
        }
    }
}.run()

@sferrini
Copy link
Author

sferrini commented Dec 3, 2016

Yeah that should work, thanks! 👍

@sferrini sferrini closed this as completed Dec 3, 2016
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