Skip to content

Commit f66b951

Browse files
committed
Test timer and test errors that it can cause
1 parent 707c934 commit f66b951

File tree

4 files changed

+110
-93
lines changed

4 files changed

+110
-93
lines changed

Efficient Timer.xcodeproj/project.pbxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@
281281
CODE_SIGN_STYLE = Automatic;
282282
DEVELOPMENT_TEAM = 299A72ZY42;
283283
INFOPLIST_FILE = "Efficient Timer/Info.plist";
284+
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
284285
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
285286
PRODUCT_BUNDLE_IDENTIFIER = "com.vtinfo.jayant.arora.Efficient-Timer";
286287
PRODUCT_NAME = "$(TARGET_NAME)";
@@ -296,6 +297,7 @@
296297
CODE_SIGN_STYLE = Automatic;
297298
DEVELOPMENT_TEAM = 299A72ZY42;
298299
INFOPLIST_FILE = "Efficient Timer/Info.plist";
300+
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
299301
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
300302
PRODUCT_BUNDLE_IDENTIFIER = "com.vtinfo.jayant.arora.Efficient-Timer";
301303
PRODUCT_NAME = "$(TARGET_NAME)";
Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,55 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13122.16" systemVersion="17A277" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13771" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
3+
<device id="retina4_7" orientation="portrait">
4+
<adaptation id="fullscreen"/>
5+
</device>
36
<dependencies>
4-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13104.12"/>
7+
<deployment identifier="iOS"/>
8+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13772"/>
59
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
610
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
711
</dependencies>
812
<scenes>
913
<!--View Controller-->
1014
<scene sceneID="tne-QT-ifu">
1115
<objects>
12-
<viewController id="BYZ-38-t0r" customClass="ViewController" customModuleProvider="target" sceneMemberID="viewController">
16+
<viewController id="BYZ-38-t0r" customClass="ViewController" customModule="Efficient_Timer" customModuleProvider="target" sceneMemberID="viewController">
1317
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
1418
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
1519
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
20+
<subviews>
21+
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="aQx-0A-4JB">
22+
<rect key="frame" x="166" y="400" width="42" height="21"/>
23+
<fontDescription key="fontDescription" type="system" pointSize="17"/>
24+
<nil key="textColor"/>
25+
<nil key="highlightedColor"/>
26+
</label>
27+
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="aCg-YF-1WE">
28+
<rect key="frame" x="166" y="209" width="42" height="20.5"/>
29+
<fontDescription key="fontDescription" type="system" pointSize="17"/>
30+
<nil key="textColor"/>
31+
<nil key="highlightedColor"/>
32+
</label>
33+
</subviews>
1634
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
35+
<constraints>
36+
<constraint firstItem="aQx-0A-4JB" firstAttribute="leading" secondItem="6Tk-OE-BBY" secondAttribute="leading" constant="166" id="5O8-oH-kf3"/>
37+
<constraint firstItem="6Tk-OE-BBY" firstAttribute="trailing" secondItem="aQx-0A-4JB" secondAttribute="trailing" constant="167" id="Bam-Q7-wQ9"/>
38+
<constraint firstItem="aCg-YF-1WE" firstAttribute="centerY" secondItem="8bC-Xf-vdC" secondAttribute="centerY" constant="-114.25" id="Tph-fY-BLs"/>
39+
<constraint firstItem="6Tk-OE-BBY" firstAttribute="bottom" secondItem="aQx-0A-4JB" secondAttribute="bottom" constant="246" id="bc0-M3-5Ir"/>
40+
<constraint firstItem="aQx-0A-4JB" firstAttribute="top" secondItem="aCg-YF-1WE" secondAttribute="bottom" constant="170.5" id="hSf-Ou-Oe2"/>
41+
<constraint firstItem="aCg-YF-1WE" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="rpk-Xm-HLr"/>
42+
</constraints>
1743
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
1844
</view>
45+
<connections>
46+
<outlet property="outputLabel" destination="aCg-YF-1WE" id="Op3-7p-CkF"/>
47+
<outlet property="outputLabel2" destination="aQx-0A-4JB" id="Y6b-HD-rD2"/>
48+
</connections>
1949
</viewController>
2050
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
2151
</objects>
52+
<point key="canvasLocation" x="136.80000000000001" y="132.68365817091455"/>
2253
</scene>
2354
</scenes>
2455
</document>

Efficient Timer/JATimer.swift

Lines changed: 50 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,20 @@
88

99
import Foundation
1010

