Skip to content

Commit

Permalink
Merge pull request #85 from superwall-me/hotfix/products-bg-thread-issue
Browse files Browse the repository at this point in the history
Hotfix/products bg thread issue
  • Loading branch information
yusuftor authored Nov 7, 2022
2 parents c282b06 + b81cd03 commit 10d73ed
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 46 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

The changelog for `Paywall`. Also see the [releases](https://github.com/superwall-me/paywall-ios/releases) on GitHub.

## 2.5.4

### Fixes

- Fixes a crash issue where the completion blocks for triggering a paywall were being called on a background thread in a specific scenario.
- Fixes an issue where lazy properties were causing an occasional crash due to the use of multithreading.

---

## 2.5.3

### Fixes
Expand Down
2 changes: 1 addition & 1 deletion Paywall.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "Paywall"
s.version = "2.5.3"
s.version = "2.5.4"
s.summary = "Superwall: In-App Paywalls Made Easy"
s.description = "Paywall infrastructure for mobile apps :) we make things like editing your paywall and running price tests as easy as clicking a few buttons. superwall.com"

Expand Down
4 changes: 0 additions & 4 deletions Sources/Paywall/Debug/SWDebugViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,6 @@ final class SWDebugViewController: UIViewController {

init() {
super.init(nibName: nil, bundle: nil)
if #available(iOS 13.0, *) {
overrideUserInterfaceStyle = .dark
}
}

required init?(coder: NSCoder) {
Expand Down Expand Up @@ -461,7 +458,6 @@ extension SWDebugViewController {
func presentAlert(title: String?, message: String?, options: [AlertOption]) {
let alertController = UIAlertController(title: title, message: message, preferredStyle: .actionSheet)
if #available(iOS 13.0, *) {
alertController.overrideUserInterfaceStyle = .dark
alertController.view.tintColor = primaryColor
} else {
alertController.view.tintColor = darkBackgroundColor
Expand Down
2 changes: 1 addition & 1 deletion Sources/Paywall/Misc/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ let sdkVersion = """
*/

let sdkVersion = """
2.5.3
2.5.4
"""
66 changes: 34 additions & 32 deletions Sources/Paywall/Network/DeviceHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,41 @@ import CoreTelephony

class DeviceHelper {
static let shared = DeviceHelper()
let reachability = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, Api.hostDomain)

var locale: String {
LocalizationManager.shared.selectedLocale ?? Locale.autoupdatingCurrent.identifier
}
let appInstalledAtString: String

private let reachability = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, Api.hostDomain)
var appVersion: String {
Bundle.main.releaseVersionNumber ?? ""
}

lazy var osVersion: String = {
let osVersion: String = {
let systemVersion = ProcessInfo.processInfo.operatingSystemVersion
return String(
format: "%ld.%ld.%ld",
arguments: [systemVersion.majorVersion, systemVersion.minorVersion, systemVersion.patchVersion]
)
}()

lazy var isMac: Bool = {
let isMac: Bool = {
var output = false
if #available(iOS 14.0, *) {
output = ProcessInfo.processInfo.isiOSAppOnMac
}
return output
}()

lazy var model: String = {
let model: String = {
UIDevice.modelName
}()

lazy var vendorId: String = {
let vendorId: String = {
UIDevice.current.identifierForVendor?.uuidString ?? ""
}()

var locale: String {
LocalizationManager.shared.selectedLocale ?? Locale.autoupdatingCurrent.identifier
}

var languageCode: String {
Locale.autoupdatingCurrent.languageCode ?? ""
}
Expand Down Expand Up @@ -104,11 +106,11 @@ class DeviceHelper {
return ProcessInfo.processInfo.isLowPowerModeEnabled ? "true" : "false"
}

lazy var bundleId: String = {
let bundleId: String = {
return Bundle.main.bundleIdentifier ?? ""
}()

lazy var appInstallDate: Date? = {
private let appInstallDate: Date? = {
guard let urlToDocumentsFolder = FileManager.default.urls(
for: .documentDirectory,
in: .userDomainMask
Expand All @@ -124,18 +126,14 @@ class DeviceHelper {
return installDate
}()

lazy var appInstalledAtString: String = {
return appInstallDate?.isoString ?? ""
}()

var daysSinceInstall: Int {
private var daysSinceInstall: Int {
let fromDate = appInstallDate ?? Date()
let toDate = Date()
let numberOfDays = Calendar.current.dateComponents([.day], from: fromDate, to: toDate)
return numberOfDays.day ?? 0
}

private lazy var localDateFormatter: DateFormatter = {
private let localDateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.calendar = Calendar(identifier: .iso8601)
formatter.locale = Locale(identifier: "en_US_POSIX")
Expand All @@ -144,7 +142,7 @@ class DeviceHelper {
return formatter
}()

private lazy var utcDateFormatter: DateFormatter = {
private let utcDateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.calendar = Calendar(identifier: .iso8601)
formatter.locale = Locale(identifier: "en_US_POSIX")
Expand All @@ -153,7 +151,7 @@ class DeviceHelper {
return formatter
}()

private lazy var utcTimeFormatter: DateFormatter = {
private let utcTimeFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.calendar = Calendar(identifier: .iso8601)
formatter.locale = Locale(identifier: "en_US_POSIX")
Expand All @@ -162,7 +160,7 @@ class DeviceHelper {
return formatter
}()

private lazy var localDateTimeFormatter: DateFormatter = {
private let localDateTimeFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.calendar = Calendar(identifier: .iso8601)
formatter.locale = Locale(identifier: "en_US_POSIX")
Expand All @@ -171,7 +169,7 @@ class DeviceHelper {
return formatter
}()

private lazy var localTimeFormatter: DateFormatter = {
private let localTimeFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.calendar = Calendar(identifier: .iso8601)
formatter.locale = Locale(identifier: "en_US_POSIX")
Expand All @@ -180,7 +178,7 @@ class DeviceHelper {
return formatter
}()

private lazy var utcDateTimeFormatter: DateFormatter = {
private let utcDateTimeFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.calendar = Calendar(identifier: .iso8601)
formatter.locale = Locale(identifier: "en_US_POSIX")
Expand All @@ -189,38 +187,38 @@ class DeviceHelper {
return formatter
}()

var localDateString: String {
private var localDateString: String {
return localDateFormatter.string(from: Date())
}

var localTimeString: String {
private var localTimeString: String {
return localTimeFormatter.string(from: Date())
}

var localDateTimeString: String {
private var localDateTimeString: String {
return localDateTimeFormatter.string(from: Date())
}

var utcDateString: String {
private var utcDateString: String {
return utcDateFormatter.string(from: Date())
}

var utcTimeString: String {
private var utcTimeString: String {
return utcTimeFormatter.string(from: Date())
}

var utcDateTimeString: String {
private var utcDateTimeString: String {
return utcDateTimeFormatter.string(from: Date())
}

var minutesSinceInstall: Int {
private var minutesSinceInstall: Int {
let fromDate = appInstallDate ?? Date()
let toDate = Date()
let numberOfMinutes = Calendar.current.dateComponents([.minute], from: fromDate, to: toDate)
return numberOfMinutes.minute ?? 0
}

var daysSinceLastPaywallView: Int? {
private var daysSinceLastPaywallView: Int? {
guard let fromDate = Storage.shared.getLastPaywallView() else {
return nil
}
Expand All @@ -229,7 +227,7 @@ class DeviceHelper {
return numberOfDays.day
}

var minutesSinceLastPaywallView: Int? {
private var minutesSinceLastPaywallView: Int? {
guard let fromDate = Storage.shared.getLastPaywallView() else {
return nil
}
Expand All @@ -238,7 +236,7 @@ class DeviceHelper {
return numberOfMinutes.minute
}

var totalPaywallViews: Int {
private var totalPaywallViews: Int {
return Storage.shared.getTotalPaywallViews() ?? 0
}

Expand Down Expand Up @@ -283,4 +281,8 @@ class DeviceHelper {
localDateTime: localDateTimeString
)
}

init() {
self.appInstalledAtString = appInstallDate?.isoString ?? ""
}
}
12 changes: 9 additions & 3 deletions Sources/Paywall/StoreKit/ProductsManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ class ProductsManager: NSObject {
info: ["product_ids": identifiers],
error: nil
)
completion(.success(productsAlreadyCachedSet))
DispatchQueue.main.async {
completion(.success(productsAlreadyCachedSet))
}
return
}

Expand Down Expand Up @@ -127,7 +129,9 @@ extension ProductsManager: SKProductsRequestDelegate {

self.cacheProducts(response.products)
for completion in completionBlocks {
completion(.success(Set(response.products)))
DispatchQueue.main.async {
completion(.success(Set(response.products)))
}
}
}
}
Expand Down Expand Up @@ -176,7 +180,9 @@ extension ProductsManager: SKProductsRequestDelegate {
self.completionHandlers.removeValue(forKey: products)
self.productsByRequests.removeValue(forKey: request)
for completion in completionBlocks {
completion(.failure(error))
DispatchQueue.main.async {
completion(.failure(error))
}
}
}
request.cancel()
Expand Down
5 changes: 0 additions & 5 deletions Tests/PaywallTests/Network/DeviceHelperMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,8 @@ import Foundation

final class DeviceHelperMock: DeviceHelper {
var internalLocale: String?
var internalMinutesSinceInstall: Int = 0

override var locale: String {
return internalLocale ?? super.locale
}

override var minutesSinceInstall: Int {
return internalMinutesSinceInstall
}
}

0 comments on commit 10d73ed

Please sign in to comment.