Skip to content

Commit 1e9728f

Browse files
committed
clean up code for iOS9.0
1 parent 31569d6 commit 1e9728f

File tree

6 files changed

+34
-174
lines changed

6 files changed

+34
-174
lines changed

Sources/Customization/DefaultEventDispatcher.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ open class DefaultEventDispatcher: BackgroundingCallbacks, OPTEventDispatcher {
216216

217217
func startTimer() {
218218
// timer is activated only for iOS10+ and non-zero interval value
219-
guard #available(iOS 10.0, tvOS 10.0, *), timerInterval > 0 else {
219+
guard timerInterval > 0 else {
220220
flushEvents()
221221
return
222222
}

Sources/Customization/DefaultLogger.swift

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,20 @@ open class DefaultLogger: OPTLogger {
4141
return
4242
}
4343

44-
if #available(iOS 10.0, tvOS 10.0, *) {
45-
var osLogType: OSLogType
46-
47-
switch level {
48-
case .error: osLogType = .error
49-
case .info: osLogType = .info
50-
case .debug: osLogType = .debug
51-
default: osLogType = .default
52-
}
53-
54-
os_log("[%{public}@] %{public}@", log: .optimizely, type: osLogType, level.name, message)
55-
osLogUsed = true
56-
} else {
57-
let message = "[OPTIMIZELY][" + level.name + "] " + message
58-
NSLog(message)
44+
var osLogType: OSLogType
45+
46+
switch level {
47+
case .error: osLogType = .error
48+
case .info: osLogType = .info
49+
case .debug: osLogType = .debug
50+
default: osLogType = .default
5951
}
52+
53+
os_log("[%{public}@] %{public}@", log: .optimizely, type: osLogType, level.name, message)
54+
osLogUsed = true
6055
}
6156
}
6257

63-
@available(iOS 10.0, tvOS 10.0, *)
6458
extension OSLog {
6559
static let optimizely = OSLog(subsystem: "com.optimizely.swift-sdk", category: "OPTIMIZELY")
6660
}

