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

Feature/defer cache updates if woken from push notification #288

Merged

Conversation

aboedo
Copy link
Member

@aboedo aboedo commented Jul 20, 2020

Updates so that when the SDK's configure is called with the app in the background, it won't try to update offerings or purchaser info.

They will, however, be updated if the app becomes active, per the didBecomeActivenotification (that was already the current behavior).

This also ensures that if the app is in the background and there's purchaser info cached, the purchaserInfo delegate gets called with the cached value. I believe this is different than the android behavior.

This behavior ensures that we don't make unnecessary backend calls in the following two scenarios:

Note: this is the iOS equivalent of
RevenueCat/purchases-android#94

@aboedo aboedo self-assigned this Jul 20, 2020
@aboedo aboedo requested a review from vegaro July 20, 2020 22:34
#import <UIKit/UIKit.h>
#elif TARGET_OS_OSX
#import <AppKit/AppKit.h>
#elif TARGET_OS_WATCH
#import <UIKit/UIKit.h>
#import <WatchKit/WatchKit.h>
Copy link
Member Author

Choose a reason for hiding this comment

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

watch apps have both

#elif TARGET_OS_OSX
#define IS_APPLICATION_BACKGROUNDED !NSApplication.sharedApplication.isActive
#elif TARGET_OS_WATCH
#define IS_APPLICATION_BACKGROUNDED WKExtension.sharedExtension.applicationState == WKApplicationStateBackground
Copy link
Member Author

Choose a reason for hiding this comment

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

Apple's API consistency at its finest 🤪

if (!self.systemInfo.isApplicationBackgrounded) {
[self updateAllCachesWithCompletionBlock:callDelegate];
} else {
[self sendCachedPurchaserInfoIfAvailable];
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 bit is so that if there's something cached we still call the delegate. I believe this is a departure from Android. @vegaro was there a specific reason not to call the delegate there? I'm worried that not calling it is a bit inconsistent with the philosophy of sending whatever is cached as fast as we can.

Copy link
Contributor

Choose a reason for hiding this comment

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

You're right, we should be calling in Android. I think that's what I would expect as a developer.

@@ -209,8 +209,7 @@ class PurchasesTests: XCTestCase {
func setupAnonPurchases() {
Purchases.automaticAppleSearchAdsAttributionCollection = false
self.identityManager.mockIsAnonymous = true
let systemInfo = RCSystemInfo(platformFlavor: nil, platformFlavorVersion: nil, finishTransactions: true)
Copy link
Member Author

Choose a reason for hiding this comment

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

just cleanup here, this line was duplicated in a couple of places

Copy link
Contributor

@vegaro vegaro left a comment

Choose a reason for hiding this comment

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

LGTM!

#elif TARGET_OS_OSX
#define IS_APPLICATION_BACKGROUNDED !NSApplication.sharedApplication.isActive
#elif TARGET_OS_WATCH
#define IS_APPLICATION_BACKGROUNDED WKExtension.sharedExtension.applicationState == WKApplicationStateBackground
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice! Much easier than in Android!

#if TARGET_OS_IOS || TARGET_OS_TV
#define IS_APPLICATION_BACKGROUNDED UIApplication.sharedApplication.applicationState == UIApplicationStateBackground
#elif TARGET_OS_OSX
#define IS_APPLICATION_BACKGROUNDED !NSApplication.sharedApplication.isActive
Copy link
Member Author

@aboedo aboedo Jul 21, 2020

Choose a reason for hiding this comment

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

in my testing, isActive is returning nil. I'm going to be conservative here and just return NO for mac, since mac apps have a different lifecycle anyway.

… for NSApplication.sharedApplication.isActive is nil for whatever reason.
@aboedo aboedo merged commit abd1936 into develop Jul 23, 2020
@aboedo aboedo deleted the feature/defer_cache_updates_if_woken_from_push_notification branch July 23, 2020 18:49
vegaro pushed a commit that referenced this pull request Jul 28, 2020
* Added logic to skip updating cache on setup if app is not active

* inverted checks from is app active to !is app backgrounded

* made the app backgrounded method an instance method to make tests easier. Added tests to check that caches aren't updated when the SDK is set up with the app running in the background.

* updated value of isApplicationBackgrounded for macOS, since the value for NSApplication.sharedApplication.isActive is nil for whatever reason.
@vegaro vegaro mentioned this pull request Jul 28, 2020
vegaro added a commit that referenced this pull request Jul 28, 2020
* Feature/defer cache updates if woken from push notification (#288)

* Added logic to skip updating cache on setup if app is not active

* inverted checks from is app active to !is app backgrounded

* made the app backgrounded method an instance method to make tests easier. Added tests to check that caches aren't updated when the SDK is set up with the app running in the background.

* updated value of isApplicationBackgrounded for macOS, since the value for NSApplication.sharedApplication.isActive is nil for whatever reason.

* Version 3.5.2

Co-authored-by: aboedo <andresboedo@gmail.com>
@vegaro vegaro added this to the 3.5.2 milestone Sep 11, 2020
@aboedo aboedo mentioned this pull request Jul 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants