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

Add a .mainImmediate context #34

Closed
lilyball opened this issue Mar 7, 2019 · 2 comments
Closed

Add a .mainImmediate context #34

lilyball opened this issue Mar 7, 2019 · 2 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@lilyball
Copy link
Owner

lilyball commented Mar 7, 2019

Occasionally I'd like to do something like "execute on the main queue, but as .immediate if that would run on the main queue". This is fairly esoteric, but I want it for scenarios like

return makeSomePromise()
    .fork({ $0.then(on: .mainImmediate, { (value) in
        self.recordValue(value)
    })})

The rationale here is if my client then attaches an .immediate listener and that ends up running on the main thread, I need my own work to have been completed first. In the then case I can work around this by using .immediate and dispatching to the main queue myself if I'm not on the main thread, but that won't work for the map case.

I'm not sold on the .mainImmediate name.

@lilyball lilyball added the enhancement New feature or request label Mar 7, 2019
@lilyball
Copy link
Owner Author

lilyball commented May 4, 2019

I have another use-case: Fetching an image and assigning it to a button. The idea is to assign it immediately if the result is already available, otherwise to assign it on .main (the fact that it would run as .immediate if it asynchronously resolves on .main is unnecessary but harmless). Something like

NetworkImageRequester.promise(for: iconURL)
    .then(on: .mainImmediate, { [weak button] (image) in
        button?.setImage(image, for: .normal)
    })

In many cases, deferring this to .main would still run before the transaction closes, unless we're calling this from within a block dispatched to the main queue already. This edge case is why we'd want to be able to run it .immediate instead if the promise has already resolved.

@lilyball
Copy link
Owner Author

I think I'll do this as .nowOr(context) rather than tying it to main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant