Combine Future vs NIO EventLoopFuture?

I need to make some asynchronous network calls to a server from an app. The simplest thing would be to pass an callback;

func getStuff(complete: @escaping (Result<Stuff,Error>) -> Void) { ... }

getStuff { result in 
  switch result { 
  case .success(let stuff):
  ...

Another option would be to use a "Future" style type, which seems to cut down on the nested closures.

I found both Combine's Future and NIO's EventLoopFuture. Is there any reason to prefer one or the other?

It's hard to answer your question without knowing a few things. Do you use Swift NIO in your code already? What platforms do you need to support?

I'm just getting started with this project. Right now it's iOS and macOS clients. Linux is a long ways away if ever, but not out of the question.

I'm using the Soto AWS SDK, which is using NIO and EventLoopFuture, so it's already in my package dependency list.

Standalone Future in Combine (that is, without using the rest of available Publisher operators) probably wouldn't give enough benefits as compared to EventLoopFuture that you probably already use when interacting with Soto.

If it's a SwiftUI app, or if you use TCA, then converting from EventLoopFuture to Combine's Future could occasionally be beneficial, but I don't know if there's a recommended way to do it properly. I think it wouldn't be too hard to implement though. You just need to be aware that EventLoopFuture is always tied to an event loop, while with Combine's Future you need to maintain subscriptions for it to work.

The benefit of having that conversion working would be that there's much more in Combine (and OpenCombine if you're interested in supporting other platforms) than a plain Future.

We could say that comparing Combine's Future to NIO's EventLoopFuture is an apple-to-apple comparison, but Combine vs NIO would be an apple-to-oranges comparison then. :slightly_smiling_face: Both of these frameworks give you much more than cutting down on nested closures, and it depends on what other libraries you use in the app (like Soto, SwiftUI, TCA etc) and what's the overall architecture.

Thanks. I'm going to use EventLoopFuture. NIO looks really nice, and I like that it's open source and cross platform.

I've been using SwiftUI, but with Combine I have yet to write anything beyond basic event handler style code.

1 Like
Terms of Service

Privacy Policy

Cookie Policy