Skip to content

Commit

Permalink
GCDTimer updates
Browse files Browse the repository at this point in the history
  • Loading branch information
hemantasapkota committed Jan 8, 2023
1 parent 6a698cf commit bdce48d
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 47 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ DerivedData
# Carthage/Checkouts

Carthage/Build

# MacOSX
.DS_Store
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
objects = {

/* Begin PBXBuildFile section */
2E124AEF1FC5232A002AEA59 /* GCDTimer.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2E3CCE481B2038400085EA22 /* GCDTimer.framework */; };
2E3CCE221B2038180085EA22 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2E3CCE211B2038180085EA22 /* AppDelegate.swift */; };
2E3CCE241B2038180085EA22 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2E3CCE231B2038180085EA22 /* ViewController.swift */; };
2E3CCE271B2038180085EA22 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 2E3CCE251B2038180085EA22 /* Main.storyboard */; };
2E3CCE291B2038180085EA22 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 2E3CCE281B2038180085EA22 /* Images.xcassets */; };
2E3CCE2C1B2038180085EA22 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 2E3CCE2A1B2038180085EA22 /* LaunchScreen.xib */; };
2E3CCE381B2038190085EA22 /* GCDTimerExampleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2E3CCE371B2038190085EA22 /* GCDTimerExampleTests.swift */; };
2EB93D15296AB3950098012E /* GCDTimer.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2E3CCE481B2038400085EA22 /* GCDTimer.framework */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -59,7 +59,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
2E124AEF1FC5232A002AEA59 /* GCDTimer.framework in Frameworks */,
2EB93D15296AB3950098012E /* GCDTimer.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -210,6 +210,7 @@
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
English,
en,
Base,
);
Expand Down Expand Up @@ -406,7 +407,7 @@
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
};
name = Debug;
};
Expand All @@ -418,7 +419,7 @@
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
};
name = Release;
};
Expand All @@ -437,6 +438,7 @@
INFOPLIST_FILE = GCDTimerExampleTests/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/GCDTimerExample.app/GCDTimerExample";
};
name = Debug;
Expand All @@ -452,6 +454,7 @@
INFOPLIST_FILE = GCDTimerExampleTests/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/GCDTimerExample.app/GCDTimerExample";
};
name = Release;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
17 changes: 7 additions & 10 deletions Example/GCDTimerExample/GCDTimerExample/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// GCDTimerExample
//
// Created by Hemanta Sapkota on 4/06/2015.
// Copyright (c) 2015 Hemanta Sapkota. All rights reserved.
// Copyright (c) 2023 Hemanta Sapkota. All rights reserved.
//

import UIKit
Expand All @@ -14,33 +14,30 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
private func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
return true
}

func applicationWillResignActive(application: UIApplication) {
private func applicationWillResignActive(application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

func applicationDidEnterBackground(application: UIApplication) {
internal func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(application: UIApplication) {
internal func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(application: UIApplication) {
internal func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(application: UIApplication) {
internal func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


}

27 changes: 17 additions & 10 deletions Example/GCDTimerExample/GCDTimerExample/Base.lproj/LaunchScreen.xib
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="6214" systemVersion="14A314h" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES">
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="21507" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" colorMatched="YES">
<device id="retina6_12" orientation="portrait" appearance="light"/>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6207"/>
<capability name="Constraints with non-1.0 multipliers" minToolsVersion="5.1"/>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="21505"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
Expand All @@ -11,20 +13,20 @@
<rect key="frame" x="0.0" y="0.0" width="480" height="480"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text=" Copyright (c) 2015 Hemanta Sapkota. All rights reserved." textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" minimumFontSize="9" translatesAutoresizingMaskIntoConstraints="NO" id="8ie-xW-0ye">
<rect key="frame" x="20" y="439" width="441" height="21"/>
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" misplaced="YES" text=" Copyright (c) 2023 Hemanta Sapkota. All rights reserved." textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" minimumFontSize="9" translatesAutoresizingMaskIntoConstraints="NO" id="8ie-xW-0ye">
<rect key="frame" x="18" y="439" width="445" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<color key="textColor" systemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="GCDTimerExample" textAlignment="center" lineBreakMode="middleTruncation" baselineAdjustment="alignBaselines" minimumFontSize="18" translatesAutoresizingMaskIntoConstraints="NO" id="kId-c2-rCX">
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" misplaced="YES" text="GCDTimerExample" textAlignment="center" lineBreakMode="middleTruncation" baselineAdjustment="alignBaselines" minimumFontSize="18" translatesAutoresizingMaskIntoConstraints="NO" id="kId-c2-rCX">
<rect key="frame" x="20" y="140" width="441" height="43"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="36"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<color key="textColor" systemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="kId-c2-rCX" firstAttribute="centerY" secondItem="iN0-l3-epB" secondAttribute="bottom" multiplier="1/3" constant="1" id="5cJ-9S-tgC"/>
<constraint firstAttribute="centerX" secondItem="kId-c2-rCX" secondAttribute="centerX" id="Koa-jz-hwk"/>
Expand All @@ -38,4 +40,9 @@
<point key="canvasLocation" x="548" y="455"/>
</view>
</objects>
<resources>
<systemColor name="darkTextColor">
<color white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</systemColor>
</resources>
</document>
18 changes: 10 additions & 8 deletions Example/GCDTimerExample/GCDTimerExample/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10116" systemVersion="15E65" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="KcQ-id-wyb">
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="21507" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="KcQ-id-wyb">
<device id="retina6_12" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="21505"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--Navigation Controller-->
<scene sceneID="EoK-XQ-fjV">
<objects>
<navigationController id="KcQ-id-wyb" sceneMemberID="viewController">
<navigationBar key="navigationBar" contentMode="scaleToFill" id="Ut3-hY-03q">
<rect key="frame" x="0.0" y="0.0" width="320" height="44"/>
<rect key="frame" x="0.0" y="59" width="393" height="44"/>
<autoresizingMask key="autoresizingMask"/>
</navigationBar>
<connections>
Expand All @@ -30,19 +32,19 @@
<viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<rect key="frame" x="0.0" y="0.0" width="393" height="852"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="114" sectionHeaderHeight="22" sectionFooterHeight="22" translatesAutoresizingMaskIntoConstraints="NO" id="I9k-U8-HUC">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<rect key="frame" x="0.0" y="0.0" width="393" height="818"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<connections>
<outlet property="dataSource" destination="BYZ-38-t0r" id="gym-Qs-oPi"/>
<outlet property="delegate" destination="BYZ-38-t0r" id="xDy-1f-zWY"/>
</connections>
</tableView>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstAttribute="trailing" secondItem="I9k-U8-HUC" secondAttribute="trailing" id="7lt-Ge-dAd"/>
<constraint firstItem="I9k-U8-HUC" firstAttribute="top" secondItem="8bC-Xf-vdC" secondAttribute="top" id="Qnd-wS-jJI"/>
Expand Down
16 changes: 9 additions & 7 deletions Example/GCDTimerExample/GCDTimerExample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// GCDTimerExample
//
// Created by Hemanta Sapkota on 4/06/2015.
// Copyright (c) 2015 Hemanta Sapkota. All rights reserved.
// Copyright (c) 2023 Hemanta Sapkota. All rights reserved.
//

import UIKit
Expand All @@ -18,7 +18,7 @@ class ViewController: UIViewController, UITableViewDataSource, UITableViewDelega
override func viewDidLoad() {
super.viewDidLoad()

tableView.register(TableViewCell.self, forCellReuseIdentifier: "Cell")
tableView.register(TableViewCell.self, forCellReuseIdentifier: "Cell")

// Use Timer
let timer = GCDTimer(intervalInSecs: 30)
Expand All @@ -33,23 +33,25 @@ class ViewController: UIViewController, UITableViewDataSource, UITableViewDelega
// Load new HN stories
stories.removeAll(keepingCapacity: false)

let url = NSURL(string: "https://hacker-news.firebaseio.com/v0/newstories.json?print=pretty")
let url = NSURL(string: "https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty")

let task = URLSession.shared.dataTask(with: url! as URL) {(data, response, error) in

if data == nil {
print("\(error)")
print("\(String(describing: error))")
return
}

let json = (try! JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers)) as! NSArray
for var i in (0..<10) {
for i in (0..<10) {
let itemUrl = NSURL(string: "https://hacker-news.firebaseio.com/v0/item/\(json[i]).json?print=pretty")
print("\(itemUrl)")
let itemTask = URLSession.shared.dataTask(with: itemUrl! as URL) { (data, response, error) in
let json = (try! JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers)) as! NSDictionary
DispatchQueue.main.async {
let title = json["title"] as! String

print("\(String(describing: title))")

self.stories.append(title)
self.tableView.reloadData()
}
Expand All @@ -70,7 +72,7 @@ extension ViewController {

class TableViewCell : UITableViewCell {

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// GCDTimerExampleTests
//
// Created by Hemanta Sapkota on 4/06/2015.
// Copyright (c) 2015 Hemanta Sapkota. All rights reserved.
// Copyright (c) 2023 Hemanta Sapkota. All rights reserved.
//

import UIKit
Expand All @@ -28,7 +28,7 @@ class GCDTimerExampleTests: XCTestCase {

func testPerformanceExample() {
// This is an example of a performance test case.
self.measureBlock() {
self.measure() {
// Put the code you want to measure the time of here.
}
}
Expand Down
2 changes: 1 addition & 1 deletion GCDTimer/GCDTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// GCDTimer
//
// Created by Hemanta Sapkota on 4/06/2015.
// Copyright (c) 2015 Hemanta Sapkota. All rights reserved.
// Copyright (c) 2023 Hemanta Sapkota. All rights reserved.
//

#import <UIKit/UIKit.h>
Expand Down
8 changes: 4 additions & 4 deletions GCDTimer/GCDTimer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ open class GCDTimer {
}

public func reset(token: String) {
if let tokenIndex = _onceTracker.index(of: token) {
if let tokenIndex = _onceTracker.firstIndex(of: token) {
_onceTracker.remove(at: tokenIndex)
}
}
Expand Down Expand Up @@ -60,9 +60,9 @@ open class GCDTimer {
set {
event = newValue

self.timerSource.scheduleRepeating(deadline: DispatchTime.now(), interval: DispatchTimeInterval.seconds(Int(interval)))
self.timerSource.setEventHandler { [weak self] in
self?.event()
self.timerSource.schedule(deadline: DispatchTime.now(), repeating: DispatchTimeInterval.seconds(Int(interval)))
self.timerSource.setEventHandler {
self.event()
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ The example shows how to use GCDTimer to periodically poll newly submitted items
### License ###
The MIT License (MIT)

Copyright (c) 2022 Hemanta Sapkota
Copyright (c) 2023 Hemanta Sapkota

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down

0 comments on commit bdce48d

Please sign in to comment.