Sources/Implementation/DefaultDatafileHandler.swift

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -135,47 +135,27 @@ class DefaultDatafileHandler: OPTDatafileHandler {
135135
func startPeriodicUpdates(sdkKey: String, updateInterval: Int, datafileChangeNotification: ((Data) -> Void)?) {
136136

137137
let now = Date()
138-
if #available(iOS 10.0, tvOS 10.0, *) {
139-
DispatchQueue.main.async {
140-
if let timer = self.timers.property?[sdkKey]?.timer, timer.isValid {
141-
return
142-
}
143-
144-
let timer = Timer.scheduledTimer(withTimeInterval: TimeInterval(updateInterval), repeats: false) { (timer) in
145-
146-
self.performPerodicDownload(sdkKey: sdkKey,
147-
startTime: now,
148-
updateInterval: updateInterval,
149-
datafileChangeNotification: datafileChangeNotification)
150-
151-
timer.invalidate()
152-
}
153-
self.timers.performAtomic(atomicOperation: { (timers) in
154-
if let interval = timers[sdkKey]?.interval {
155-
timers[sdkKey] = (timer, interval)
156-
} else {
157-
timers[sdkKey] = (timer, updateInterval)
158-
}
159-
})
138+
DispatchQueue.main.async {
139+
if let timer = self.timers.property?[sdkKey]?.timer, timer.isValid {
140+
return
160141
}
161-
} else {
162-
// Fallback on earlier versions
163-
DispatchQueue.main.async {
164-
if let timer = self.timers.property?[sdkKey]?.timer, timer.isValid {
165-
return
166-
}
167-
168-
let timer = Timer.scheduledTimer(timeInterval: TimeInterval(updateInterval), target: self, selector: #selector(self.timerFired(timer:)), userInfo: ["sdkKey": sdkKey, "startTime": Date(), "updateInterval": updateInterval, "datafileChangeNotification": datafileChangeNotification ?? { (data) in }], repeats: false)
142+
143+
let timer = Timer.scheduledTimer(withTimeInterval: TimeInterval(updateInterval), repeats: false) { (timer) in
169144

170-
self.timers.performAtomic(atomicOperation: { (timers) in
171-
if let interval = timers[sdkKey]?.interval {
172-
timers[sdkKey] = (timer, interval)
173-
} else {
174-
timers[sdkKey] = (timer, updateInterval)
175-
}
176-
})
145+
self.performPerodicDownload(sdkKey: sdkKey,
146+
startTime: now,
147+
updateInterval: updateInterval,
148+
datafileChangeNotification: datafileChangeNotification)
149+
150+
timer.invalidate()
177151
}
178-
152+
self.timers.performAtomic(atomicOperation: { (timers) in
153+
if let interval = timers[sdkKey]?.interval {
154+
timers[sdkKey] = (timer, interval)
155+
} else {
156+
timers[sdkKey] = (timer, updateInterval)
157+
}
158+
})
179159
}
180160
}
181161

Tests/OptimizelyTests-Common/EventDispatcherTests.swift

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,7 @@ class EventDispatcherTests: XCTestCase {
6060
eventDispatcher?.dispatcher.sync {
6161
}
6262

63-
if #available(iOS 10.0, tvOS 10.0, *) {
64-
XCTAssert(eventDispatcher?.dataStore.count == 1)
65-
} else {
66-
XCTAssert(eventDispatcher?.dataStore.count == 0)
67-
}
63+
XCTAssert(eventDispatcher?.dataStore.count == 1)
6864
eventDispatcher?.flushEvents()
6965

7066
eventDispatcher?.dispatcher.sync {
@@ -119,11 +115,7 @@ class EventDispatcherTests: XCTestCase {
119115
}
120116
wait()
121117

122-
if #available(iOS 10.0, tvOS 10.0, *) {
123-
XCTAssert(eventDispatcher?.dataStore.count == 1)
124-
} else {
125-
XCTAssert(eventDispatcher?.dataStore.count == 0)
126-
}
118+
XCTAssert(eventDispatcher?.dataStore.count == 1)
127119

128120
eventDispatcher?.flushEvents()
129121
wait()
@@ -151,11 +143,7 @@ class EventDispatcherTests: XCTestCase {
151143
}
152144
wait()
153145

154-
if #available(iOS 10.0, tvOS 10.0, *) {
155-
XCTAssert(eventDispatcher?.dataStore.count == 1)
156-
} else {
157-
XCTAssert(eventDispatcher?.dataStore.count == 0)
158-
}
146+
XCTAssert(eventDispatcher?.dataStore.count == 1)
159147

160148
eventDispatcher?.flushEvents()
161149
wait()
@@ -182,11 +170,7 @@ class EventDispatcherTests: XCTestCase {
182170
}
183171
wait()
184172

185-
if #available(iOS 10.0, tvOS 10.0, *) {
186-
XCTAssert(eventDispatcher?.dataStore.count == 1)
187-
} else {
188-
XCTAssert(eventDispatcher?.dataStore.count == 0)
189-
}
173+
XCTAssert(eventDispatcher?.dataStore.count == 1)
190174

191175
eventDispatcher?.flushEvents()
192176
wait()

Tests/OptimizelyTests-Common/EventDispatcherTests_Batch.swift

Lines changed: 0 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,6 @@ extension EventDispatcherTests_Batch {
276276
}
277277

278278
func testEventDiscardedWhenQueueIfFull() {
279-
// this tests timer-based dispatch, available for iOS 10+
280-
guard #available(iOS 10.0, tvOS 10.0, *) else { return }
281-
282279
eventDispatcher.maxQueueSize = 100
283280

284281
// illegal config batchSize cannot be bigger than maxQueueSize. just for testing
@@ -336,9 +333,6 @@ extension EventDispatcherTests_Batch {
336333
extension EventDispatcherTests_Batch {
337334

338335
func testFlushEvents() {
339-
// this tests timer-based dispatch, available for iOS 10+
340-
guard #available(iOS 10.0, tvOS 10.0, *) else { return }
341-
342336
XCTAssert(eventDispatcher.batchSize == 10)
343337

344338
dispatchMultipleEvents([(kUrlA, batchEventA),
@@ -369,9 +363,6 @@ extension EventDispatcherTests_Batch {
369363
}
370364

371365
func testFlushEventsWhenBatchFails() {
372-
// this tests timer-based dispatch, available for iOS 10+
373-
guard #available(iOS 10.0, tvOS 10.0, *) else { return }
374-
375366
XCTAssert(eventDispatcher.batchSize == 10)
376367

377368
dispatchMultipleEvents([(kUrlA, batchEventA),
@@ -416,9 +407,6 @@ extension EventDispatcherTests_Batch {
416407
}
417408

418409
func testFlushEventsWhenBatchFailsWithInvalidEvent() {
419-
// this tests timer-based dispatch, available for iOS 10+
420-
guard #available(iOS 10.0, tvOS 10.0, *) else { return }
421-
422410
XCTAssert(eventDispatcher.batchSize == 10)
423411

424412
eventDispatcher.dispatchEvent(event: makeEventForDispatch(url: kUrlA, event: batchEventA), completionHandler: nil)
@@ -467,9 +455,6 @@ extension EventDispatcherTests_Batch {
467455

468456

469457
func testFlushEventsWhenSendEventFailsAndRecovers() {
470-
// this tests timer-based dispatch, available for iOS 10+
471-
guard #available(iOS 10.0, tvOS 10.0, *) else { return }
472-
473458
// (1) error injected - all event send fails
474459

475460
eventDispatcher.forceError = true
@@ -517,9 +502,6 @@ extension EventDispatcherTests_Batch {
517502
extension EventDispatcherTests_Batch {
518503

519504
func testEventDispatchedOnTimer() {
520-
// this tests timer-based dispatch, available for iOS 10+
521-
guard #available(iOS 10.0, tvOS 10.0, *) else { return }
522-
523505
eventDispatcher.timerInterval = 2
524506

525507
eventDispatcher.exp = expectation(description: "timer")
@@ -545,9 +527,6 @@ extension EventDispatcherTests_Batch {
545527
}
546528

547529
func testEventShouldNotBeSentUntilTimer() {
548-
// this tests timer-based dispatch, available for iOS 10+
549-
guard #available(iOS 10.0, tvOS 10.0, *) else { return }
550-
551530
eventDispatcher.timerInterval = 99999
552531

553532
eventDispatcher.exp = expectation(description: "timer")
@@ -562,9 +541,6 @@ extension EventDispatcherTests_Batch {
562541
}
563542

564543
func testEventDispatchedOnTimer_ZeroInterval() {
565-
// this tests timer-based dispatch, available for iOS 10+
566-
guard #available(iOS 10.0, tvOS 10.0, *) else { return }
567-
568544
// zero-interval means that all events are sent out immediately
569545
eventDispatcher.timerInterval = 0
570546

@@ -599,9 +575,6 @@ extension EventDispatcherTests_Batch {
599575
}
600576

601577
func testEventBatchedOnTimer_CheckNoRedundantSend() {
602-
// this tests timer-based dispatch, available for iOS 10+
603-
guard #available(iOS 10.0, tvOS 10.0, *) else { return }
604-
605578
eventDispatcher.timerInterval = 3
606579

607580
eventDispatcher.exp = expectation(description: "timer")
@@ -622,9 +595,6 @@ extension EventDispatcherTests_Batch {
622595
}
623596

624597
func testEventBatchedAndErrorRecoveredOnTimer() {
625-
// this tests timer-based dispatch, available for iOS 10+
626-
guard #available(iOS 10.0, tvOS 10.0, *) else { return }
627-
628598
eventDispatcher.timerInterval = 5
629599

630600
// (1) inject error
@@ -658,9 +628,6 @@ extension EventDispatcherTests_Batch {
658628
extension EventDispatcherTests_Batch {
659629

660630
func testEventsFlushedOnEventQueueSizeHit() {
661-
// this tests timer-based dispatch, available for iOS 10+
662-
guard #available(iOS 10.0, tvOS 10.0, *) else { return }
663-
664631
eventDispatcher.batchSize = 3
665632
eventDispatcher.timerInterval = 99999 // timer is big, won't fire
666633

@@ -686,9 +653,6 @@ extension EventDispatcherTests_Batch {
686653
}
687654

688655
func testEventsFlushedOnRevisionChange() {
689-
// this tests timer-based dispatch, available for iOS 10+
690-
guard #available(iOS 10.0, tvOS 10.0, *) else { return }
691-
692656
self.eventDispatcher = TestEventDispatcher(eventFileName: uniqueFileName, removeDatafileObserver: false)
693657

694658
eventDispatcher.batchSize = 1000 // big, won't flush
@@ -724,9 +688,6 @@ extension EventDispatcherTests_Batch {
724688
}
725689

726690
func testEventsFlushedOnProjectIdChange() {
727-
// this tests timer-based dispatch, available for iOS 10+
728-
guard #available(iOS 10.0, tvOS 10.0, *) else { return }
729-
730691
self.eventDispatcher = TestEventDispatcher(eventFileName: uniqueFileName, removeDatafileObserver: false)
731692

732693
eventDispatcher.batchSize = 1000 // big, won't flush
@@ -762,9 +723,6 @@ extension EventDispatcherTests_Batch {
762723
}
763724

764725
func testEventsNotFlushedOnOtherDatafileChanges() {
765-
// this tests timer-based dispatch, available for iOS 10+
766-
guard #available(iOS 10.0, tvOS 10.0, *) else { return }
767-
768726
self.eventDispatcher = TestEventDispatcher(eventFileName: uniqueFileName, removeDatafileObserver: false)
769727

770728
eventDispatcher.batchSize = 1000 // big, won't flush
@@ -801,9 +759,6 @@ extension EventDispatcherTests_Batch {
801759
}
802760

803761
func testEventsNotFlushedOnFirstDatafileLoad() {
804-
// this tests timer-based dispatch, available for iOS 10+
805-
guard #available(iOS 10.0, tvOS 10.0, *) else { return }
806-
807762
self.eventDispatcher = TestEventDispatcher(eventFileName: uniqueFileName, removeDatafileObserver: false)
808763

809764
eventDispatcher.batchSize = 1000 // big, won't flush
@@ -946,60 +901,11 @@ extension EventDispatcherTests_Batch {
946901

947902
}
948903

949-
// MARK: - iOS9 Devices
950-
951-
extension EventDispatcherTests_Batch {
952-
953-
func testFlushEventsForIOS9Only() {
954-
// this tests iOS9 (no-timer)
955-
if #available(iOS 10.0, tvOS 10.0, *) { return }
956-
957-
dispatchMultipleEvents([(kUrlA, batchEventA)])
958-
959-
eventDispatcher.dispatcher.sync {}
960-
961-
XCTAssertEqual(eventDispatcher.sendRequestedEvents.count, 1)
962-
let batch = eventDispatcher.sendRequestedEvents[0]
963-
let batchedEvents = try! JSONDecoder().decode(BatchEvent.self, from: batch.body)
964-
XCTAssertEqual(batch.url.absoluteString, kUrlA)
965-
XCTAssertEqual(batchedEvents.revision, kRevisionA)
966-
XCTAssertEqual(batchedEvents.accountID, kAccountId)
967-
XCTAssertEqual(batchedEvents.projectID, kProjectIdA)
968-
XCTAssertEqual(batchedEvents.clientVersion, kClientVersion)
969-
XCTAssertEqual(batchedEvents.clientName, kClientName)
970-
XCTAssertEqual(batchedEvents.anonymizeIP, kAnonymizeIP)
971-
XCTAssertEqual(batchedEvents.enrichDecisions, kEnrichDecision)
972-
XCTAssertEqual(batchedEvents.visitors[0], visitorA)
973-
XCTAssertEqual(eventDispatcher.dataStore.count, 0)
974-
}
975-
976-
func testFlushEventsForIOS9Only_ZeroInterval() {
977-
// this tests iOS9 (no-timer)
978-
if #available(iOS 10.0, tvOS 10.0, *) { return }
979-
980-
eventDispatcher.timerInterval = 0
981-
982-
dispatchMultipleEvents([(kUrlA, batchEventA)])
983-
eventDispatcher.dispatcher.sync {}
984-
985-
XCTAssertEqual(eventDispatcher.sendRequestedEvents.count, 1)
986-
let batch = eventDispatcher.sendRequestedEvents[0]
987-
let batchedEvents = try! JSONDecoder().decode(BatchEvent.self, from: batch.body)
988-
XCTAssertEqual(batch.url.absoluteString, kUrlA)
989-
XCTAssertEqual(batchedEvents.revision, kRevisionA)
990-
XCTAssertEqual(eventDispatcher.dataStore.count, 0)
991-
}
992-
993-
}
994-
995904
// MARK: - OptimizleyClient: Close()
996905

997906
extension EventDispatcherTests_Batch {
998907

999908
func testCloseForOptimizleyClient() {
1000-
// this tests timer-based dispatch, available for iOS 10+
1001-
guard #available(iOS 10.0, tvOS 10.0, *) else { return }
1002-
1003909
self.eventDispatcher = TestEventDispatcher(eventFileName: uniqueFileName, removeDatafileObserver: false)
1004910

1005911
eventDispatcher.batchSize = 1000 // big, won't flush

Tests/OptimizelyTests-Common/LoggerTests.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,7 @@ class LoggerTests: XCTestCase {
4343
let logger = DefaultLogger()
4444
logger.i("Log Message")
4545

46-
if #available(iOS 10.0, tvOS 10.0, *) {
47-
XCTAssertTrue(logger.osLogUsed)
48-
} else {
49-
XCTAssertFalse(logger.osLogUsed)
50-
}
46+
XCTAssertTrue(logger.osLogUsed)
5147
}
5248

5349
}

0 commit comments

Comments
 (0)