@@ -28,6 +28,7 @@ public class Analytics {
28
28
29
29
static internal let deadInstance = " DEADINSTANCE "
30
30
static internal weak var firstInstance : Analytics ? = nil
31
+ @Atomic static internal var activeWriteKeys = [ String] ( )
31
32
32
33
/**
33
34
This method isn't a traditional singleton implementation. It's provided here
@@ -58,6 +59,12 @@ public class Analytics {
58
59
/// - Parameters:
59
60
/// - configuration: The configuration to use
60
61
public init ( configuration: Configuration ) {
62
+ if Self . isActiveWriteKey ( configuration. values. writeKey) {
63
+ fatalError ( " Cannot initialize multiple instances of Analytics with the same write key " )
64
+ } else {
65
+ Self . addActiveWriteKey ( configuration. values. writeKey)
66
+ }
67
+
61
68
store = Store ( )
62
69
storage = Storage ( store: self . store, writeKey: configuration. values. writeKey)
63
70
timeline = Timeline ( )
@@ -74,6 +81,10 @@ public class Analytics {
74
81
platformStartup ( )
75
82
}
76
83
84
+ deinit {
85
+ Self . removeActiveWriteKey ( configuration. values. writeKey)
86
+ }
87
+
77
88
internal func process< E: RawEvent > ( incomingEvent: E ) {
78
89
guard enabled == true else { return }
79
90
let event = incomingEvent. applyRawEventData ( store: store)
@@ -428,11 +439,26 @@ extension Analytics {
428
439
Self . firstInstance = self
429
440
}
430
441
}
431
-
442
+
432
443
/// Determines if an instance is dead.
433
444
internal var isDead : Bool {
434
445
return configuration. values. writeKey == Self . deadInstance
435
446
}
447
+
448
+ /// Manage active writekeys. It's wrapped in @atomic
449
+ internal static func isActiveWriteKey( _ writeKey: String ) -> Bool {
450
+ Self . activeWriteKeys. contains ( writeKey)
451
+ }
452
+
453
+ internal static func addActiveWriteKey( _ writeKey: String ) {
454
+ Self . activeWriteKeys. append ( writeKey)
455
+ }
456
+
457
+ internal static func removeActiveWriteKey( _ writeKey: String ) {
458
+ Self . activeWriteKeys. removeAll { key in
459
+ writeKey == key
460
+ }
461
+ }
436
462
}
437
463
438
464
// MARK: Operating mode based scheduling
0 commit comments