Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
43 changes: 23 additions & 20 deletions Mixpanel/AutomaticEvents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class AutomaticEvents: NSObject, SKPaymentTransactionObserver, SKProductsRequest
var hasAddedObserver = false
var automaticPushTracking = true
var firstAppOpen = false
let awaitingTransactionsWriteLock = DispatchQueue(label: "com.mixpanel.awaiting_transactions_writeLock")

func initializeEvents() {
let firstOpenKey = "MPFirstOpen"
Expand Down Expand Up @@ -119,21 +120,23 @@ class AutomaticEvents: NSObject, SKPaymentTransactionObserver, SKProductsRequest
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
var productsRequest = SKProductsRequest()
var productIdentifiers: Set<String> = []
objc_sync_enter(awaitingTransactions)
for transaction:AnyObject in transactions {
if let trans = transaction as? SKPaymentTransaction {
switch trans.transactionState {
case .purchased:
productIdentifiers.insert(trans.payment.productIdentifier)
awaitingTransactions[trans.payment.productIdentifier] = trans
break
case .failed: break
case .restored: break
default: break
awaitingTransactionsWriteLock.sync {
for transaction:AnyObject in transactions {
if let trans = transaction as? SKPaymentTransaction {
switch trans.transactionState {
case .purchased:
productIdentifiers.insert(trans.payment.productIdentifier)
awaitingTransactions[trans.payment.productIdentifier] = trans
break
case .failed: break
case .restored: break
default: break
}
}
}
}
objc_sync_exit(awaitingTransactions)


if productIdentifiers.count > 0 {
productsRequest = SKProductsRequest(productIdentifiers: productIdentifiers)
productsRequest.delegate = self
Expand Down Expand Up @@ -163,16 +166,16 @@ class AutomaticEvents: NSObject, SKPaymentTransactionObserver, SKProductsRequest


func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
objc_sync_enter(awaitingTransactions)
for product in response.products {
if let trans = awaitingTransactions[product.productIdentifier] {
delegate?.track(event: "$ae_iap", properties: ["$ae_iap_price": "\(product.price)",
"$ae_iap_quantity": trans.payment.quantity,
"$ae_iap_name": product.productIdentifier])
awaitingTransactions.removeValue(forKey: product.productIdentifier)
awaitingTransactionsWriteLock.sync {
for product in response.products {
if let trans = awaitingTransactions[product.productIdentifier] {
delegate?.track(event: "$ae_iap", properties: ["$ae_iap_price": "\(product.price)",
"$ae_iap_quantity": trans.payment.quantity,
"$ae_iap_name": product.productIdentifier])
awaitingTransactions.removeValue(forKey: product.productIdentifier)
}
}
}
objc_sync_exit(awaitingTransactions)
}

#if DECIDE
Expand Down
2 changes: 0 additions & 2 deletions Mixpanel/AutomaticProperties.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class AutomaticProperties {
static let automaticPropertiesLock = ReadWriteLock(label: "automaticPropertiesLock")

static var properties: InternalProperties = {
objc_sync_enter(AutomaticProperties.self); defer { objc_sync_exit(AutomaticProperties.self) }
var p = InternalProperties()
#if os(iOS) || TV_OS
let screenSize = UIScreen.main.bounds.size
Expand Down Expand Up @@ -62,7 +61,6 @@ class AutomaticProperties {
}()

static var peopleProperties: InternalProperties = {
objc_sync_enter(AutomaticProperties.self); defer { objc_sync_exit(AutomaticProperties.self) }
var p = InternalProperties()
let infoDict = Bundle.main.infoDictionary
if let infoDict = infoDict {
Expand Down
20 changes: 10 additions & 10 deletions Mixpanel/Flush.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,33 @@ protocol FlushDelegate {
}

class Flush: AppLifecycle {
let lock: ReadWriteLock
var timer: Timer?
var delegate: FlushDelegate?
var useIPAddressForGeoLocation = true
var flushRequest: FlushRequest
var flushOnBackground = true
var _flushInterval = 0.0
private let flushIntervalReadWriteLock: DispatchQueue

var flushInterval: Double {
set {
objc_sync_enter(self)
_flushInterval = newValue
objc_sync_exit(self)
flushIntervalReadWriteLock.sync(flags: .barrier, execute: {
_flushInterval = newValue
})

delegate?.flush(completion: nil)
startFlushTimer()
}
get {
objc_sync_enter(self)
defer { objc_sync_exit(self) }

return _flushInterval
flushIntervalReadWriteLock.sync {
return _flushInterval
}
}
}

required init(basePathIdentifier: String, lock: ReadWriteLock) {
required init(basePathIdentifier: String) {
self.flushRequest = FlushRequest(basePathIdentifier: basePathIdentifier)
self.lock = lock
flushIntervalReadWriteLock = DispatchQueue(label: "com.mixpanel.flush_interval.lock", attributes: .concurrent)
}

func flushEventsQueue(_ eventsQueue: Queue, automaticEventsEnabled: Bool?) -> Queue? {
Expand Down
4 changes: 2 additions & 2 deletions Mixpanel/MixpanelInstance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ open class MixpanelInstance: CustomDebugStringConvertible, FlushDelegate, AEDele
}
self.name = name
self.readWriteLock = ReadWriteLock(label: "globalLock")
flushInstance = Flush(basePathIdentifier: name, lock: self.readWriteLock)
flushInstance = Flush(basePathIdentifier: name)
#if DECIDE
decideInstance = Decide(basePathIdentifier: name, lock: self.readWriteLock)
#endif // DECIDE
Expand Down Expand Up @@ -357,7 +357,7 @@ open class MixpanelInstance: CustomDebugStringConvertible, FlushDelegate, AEDele
}
self.name = name
self.readWriteLock = ReadWriteLock(label: "globalLock")
flushInstance = Flush(basePathIdentifier: name, lock: self.readWriteLock)
flushInstance = Flush(basePathIdentifier: name)
let label = "com.mixpanel.\(self.apiToken)"
trackingQueue = DispatchQueue(label: label)
sessionMetadata = SessionMetadata(trackingQueue: trackingQueue)
Expand Down
13 changes: 0 additions & 13 deletions Mixpanel/Swizzle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,6 @@

import Foundation

extension DispatchQueue {
private static var _onceTracker = [String]()

class func once(token: String, block: () -> Void) {
objc_sync_enter(self); defer { objc_sync_exit(self) }
if _onceTracker.contains(token) {
return
}
_onceTracker.append(token)
block()
}
}

class Swizzler {
static var swizzles = [Method: Swizzle]()

Expand Down
16 changes: 8 additions & 8 deletions Mixpanel/WebSocketWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ class WebSocketWrapper: WebSocketDelegate {
var connectivityIndiciatorWindow: UIWindow? = nil
let connectCallback: (() -> Void)?
let disconnectCallback: (() -> Void)?
let sessionObjectLock: DispatchQueue

init(url: URL, keepTrying: Bool, connectCallback: (() -> Void)?, disconnectCallback: (() -> Void)?) {
open = false
connected = false
session = [String: Any]()
sessionObjectLock = DispatchQueue(label: "com.mixpanel.session_object_lock", attributes: .concurrent)
self.url = url
self.connectCallback = connectCallback
self.disconnectCallback = disconnectCallback
Expand All @@ -65,17 +67,15 @@ class WebSocketWrapper: WebSocketDelegate {
}

func setSessionObjectSynchronized(with value: Any, for key: String) {
objc_sync_enter(self)
defer { objc_sync_exit(self) }

session[key] = value
sessionObjectLock.sync(flags: .barrier, execute: {
session[key] = value
})
}

func getSessionObjectSynchronized(for key: String) -> Any? {
objc_sync_enter(self)
defer { objc_sync_exit(self) }

return session[key]
sessionObjectLock.sync {
return session[key]
}
}

func open(initiate: Bool, maxInterval: Int = 0, maxRetries: Int = 0) {
Expand Down
2 changes: 0 additions & 2 deletions MixpanelDemo/MixpanelDemoTests/MixpanelBaseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,12 @@ class MixpanelBaseTests: XCTestCase, MixpanelDelegate {

func deleteOptOutSettings(mixpanelInstance: MixpanelInstance)
{
objc_sync_enter(self)
let filePath = Persistence.filePathWithType(.optOutStatus, token: mixpanelInstance.apiToken)
do {
try FileManager.default.removeItem(atPath: filePath!)
} catch {
Logger.info(message: "Unable to remove file at path: \(filePath!)")
}
objc_sync_exit(self)
}

func mixpanelWillFlush(_ mixpanel: MixpanelInstance) -> Bool {
Expand Down