-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
DispatchQueues should be generalized #878
Comments
I'm cool, and agree it would be useful, but there are a few conditions:
We could put this in PMK 7 though. Although I'd still want |
Great! I'll see what I can do. |
We have a test breakage in CI for Linux, any ideas? https://travis-ci.org/mxcl/PromiseKit/builds/492338166 |
Looking at it now... |
In the latest Linux library, DispatchQueue.main is evidently no longer a static object. You get a new instance each time, or perhaps it's a value type now. Odd, because you can still access the same queue-specific data through any of the instances... The library code doesn't depend on this, just the testing code. PR incoming. |
Merged into v7. |
I've been converting a project from RxSwift to PromiseKit, and it's going really well. I know these aren't regarded as competitive substitutes, but it has been a bit surprising to see how much cleaner some of the code gets.
The one thing that I'm feeling some regret about letting go of is RxSwift's
Scheduler
abstraction. Not because it's the world's most elegant API (though it's fine), but because it introduces a layer of abstraction between the Foundation-level facilities and the kit-level facilities. This separation is important because you can't actually subclassDispatchQueue
: it's a C language system that's intimately intertwined with the compiler and runtime. If you want different dispatch behavior in PromiseKit, you're pretty much stuck, as far as I can tell. (Good thing it's easy to intermix manual GCD blocks with PromiseKit-managed blocks!)I'll cite some specific use cases below, but just to get to the point: it would be nice if PromiseKit's
on
arguments accepted something like aDispatchProtocol
object instead of an object-type-specificDispatchQueue
. It's fine if the protocol mirrors the existingDispatchQueue
API - I just want to be able to roll my own.It looks like it would be a pretty straightforward change. I'd send it in as PR, except that I can imagine some strong potential objections to the basic idea. So I thought it would be better to get a temperature reading first. The main drawback is that
DispatchProtocol
becomes visible in the public API. I imagine most people's initial reaction would be something like "WTF is aDispatchProtocol
?" Calling it something likeDispatchQueueProtocol
would perhaps clarify, but that's an awfully long name. Something likeScheduler
is clear and succinct, but it obscures the fact that these objects really are justDispatchQueue
s in 99% of cases.I have three use cases in the current project. One is a concurrency-limited queue. You can submit as many blocks as you like, but only N will be allowed to execute concurrently. The rest have to wait until a slot opens up. This is great for cases where you need to, e.g., read 100 XML feeds, but you know that
URLSession
will start acting up if you just fire them all off at once. At the same time, it's perfectly reasonable to queue up 100 blocks in aDispatchQueue
, so you can get away with no queueing code at all outside of a generic concurrency-limited queue implementation.The second case is rate-limited scheduling, where one of the external APIs limits the number of network requests that can be made in a given period of time. If you go over the limit, you get a significant punishment such as a 5-minute timeout, so it's worthwhile to obey the limit stringently. It's really easy to implement this in terms of
DispatchQueue
s, but again, you need some way to interpose your own layer.The last case is plain old Core Data. If I understand the architecture correctly,
perform
andperformAndWait
are just thin wrappers around dispatch queues. Nevertheless, they are the designated interface and you are supposed to use them instead of trying to get your hands on the underlyingDispatchQueue
. It would be really nice to be able to write, e.g.,done(on: myCoreDataMOC) {...}
and just have it useperform*
behind the scenes.Thoughts?
The text was updated successfully, but these errors were encountered: