From 161b3dd054a15924d2eef711c136f1a224c53771 Mon Sep 17 00:00:00 2001 From: Leandro Alonso Date: Tue, 9 Feb 2021 18:06:24 -0300 Subject: [PATCH] Remove scheduled refresh --- Automattic-Tracks-iOS/ABTesting/ExPlat.swift | 38 ------------------- Automattic-Tracks-iOS/TracksConstants.m | 2 +- .../Tests/ABTesting/ExPlatTests.swift | 19 ---------- 3 files changed, 1 insertion(+), 58 deletions(-) diff --git a/Automattic-Tracks-iOS/ABTesting/ExPlat.swift b/Automattic-Tracks-iOS/ABTesting/ExPlat.swift index 0ebafe3c..c0a8e4f6 100644 --- a/Automattic-Tracks-iOS/ABTesting/ExPlat.swift +++ b/Automattic-Tracks-iOS/ABTesting/ExPlat.swift @@ -21,8 +21,6 @@ import Cocoa return ttlDate.timeIntervalSinceReferenceDate - Date().timeIntervalSinceReferenceDate } - private(set) var scheduledTimer: Timer? - public init(configuration: ExPlatConfiguration, service: ExPlatService? = nil) { self.service = service ?? ExPlatService(configuration: configuration) @@ -50,7 +48,6 @@ import Cocoa public func refreshIfNeeded(completion: (() -> Void)? = nil) { guard ttl > 0 else { completion?() - scheduleRefresh() return } @@ -73,7 +70,6 @@ import Cocoa var ttlDate = Date() ttlDate.addTimeInterval(TimeInterval(assignments.ttl)) UserDefaults.standard.setValue(ttlDate, forKey: self.ttlDateKey) - self.scheduleRefresh() completion?() } @@ -95,30 +91,6 @@ import Cocoa } } - private func scheduleRefresh() { - if ttl > 0 { - scheduledTimer?.invalidate() - - /// Schedule the refresh on a background thread - DispatchQueue.global(qos: .background).async { [weak self] in - guard let `self` = self else { - return - } - - self.scheduledTimer = Timer.scheduledTimer(withTimeInterval: self.ttl, repeats: true) { [weak self] timer in - self?.refresh() - timer.invalidate() - } - - RunLoop.current.run() - } - - - } else { - refresh() - } - } - /// Check if the app is entering background and/or foreground /// and start/stop the timers /// @@ -126,10 +98,8 @@ import Cocoa let notificationCenter = NotificationCenter.default #if os(iOS) || os(watchOS) || os(tvOS) - notificationCenter.addObserver(self, selector: #selector(applicationDidEnterBackground), name: UIApplication.didEnterBackgroundNotification, object: nil) notificationCenter.addObserver(self, selector: #selector(applicationWillEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil) #elseif os(macOS) - notificationCenter.addObserver(self, selector: #selector(applicationDidEnterBackground), name: NSApplication.didResignActiveNotification, object: nil) notificationCenter.addObserver(self, selector: #selector(applicationWillEnterForeground), name: NSApplication.willBecomeActiveNotification, object: nil) #endif } @@ -138,20 +108,12 @@ import Cocoa let notificationCenter = NotificationCenter.default #if os(iOS) || os(watchOS) || os(tvOS) - notificationCenter.removeObserver(self, name: UIApplication.didEnterBackgroundNotification, object: nil) notificationCenter.removeObserver(self, name: UIApplication.willEnterForegroundNotification, object: nil) #elseif os(macOS) - notificationCenter.removeObserver(self, name: NSApplication.didResignActiveNotification, object: nil) notificationCenter.removeObserver(self, name: NSApplication.willBecomeActiveNotification, object: nil) #endif } - /// When the app goes to background stop the timer - /// - @objc private func applicationDidEnterBackground() { - scheduledTimer?.invalidate() - } - /// When the app enter foreground refresh the assignments or /// start the timer /// diff --git a/Automattic-Tracks-iOS/TracksConstants.m b/Automattic-Tracks-iOS/TracksConstants.m index b6a9de23..3827a815 100644 --- a/Automattic-Tracks-iOS/TracksConstants.m +++ b/Automattic-Tracks-iOS/TracksConstants.m @@ -1,4 +1,4 @@ #import "TracksConstants.h" NSString *const TracksErrorDomain = @"TracksErrorDomain"; -NSString *const TracksLibraryVersion = @"0.8.1"; +NSString *const TracksLibraryVersion = @"0.8.2"; diff --git a/Automattic-Tracks-iOSTests/Tests/ABTesting/ExPlatTests.swift b/Automattic-Tracks-iOSTests/Tests/ABTesting/ExPlatTests.swift index 7cffbcdc..dcc8eb21 100644 --- a/Automattic-Tracks-iOSTests/Tests/ABTesting/ExPlatTests.swift +++ b/Automattic-Tracks-iOSTests/Tests/ABTesting/ExPlatTests.swift @@ -46,25 +46,6 @@ class ExPlatTests: XCTestCase { wait(for: [expectation], timeout: 2.0) } - - // Schedule a timer to automatically refresh - // - func testScheduleRefresh() { - let expectation = XCTestExpectation(description: "Automatically refresh") - let serviceMock = ExPlatServiceMock() - let abTesting = ExPlat(configuration: exPlatTestConfiguration, service: serviceMock) - abTesting.refresh { - - DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { - XCTAssertTrue(abTesting.scheduledTimer!.isValid) - XCTAssertEqual(round(abTesting.scheduledTimer!.timeInterval), 60) - expectation.fulfill() - } - - } - - wait(for: [expectation], timeout: 2.0) - } } private class ExPlatServiceMock: ExPlatService {