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

Fixes the PurchasesDelegate being deallocated on iOS #214

Merged
merged 3 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ public expect class Purchases {
/**
* The delegate is responsible for handling promotional product purchases (App Store only) and
* changes to customer information.
*
* **Note:** If your delegate is not a singleton, make sure you set this back to null when
* you're done to avoid memory leaks. For instance, if your delegate is tied to a screen, set
* this to null when the user navigates away from the screen.
Comment on lines +111 to +113
Copy link
Member Author

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.

Copy link
Contributor

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 👍

*/
public var delegate: PurchasesDelegate?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public actual class Purchases private constructor(private val iosPurchases: IosP

public actual var delegate: PurchasesDelegate?
get() = iosPurchases.delegate()?.toPurchasesDelegate()
set(value) = iosPurchases.setDelegate(value?.toRcPurchasesDelegate())
set(value) = iosPurchases.setDelegate(value.toRcPurchasesDelegate())

public actual val isAnonymous: Boolean
get() = iosPurchases.isAnonymous()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is changed so that when the delegate is set to null, the strong reference is cleared.


private class PurchasesDelegateWrapper(val wrapped: PurchasesDelegate) :
RCPurchasesDelegateProtocol,
Expand Down Expand Up @@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

The 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 Purchases object lifecycle... So if the Purchases object is cleared, this is also cleared... Wdyt?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh great suggestion! This should do it: cc4f72b.

var delegate: RCPurchasesDelegateProtocol? = null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to just hold this inside the Purchases.ios class instead? Just to make sure if it gets dereferenced (without calling the close function, we also clear it. (honestly this is an edge case, since usually Purchases would live forever, but just in case)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes that makes way more sense haha! 1810b44

}