Skip to content

Commit 673e421

Browse files
authored
Merge pull request #5 from quanvo87/0.0.3
re architect, update README, add test, add travis
2 parents f596e20 + 104847c commit 673e421

File tree

7 files changed

+277
-103
lines changed

7 files changed

+277
-103
lines changed

.travis.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
osx_image: xcode9.2
2+
language: objective-c
3+
xcode_project: GroupWork.xcodeproj
4+
5+
env:
6+
- UDID="7A5E86EB-E32C-475F-8FF5-10AA330C1D34", IOS_DEVICE="iPhone 7 Plus (11.1)"
7+
- UDID="15E2107D-EAD2-4E1F-8BC3-59E58E750FB1", IOS_DEVICE="iPhone 7 (10.0)"
8+
- UDID="9AB3D590-C748-4E17-8E35-67B298E973F7", IOS_DEVICE="iPhone 6 Plus (9.0)"
9+
- UDID="80BE80F4-C243-43F0-BD95-5CB085DAD04F", IOS_DEVICE="iPhone 6 (8.1)"
10+
11+
script:
12+
- open -a "simulator" --args -CurrentDeviceUDID $UDID
13+
- xcodebuild clean test -project GroupWork.xcodeproj -scheme GroupWork -configuration Debug -sdk iphonesimulator -destination "platform=iOS Simulator,id=$UDID" CODE_SIGN_IDENTITY=\"\" CODE_SIGNING_REQUIRED=NO ONLY_ACTIVE_ARCH=NO -quiet

GroupWork.podspec

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
Pod::Spec.new do |s|
22
s.name = "GroupWork"
3-
s.version = "0.0.1"
4-
s.summary = "Swift module that helps with running multiple, simultaneous, asynchronous tasks in a clean way."
5-
s.description = "Swift module that helps with running multiple, simultaneous, asynchronous tasks in a clean way. Better description inc."
3+
s.version = "0.0.3"
4+
s.summary = "Swift module that helps with running multiple, concurrent, asynchronous tasks in a clean way."
5+
s.description = "Swift module that helps with running multiple, concurrent, asynchronous tasks in a clean way. Better description inc."
66
s.homepage = "https://github.com/quanvo87/GroupWork"
77
s.license = { :type => "MIT", :file => "LICENSE" }
88
s.author = { "Quan Vo" => "qvo1987@gmail.com",
99
"Wilson Ding" => "hello@wilsonding.com" }
1010
s.platform = :ios, "8.0"
11-
s.source = { :git => "https://github.com/quanvo87/GroupWork.git", :tag => "0.0.1" }
11+
s.source = { :git => "https://github.com/quanvo87/GroupWork.git", :tag => "0.0.3" }
1212
s.source_files = "GroupWork", "GroupWork/**/*.{h,m,swift}"
1313
s.exclude_files = "Classes/Exclude"
1414
end
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Scheme
3+
LastUpgradeVersion = "0920"
4+
version = "1.3">
5+
<BuildAction
6+
parallelizeBuildables = "YES"
7+
buildImplicitDependencies = "YES">
8+
<BuildActionEntries>
9+
<BuildActionEntry
10+
buildForTesting = "YES"
11+
buildForRunning = "YES"
12+
buildForProfiling = "YES"
13+
buildForArchiving = "YES"
14+
buildForAnalyzing = "YES">
15+
<BuildableReference
16+
BuildableIdentifier = "primary"
17+
BlueprintIdentifier = "591BDE961FE86B27000A1EC8"
18+
BuildableName = "GroupWork.framework"
19+
BlueprintName = "GroupWork"
20+
ReferencedContainer = "container:GroupWork.xcodeproj">
21+
</BuildableReference>
22+
</BuildActionEntry>
23+
</BuildActionEntries>
24+
</BuildAction>
25+
<TestAction
26+
buildConfiguration = "Debug"
27+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
28+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
29+
language = ""
30+
shouldUseLaunchSchemeArgsEnv = "YES"
31+
codeCoverageEnabled = "YES">
32+
<Testables>
33+
<TestableReference
34+
skipped = "NO">
35+
<BuildableReference
36+
BuildableIdentifier = "primary"
37+
BlueprintIdentifier = "591BDE9F1FE86B27000A1EC8"
38+
BuildableName = "GroupWorkTests.xctest"
39+
BlueprintName = "GroupWorkTests"
40+
ReferencedContainer = "container:GroupWork.xcodeproj">
41+
</BuildableReference>
42+
</TestableReference>
43+
</Testables>
44+
<MacroExpansion>
45+
<BuildableReference
46+
BuildableIdentifier = "primary"
47+
BlueprintIdentifier = "591BDE961FE86B27000A1EC8"
48+
BuildableName = "GroupWork.framework"
49+
BlueprintName = "GroupWork"
50+
ReferencedContainer = "container:GroupWork.xcodeproj">
51+
</BuildableReference>
52+
</MacroExpansion>
53+
<AdditionalOptions>
54+
</AdditionalOptions>
55+
</TestAction>
56+
<LaunchAction
57+
buildConfiguration = "Debug"
58+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
59+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
60+
language = ""
61+
launchStyle = "0"
62+
useCustomWorkingDirectory = "NO"
63+
ignoresPersistentStateOnLaunch = "NO"
64+
debugDocumentVersioning = "YES"
65+
debugServiceExtension = "internal"
66+
allowLocationSimulation = "YES">
67+
<MacroExpansion>
68+
<BuildableReference
69+
BuildableIdentifier = "primary"
70+
BlueprintIdentifier = "591BDE961FE86B27000A1EC8"
71+
BuildableName = "GroupWork.framework"
72+
BlueprintName = "GroupWork"
73+
ReferencedContainer = "container:GroupWork.xcodeproj">
74+
</BuildableReference>
75+
</MacroExpansion>
76+
<AdditionalOptions>
77+
</AdditionalOptions>
78+
</LaunchAction>
79+
<ProfileAction
80+
buildConfiguration = "Release"
81+
shouldUseLaunchSchemeArgsEnv = "YES"
82+
savedToolIdentifier = ""
83+
useCustomWorkingDirectory = "NO"
84+
debugDocumentVersioning = "YES">
85+
<MacroExpansion>
86+
<BuildableReference
87+
BuildableIdentifier = "primary"
88+
BlueprintIdentifier = "591BDE961FE86B27000A1EC8"
89+
BuildableName = "GroupWork.framework"
90+
BlueprintName = "GroupWork"
91+
ReferencedContainer = "container:GroupWork.xcodeproj">
92+
</BuildableReference>
93+
</MacroExpansion>
94+
</ProfileAction>
95+
<AnalyzeAction
96+
buildConfiguration = "Debug">
97+
</AnalyzeAction>
98+
<ArchiveAction
99+
buildConfiguration = "Release"
100+
revealArchiveInOrganizer = "YES">
101+
</ArchiveAction>
102+
</Scheme>

GroupWork.xcodeproj/xcuserdata/quanvo.xcuserdatad/xcschemes/xcschememanagement.plist

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,24 @@
44
<dict>
55
<key>SchemeUserState</key>
66
<dict>
7-
<key>GroupWork.xcscheme</key>
7+
<key>GroupWork.xcscheme_^#shared#^_</key>
88
<dict>
99
<key>orderHint</key>
1010
<integer>0</integer>
1111
</dict>
1212
</dict>
13+
<key>SuppressBuildableAutocreation</key>
14+
<dict>
15+
<key>591BDE961FE86B27000A1EC8</key>
16+
<dict>
17+
<key>primary</key>
18+
<true/>
19+
</dict>
20+
<key>591BDE9F1FE86B27000A1EC8</key>
21+
<dict>
22+
<key>primary</key>
23+
<true/>
24+
</dict>
25+
</dict>
1326
</dict>
1427
</plist>

GroupWork/GroupWork.swift

Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,34 @@
11
/// Used to perform groups of work and keep track of the results.
2-
public typealias Key = String
2+
public class GroupWork {
3+
private let group = DispatchGroup()
34

4-
struct GroupWorkDictionary {
5-
static var dictionary: [Key: (group: DispatchGroup, result: Bool)] = {
6-
return [Key: (group: DispatchGroup, result: Bool)]()
7-
}()
8-
}
9-
10-
/// Used to perform groups of work and keep track of the results.
11-
public extension Key {
12-
13-
/// The current result of the `Key`'s work items. Each time a `Key`'s work
14-
/// item completes, it `&&`'s its result with this property. `result` begins
15-
/// as `true` for new `Key`s. Returns false if called on an invalid `Key`.
16-
public var result: Bool {
17-
return GroupWorkDictionary.dictionary[self]?.result ?? false
18-
}
5+
/// The current result of the work items.
6+
public var result = true
197

20-
/// Returns a new `Key`.
21-
public static func make() -> Key {
22-
let key = UUID().uuidString
23-
GroupWorkDictionary.dictionary[key] = (DispatchGroup(), true)
24-
return key
25-
}
8+
/// Initializes a `GroupWork`.
9+
public init() {}
2610

27-
/// Indicates that some work has just started for the `Key`. A call to
28-
/// `start()` must be proceeded by a call to `finish()` eventually.
11+
/// Should be called before some asynchronous work begins.
2912
public func start() {
30-
GroupWorkDictionary.dictionary[self]?.group.enter()
13+
group.enter()
3114
}
3215

33-
/// Indicates that some work has just finished for the `Key`. A call to
34-
/// `finish()` must proceed a call to `start()`.
16+
/// Should be called after some asynchronous work is done.
3517
///
3618
/// - parameter result: The `Bool` result of the work that just finished.
37-
/// This will get `&&`'d to the current `result` for the `Key`.
38-
public func finish(withResult result: Bool = true) {
39-
GroupWorkDictionary.dictionary[self]?.result = self.result && result
40-
GroupWorkDictionary.dictionary[self]?.group.leave()
19+
/// This will get `&&`'d with the current `result`.
20+
public func finish(withResult result: Bool) {
21+
let oldResult = self.result
22+
self.result = oldResult && result
23+
group.leave()
4124
}
4225

43-
/// Used to pass in a function that should be called when ALL work for a
44-
/// `Key` is done.
26+
/// Handles what to do once all work is done.
4527
///
46-
/// - parameter completion: The function to be executed when ALL work for a
47-
/// `Key` is done. Typically, `remove()` should be
48-
/// called somewhere in the body of this function.
28+
/// - parameter completion: The function to be executed when ALL work is done.
4929
public func allDone(completion: @escaping () -> Void) {
50-
GroupWorkDictionary.dictionary[self]?.group.notify(queue: .main) {
30+
group.notify(queue: .main) {
5131
completion()
5232
}
5333
}
54-
55-
/// Removes a `Key`'s data. This should be called to clean up resources when
56-
/// done with a `Key`. A `Key` should not be used after this function is called.
57-
public func remove() {
58-
GroupWorkDictionary.dictionary.removeValue(forKey: self)
59-
}
6034
}

GroupWorkTests/GroupWorkTests.swift

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,44 @@
1-
//
2-
// GroupWorkTests.swift
3-
// GroupWorkTests
4-
//
5-
// Created by Quan Vo on 12/18/17.
6-
// Copyright © 2017 Quan Vo. All rights reserved.
7-
//
8-
91
import XCTest
10-
@testable import GroupWork
2+
import GroupWork
113

124
class GroupWorkTests: XCTestCase {
13-
14-
override func setUp() {
15-
super.setUp()
16-
// Put setup code here. This method is called before the invocation of each test method in the class.
17-
}
18-
19-
override func tearDown() {
20-
// Put teardown code here. This method is called after the invocation of each test method in the class.
21-
super.tearDown()
5+
func test() {
6+
let expectation = self.expectation(description: #function)
7+
8+
let work = GroupWork()
9+
10+
work.google()
11+
work.yahoo()
12+
13+
work.allDone {
14+
print(work.result)
15+
expectation.fulfill()
16+
}
17+
18+
waitForExpectations(timeout: 10)
2219
}
23-
24-
func testExample() {
25-
// This is an example of a functional test case.
26-
// Use XCTAssert and related functions to verify your tests produce the correct results.
20+
}
21+
22+
extension GroupWork {
23+
func google() {
24+
start()
25+
URLSession.shared.dataTask(with: URL(string: "https://google.com")!) { (_, response, _) in
26+
if let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 {
27+
self.finish(withResult: true)
28+
} else {
29+
self.finish(withResult: false)
30+
}
31+
}.resume()
2732
}
28-
29-
func testPerformanceExample() {
30-
// This is an example of a performance test case.
31-
self.measure {
32-
// Put the code you want to measure the time of here.
33-
}
33+
34+
func yahoo() {
35+
start()
36+
URLSession.shared.dataTask(with: URL(string: "https://yahoo.com")!) { (_, response, _) in
37+
if let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 {
38+
self.finish(withResult: true)
39+
} else {
40+
self.finish(withResult: false)
41+
}
42+
}.resume()
3443
}
35-
3644
}

0 commit comments

Comments
 (0)