Skip to content

Set the number of events per flush to 1,000 to reduce memory footprint #596

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

Merged
merged 2 commits into from
Mar 22, 2023
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
1 change: 1 addition & 0 deletions Sources/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ struct QueueConstants {

struct APIConstants {
static let batchSize = 50
static let flushSize = 1000
static let minRetryBackoff = 60.0
static let maxRetryBackoff = 600.0
static let failuresTillBackoff = 2
Expand Down
6 changes: 3 additions & 3 deletions Sources/Flush.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Foundation

protocol FlushDelegate: AnyObject {
func flush(completion: (() -> Void)?)
func flush(performFullFlush: Bool, completion: (() -> Void)?)
func flushSuccess(type: FlushType, ids: [Int32])

#if os(iOS)
Expand Down Expand Up @@ -37,7 +37,7 @@ class Flush: AppLifecycle {
_flushInterval = newValue
})

delegate?.flush(completion: nil)
delegate?.flush(performFullFlush: false, completion: nil)
startFlushTimer()
}
}
Expand Down Expand Up @@ -73,7 +73,7 @@ class Flush: AppLifecycle {
}

@objc func flushSelector() {
delegate?.flush(completion: nil)
delegate?.flush(performFullFlush: false, completion: nil)
}

func stopFlushTimer() {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Group.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ open class Group {
}

if MixpanelInstance.isiOSAppExtension() {
delegate?.flush(completion: nil)
delegate?.flush(performFullFlush: false, completion: nil)
}
}

Expand Down
17 changes: 13 additions & 4 deletions Sources/MixpanelInstance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ protocol AppLifecycle {

/// The class that represents the Mixpanel Instance
open class MixpanelInstance: CustomDebugStringConvertible, FlushDelegate, AEDelegate {

/// apiToken string that identifies the project to track data to
open var apiToken = ""

Expand Down Expand Up @@ -410,7 +411,7 @@ open class MixpanelInstance: CustomDebugStringConvertible, FlushDelegate, AEDele
taskId = sharedApplication.beginBackgroundTask(expirationHandler: completionHandler)

if flushOnBackground {
flush(completion: completionHandler)
flush(performFullFlush: true, completion: completionHandler)
}
}

Expand Down Expand Up @@ -899,9 +900,10 @@ extension MixpanelInstance {
`flushOnBackground` is on by default). You only need to call this
method manually if you want to force a flush at a particular moment.

- parameter performFullFlush: A optional boolean value indicating whether a full flush should be performed. If `true`, a full flush will be triggered, sending all events to the server. Default to `false`, a partial flush will be executed for reducing memory footprint.
- parameter completion: an optional completion handler for when the flush has completed.
*/
public func flush(completion: (() -> Void)? = nil) {
public func flush(performFullFlush: Bool = false, completion: (() -> Void)? = nil) {
if hasOptedOutTracking() {
if let completion = completion {
DispatchQueue.main.async(execute: completion)
Expand All @@ -926,10 +928,17 @@ extension MixpanelInstance {
// automatic events will NOT be flushed until one of the flags is non-nil
let eventQueue = self.mixpanelPersistence.loadEntitiesInBatch(
type: self.persistenceTypeFromFlushType(.events),
batchSize: performFullFlush ? Int.max : APIConstants.flushSize,
excludeAutomaticEvents: !self.trackAutomaticEventsEnabled
)
let peopleQueue = self.mixpanelPersistence.loadEntitiesInBatch(type: self.persistenceTypeFromFlushType(.people))
let groupsQueue = self.mixpanelPersistence.loadEntitiesInBatch(type: self.persistenceTypeFromFlushType(.groups))
let peopleQueue = self.mixpanelPersistence.loadEntitiesInBatch(
type: self.persistenceTypeFromFlushType(.people),
batchSize: performFullFlush ? Int.max : APIConstants.flushSize
)
let groupsQueue = self.mixpanelPersistence.loadEntitiesInBatch(
type: self.persistenceTypeFromFlushType(.groups),
batchSize: performFullFlush ? Int.max : APIConstants.flushSize
)

self.networkQueue.async { [weak self, completion] in
guard let self = self else {
Expand Down
2 changes: 1 addition & 1 deletion Sources/People.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ open class People {
}

if MixpanelInstance.isiOSAppExtension() {
delegate?.flush(completion: nil)
delegate?.flush(performFullFlush: false, completion: nil)
}
}

Expand Down