-
Notifications
You must be signed in to change notification settings - Fork 3
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
Fixes the PurchasesDelegate being deallocated on iOS #214
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,8 +15,9 @@ import platform.darwin.NSObject | |
internal fun RCPurchasesDelegateProtocol.toPurchasesDelegate(): PurchasesDelegate = | ||
(this as PurchasesDelegateWrapper).wrapped | ||
|
||
internal fun PurchasesDelegate.toRcPurchasesDelegate(): RCPurchasesDelegateProtocol = | ||
PurchasesDelegateWrapper(this) | ||
internal fun PurchasesDelegate?.toRcPurchasesDelegate(): RCPurchasesDelegateProtocol? = | ||
this?.let { PurchasesDelegateWrapper(it) } | ||
.also { PurchasesDelegateStrongReference.delegate = it } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is changed so that when the |
||
|
||
private class PurchasesDelegateWrapper(val wrapped: PurchasesDelegate) : | ||
RCPurchasesDelegateProtocol, | ||
|
@@ -45,3 +46,20 @@ private class PurchasesDelegateWrapper(val wrapped: PurchasesDelegate) : | |
} | ||
|
||
} | ||
|
||
/** | ||
* On the iOS platform side, the backing field of `Purchases.delegate`, `Purchases.privateDelegate`, | ||
* is a `weak var`. On the multiplatform side, we wrap the actual delegate in | ||
* `PurchasesDelegateWrapper` to conform to `RCPurchasesDelegateProtocol`. Since that is a private | ||
* implementation detail, the only reference held to our wrapper is the `weak var`. This means that | ||
* it gets deallocated the first chance it gets. For this reason, we keep a strong reference to our | ||
* `PurchasesDelegateWrapper` here. This matches the Android behavior, which keeps a strong | ||
* reference on the platform side already. | ||
* | ||
* **Note**: we cannot make our `PurchasesDelegateWrapper` an `object` directly, as objects cannot | ||
* extend `NSObject`. See also | ||
* [KT67930](https://youtrack.jetbrains.com/issue/KT-67930/Getting-Crash-while-building-KMM-project-with-XCode-15.3#focus=Comments-27-9796215.0-0) | ||
*/ | ||
private object PurchasesDelegateStrongReference { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm it would be great if we could at least tie it to the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh great suggestion! This should do it: cc4f72b. |
||
var delegate: RCPurchasesDelegateProtocol? = null | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be possible to just hold this inside the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah yes that makes way more sense haha! 1810b44 |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This already applied to Android, so would already have been useful to call out, but now it applies to both platforms.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm it's true that in iOS delegates are much more commonly held weakly... But yeah, since this is KMP, I think this makes sense 👍