11-
enum CurrentState {
11+
@objc
12+
public enum CurrentState: Int {
1213
case running
1314
case suspended
1415
}
1516

1617
@objc
1718
public class JATimer: NSObject {
1819

20+
// MARK: Private Properties
21+
1922
private var name: String = ""
2023
private var initialized = false
21-
private var running: CurrentState = .suspended
24+
private var timerState: CurrentState = .suspended
2225
private var queue: DispatchQueue?
2326
private var timerForQueue: Int = 0
2427
private var block: () -> Void = { }
@@ -27,6 +30,8 @@ public class JATimer: NSObject {
2730
super.init()
2831
}
2932

33+
// MARK: Public Properties
34+
3035
@objc
3136
public convenience init(name: String, interval: Int, completion: (@escaping () -> Void)) {
3237
self.init()
@@ -42,104 +47,62 @@ public class JATimer: NSObject {
4247
return queue
4348
}
4449

50+
deinit {
51+
queue = nil
52+
}
53+
54+
/// This function needs to be called to initialize timer after instantiation
4555
@objc
4656
public func inititalize() {
4757
if let _ = self.getQueue() {
4858
timedBlock()
4959
}
5060
initialized = true
61+
timerState = .running
62+
}
63+
64+
@objc
65+
public func isInitialized() -> Bool{
66+
return initialized
67+
}
68+
69+
@objc
70+
public func getCurrentState() -> CurrentState {
71+
return timerState
72+
}
73+
74+
@objc
75+
public func pause() {
76+
guard initialized == true else {
77+
fatalError("Timer needs to be initialized before you can pause it.")
78+
}
79+
guard timerState == .running else {
80+
fatalError("Attempt to pause timer when it is already paused.")
81+
}
82+
queue?.suspend()
83+
timerState = .suspended
84+
}
85+
86+
@objc
87+
public func resume() {
88+
guard initialized == true else {
89+
fatalError("Timer needs to be initialized before you can reusme it.")
90+
}
91+
guard timerState == .suspended else {
92+
fatalError("Attempt to resume timer when it is already running.")
93+
}
94+
queue?.resume()
95+
timerState = .running
5196
}
5297

98+
// MARK: Private Properties
99+
53100
private func timedBlock() {
54101
queue?.asyncAfter(deadline: .now() + .seconds(timerForQueue)) {
55102
self.block()
56103
self.timedBlock()
57104
}
58-
print("Test: seconds: \(timerForQueue)")
105+
print("Timed Block set up")
59106
}
60107

61108
}
62-
63-
//
64-
//import Foundation
65-
//
66-
//@objc
67-
//public class LocationManagerScheduler: NSObject {
68-
// private static var frequentCounter = 0
69-
// private static var occasionalCounter = 0
70-
// private static let frequentInterval = DispatchQueue(label: "com.vtinfo.locationScheduler.Frequent")
71-
// private static let occasionalInterval = DispatchQueue(label: "com.vtinfo.locationScheduler.Occasional")
72-
//
73-
// @objc
74-
// static var currentInterval: locationUpdateInterval = .occasional {
75-
// didSet {
76-
// guard currentInterval != oldValue else { return }
77-
//
78-
// switch currentInterval {
79-
// case .frequent:
80-
// switchQueue()
81-
// case .occasional:
82-
// switchQueue()
83-
// case .none:
84-
// suspendAllQueues()
85-
// }
86-
// }
87-
// }
88-
//
89-
// private class func switchQueue() {
90-
// if occasionalCounter == 0 {
91-
// occasionalInterval.suspend()
92-
// occasionalCounter -= 1
93-
//
94-
// frequentInterval.resume()
95-
// frequentCounter = 1
96-
// }
97-
//
98-
// if frequentCounter == 0 {
99-
// frequentInterval.suspend()
100-
// frequentCounter -= 1
101-
//
102-
// occasionalInterval.resume()
103-
// occasionalCounter = 1
104-
// }
105-
// }
106-
//
107-
// private class func suspendAllQueues() {
108-
// if occasionalCounter == 0 {
109-
// occasionalInterval.suspend()
110-
// occasionalCounter -= 1
111-
// }
112-
//
113-
// if frequentCounter == 0 {
114-
// frequentInterval.suspend()
115-
// frequentCounter -= 1
116-
// }
117-
// }
118-
//
119-
// private func frequentIntervalHandler() {
120-
// print("[LOC TEST] Frequent running")
121-
// LocationManagerScheduler.frequentInterval.asyncAfter(deadline: .now() 10) {
122-
// self.frequentIntervalHandler()
123-
// }
124-
// }
125-
//
126-
// private func occassionalIntervalHandler() {
127-
// print("[LOC TEST] Occassional running")
128-
// LocationManagerScheduler.occasionalInterval.asyncAfter(deadline: .now() 30) {
129-
// self.occassionalIntervalHandler()
130-
// }
131-
// }
132-
//
133-
// public override init() {
134-
// LocationManagerScheduler.frequentInterval.asyncAfter(deadline: .now() 10) {
135-
// self.frequentIntervalHandler()
136-
// }
137-
//
138-
// LocationManagerScheduler.occasionalInterval.asyncAfter(deadline: .now() 30) {
139-
// self.occassionalIntervalHandler()
140-
// }
141-
//
142-
//
143-
// }
144-
//}
145-

Efficient Timer/ViewController.swift

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,37 @@ import UIKit
1010

1111
class ViewController: UIViewController {
1212

13+
// MARK: Variables
14+
var timer: JATimer!
15+
var timer2: JATimer!
16+
17+
// MARK: Outlets
18+
@IBOutlet weak var outputLabel: UILabel!
19+
@IBOutlet weak var outputLabel2: UILabel!
20+
21+
// MARK: View Lifecycle
1322
override func viewDidLoad() {
1423
super.viewDidLoad()
15-
// Do any additional setup after loading the view, typically from a nib.
24+
timer = JATimer(name: "Timer 1", interval: 10, completion: {
25+
self.outputLabel.text = "Timer 1 ran" // not the best approach. Should not be used from background
26+
// thread
27+
print("Timer 1 ran")
28+
})
29+
30+
timer.inititalize()
31+
32+
timer2 = JATimer(name: "Timer 2", interval: 15, completion: {
33+
self.outputLabel2.text = "Timer 2 ran" // not the best approach. Should not be used from background
34+
// thread
35+
print("Timer 2 ran")
36+
})
37+
38+
timer2.inititalize()
1639
}
1740

1841
override func didReceiveMemoryWarning() {
1942
super.didReceiveMemoryWarning()
2043
// Dispose of any resources that can be recreated.
2144
}
22-
23-
2445
}
2546

0 commit comments

Comments
 (0)