purchases-ios icon indicating copy to clipboard operation
purchases-ios copied to clipboard

Improve Swift Concurrency support

Open aboedo opened this issue 3 years ago • 8 comments

We're using Swift Concurrency in a few places. There are a couple of compiler flags that help prevent issues with Swift Concurrency, that we should leverage.

The flags are -Xfrontend -warn-concurrency -Xfrontend -enable-actor-data-race-checks

And can be enabled in Xcode -> RevenueCat Project -> RevenueCat target -> Build Settings -> Other Swift Flags.

image

Enabling them fires up a few warnings and errors. We should enable the compiler flags, and fix the warnings / errors. More context here: https://forums.swift.org/t/concurrency-in-swift-5-and-6/49337

https://app.shortcut.com/revenuecat/story/11686/enable-swift-compiler-flags-for-concurrency-and-fix-errors

aboedo avatar Dec 07 '21 15:12 aboedo

From the post:

If at any point the only way forward for the developer is to switch the language mode and fix 100 errors before they can make progress, we have failed.

Well that's kind of the case right now with these warnings. I worked on this for a while yesterday, and it's kinda of a pain to do right in a codebase that only partially supports Swift concurrency. A bunch of types in the standard library (Foundation, UIKit, etc) added @MainActor (which, I have to say, it's a great abstraction for being able to annotate "this class isn't thread safe", but...), which requires us to either:

  • Make our own types @MainActor
  • await calls that come from outside the actor.

The second option leads to my biggest pet-peeve of async/await: an explosion of everything needing to be async. FRP doesn't suffer from this as much. In our case not only does that require making lots of code asynchronous, but it's also impossible because we need to support pre-iOS 13.0. The first option works, but only delays the need for the second one further up the hierarchy.

Similarly, declarations can be marked with the “unsafe” form of a global actor, meaning that they should be considered to be a part of the global actor, but that should only be enforced in Swift 6 code or Swift 5 code that has adopted concurrency. For example:

Unfortunately @MainActor(unsafe) isn't helping us here either. It's not clear what "Swift 5 code that has adopted concurrency." refers to, but adding @MainActor(unsafe) to a method that uses a @MainActor type, and calling that method from a regular method still fails:

Property 'identifierForVendor' isolated to global actor 'MainActor' can not be referenced from this synchronous context

There's also problems with the need for APIs to have @Sendable closures, which the post talks about. @_unsafeSendable would help here, but that's not available yet.

There's a few improvements that I'll put in a PR in a bit, but for the most part unfortunately there isn't much that we can do for the time being. I say for the time being, but I'm scared of what this situation will look like in Swift 6.0.

NachoSoto avatar Dec 10 '21 18:12 NachoSoto

🤕 Xcode 14 will be a fun release. Thanks for taking a look at this!

aboedo avatar Dec 10 '21 18:12 aboedo

Good news: https://twitter.com/dgregor79/status/1470940523259056129?s=21

NachoSoto avatar Dec 15 '21 05:12 NachoSoto

Won't fix until Swift 5.6

taquitos avatar Jan 06 '22 18:01 taquitos

I am still facing this type of error

pro-akbar avatar Apr 14 '22 09:04 pro-akbar

@pro-akbar Hey! Would you be able to create a new issue and fill out all of your environment information in there? 😊 It's a bit easier to have a conversation in a new issue and reference this one. Thank you thank you! ❤️

joshdholtz avatar Apr 15 '22 18:04 joshdholtz

I think it's a good moment for this SDK to start preparing for Swift 6 and adopt Sendable where possible.

It should be doable to add Sendable inheritance for some types. I've currently got these warnings:

CleanShot 2022-08-05 at 15 13 03@2x

AvdLee avatar Aug 05 '22 13:08 AvdLee

@AvdLee thanks for bringing this up!

We tried it out a while back but had some issues that we would hoping would go away in newer Xcode betas. We'll give it another shot.

aboedo avatar Aug 05 '22 17:08 aboedo

@AvdLee Sendable support should be ready in #1795. Would you be able to give that branch a try? If not we could release that as a beta for you to try it in your app.

NachoSoto avatar Aug 16 '22 18:08 NachoSoto

Not relevant to this issue but just wanted to say... Hi @AvdLee! 👋

joshdholtz avatar Aug 17 '22 18:08 joshdholtz

Should we close this? :)

NachoSoto avatar Aug 31 '22 20:08 NachoSoto

I think we're all done with this one! Closing

aboedo avatar Aug 31 '22 20:08 aboedo

Changes will be released in the next version, next Wednesday or sooner

aboedo avatar Aug 31 '22 20:08 aboedo

Thanks a lot for the hard work on this @NachoSoto! And Hi back to you @joshdholtz ⛳️

AvdLee avatar Sep 15 '22 09:09 AvdLee

Wow... always rubbing in that you won that round of golf, aren't you? 😛

joshdholtz avatar Sep 15 '22 15:09 joshdholtz

Of course, I need to create extra reason to meet up again for another round of golf 🤪

AvdLee avatar Sep 19 '22 06:09 AvdLee