Skip to content

Commit

Permalink
Replacing NSOperating with GCD
Browse files Browse the repository at this point in the history
  • Loading branch information
marmelroy committed Dec 3, 2016
1 parent 253ee79 commit 8012d8e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 264 deletions.
10 changes: 0 additions & 10 deletions PhoneNumberKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
34566C9A1BC112C500715E6B /* RegexManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34566C991BC112C500715E6B /* RegexManager.swift */; };
346922671BC01DCC0023482F /* MetadataManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 346922661BC01DCC0023482F /* MetadataManager.swift */; };
346922691BC023A60023482F /* PhoneNumberKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 346922681BC023A60023482F /* PhoneNumberKit.swift */; };
346DEC0B1BE4B2F900A63274 /* ParseOperation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 346DEC0A1BE4B2F900A63274 /* ParseOperation.swift */; };
346EF14E1C69C688008C7306 /* PartialFormatterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 346EF14D1C69C688008C7306 /* PartialFormatterTests.swift */; };
347209901BB80A69004DE6DA /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3472098F1BB80A69004DE6DA /* Foundation.framework */; };
34776AA81BE2BF1100400790 /* PhoneNumberKitParsingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 34776AA71BE2BF1100400790 /* PhoneNumberKitParsingTests.swift */; };
Expand All @@ -39,7 +38,6 @@
C6DF6C581D1B09DD00259F4B /* MetadataManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 346922661BC01DCC0023482F /* MetadataManager.swift */; };
C6DF6C591D1B09DD00259F4B /* MetadataTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 342548EF1BE7EED500FBE524 /* MetadataTypes.swift */; };
C6DF6C5A1D1B09DD00259F4B /* ParseManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3422D9B91BE6A2D500867D02 /* ParseManager.swift */; };
C6DF6C5B1D1B09DD00259F4B /* ParseOperation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 346DEC0A1BE4B2F900A63274 /* ParseOperation.swift */; };
C6DF6C5C1D1B09DD00259F4B /* PhoneNumber.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3424187F1BB705B500EE70E7 /* PhoneNumber.swift */; };
C6DF6C5D1D1B09DD00259F4B /* PhoneNumberKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 346922681BC023A60023482F /* PhoneNumberKit.swift */; };
C6DF6C5E1D1B09DD00259F4B /* PhoneNumberParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 342418811BB70F5200EE70E7 /* PhoneNumberParser.swift */; };
Expand All @@ -52,7 +50,6 @@
C6DF6CB61D1B159A00259F4B /* MetadataManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 346922661BC01DCC0023482F /* MetadataManager.swift */; };
C6DF6CB71D1B159A00259F4B /* MetadataTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 342548EF1BE7EED500FBE524 /* MetadataTypes.swift */; };
C6DF6CB81D1B159A00259F4B /* ParseManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3422D9B91BE6A2D500867D02 /* ParseManager.swift */; };
C6DF6CB91D1B159A00259F4B /* ParseOperation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 346DEC0A1BE4B2F900A63274 /* ParseOperation.swift */; };
C6DF6CBA1D1B159A00259F4B /* PhoneNumber.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3424187F1BB705B500EE70E7 /* PhoneNumber.swift */; };
C6DF6CBB1D1B159A00259F4B /* PhoneNumberKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 346922681BC023A60023482F /* PhoneNumberKit.swift */; };
C6DF6CBC1D1B159A00259F4B /* PhoneNumberParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 342418811BB70F5200EE70E7 /* PhoneNumberParser.swift */; };
Expand All @@ -66,7 +63,6 @@
C6DF6CD91D1B18D800259F4B /* MetadataManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 346922661BC01DCC0023482F /* MetadataManager.swift */; };
C6DF6CDA1D1B18D800259F4B /* MetadataTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 342548EF1BE7EED500FBE524 /* MetadataTypes.swift */; };
C6DF6CDB1D1B18D800259F4B /* ParseManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3422D9B91BE6A2D500867D02 /* ParseManager.swift */; };
C6DF6CDC1D1B18D800259F4B /* ParseOperation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 346DEC0A1BE4B2F900A63274 /* ParseOperation.swift */; };
C6DF6CDD1D1B18D800259F4B /* PhoneNumber.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3424187F1BB705B500EE70E7 /* PhoneNumber.swift */; };
C6DF6CDE1D1B18D800259F4B /* PhoneNumberKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 346922681BC023A60023482F /* PhoneNumberKit.swift */; };
C6DF6CDF1D1B18D800259F4B /* PhoneNumberParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 342418811BB70F5200EE70E7 /* PhoneNumberParser.swift */; };
Expand Down Expand Up @@ -102,7 +98,6 @@
34566C991BC112C500715E6B /* RegexManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RegexManager.swift; sourceTree = "<group>"; };
346922661BC01DCC0023482F /* MetadataManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MetadataManager.swift; sourceTree = "<group>"; };
346922681BC023A60023482F /* PhoneNumberKit.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PhoneNumberKit.swift; sourceTree = "<group>"; };
346DEC0A1BE4B2F900A63274 /* ParseOperation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ParseOperation.swift; sourceTree = "<group>"; };
346EF14D1C69C688008C7306 /* PartialFormatterTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PartialFormatterTests.swift; sourceTree = "<group>"; };
3472098F1BB80A69004DE6DA /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
34776AA71BE2BF1100400790 /* PhoneNumberKitParsingTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PhoneNumberKitParsingTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -209,7 +204,6 @@
3420CF5D1BE8959F00FAE34F /* Formatter.swift */,
343B850A1C62A25600918E46 /* PartialFormatter.swift */,
343B850B1C62A25600918E46 /* TextField.swift */,
346DEC0A1BE4B2F900A63274 /* ParseOperation.swift */,
342418811BB70F5200EE70E7 /* PhoneNumberParser.swift */,
11C2EF381D62BC3200052D44 /* NSRegularExpression+Swift.swift */,
3435CC8F1BBFF66F003F953B /* Resources */,
Expand Down Expand Up @@ -472,7 +466,6 @@
343B850C1C62A25600918E46 /* PartialFormatter.swift in Sources */,
3422D9BA1BE6A2D500867D02 /* ParseManager.swift in Sources */,
34566C9A1BC112C500715E6B /* RegexManager.swift in Sources */,
346DEC0B1BE4B2F900A63274 /* ParseOperation.swift in Sources */,
342418801BB705B500EE70E7 /* PhoneNumber.swift in Sources */,
34AA66021BDD160B00467912 /* Constants.swift in Sources */,
342548F01BE7EED500FBE524 /* MetadataTypes.swift in Sources */,
Expand Down Expand Up @@ -500,7 +493,6 @@
C6DF6C551D1B09DD00259F4B /* Formatter.swift in Sources */,
C6DF6C5C1D1B09DD00259F4B /* PhoneNumber.swift in Sources */,
C6DF6C541D1B09DD00259F4B /* Constants.swift in Sources */,
C6DF6C5B1D1B09DD00259F4B /* ParseOperation.swift in Sources */,
C6DF6C591D1B09DD00259F4B /* MetadataTypes.swift in Sources */,
C6DF6C5F1D1B09DD00259F4B /* RegexManager.swift in Sources */,
C6DF6C581D1B09DD00259F4B /* MetadataManager.swift in Sources */,
Expand All @@ -519,7 +511,6 @@
C6DF6CB31D1B159A00259F4B /* Formatter.swift in Sources */,
C6DF6CBA1D1B159A00259F4B /* PhoneNumber.swift in Sources */,
C6DF6CB21D1B159A00259F4B /* Constants.swift in Sources */,
C6DF6CB91D1B159A00259F4B /* ParseOperation.swift in Sources */,
C6DF6CB71D1B159A00259F4B /* MetadataTypes.swift in Sources */,
C6DF6CBD1D1B159A00259F4B /* RegexManager.swift in Sources */,
C6DF6CB61D1B159A00259F4B /* MetadataManager.swift in Sources */,
Expand All @@ -538,7 +529,6 @@
C6DF6CD61D1B18D800259F4B /* Formatter.swift in Sources */,
C6DF6CDD1D1B18D800259F4B /* PhoneNumber.swift in Sources */,
C6DF6CD51D1B18D800259F4B /* Constants.swift in Sources */,
C6DF6CDC1D1B18D800259F4B /* ParseOperation.swift in Sources */,
C6DF6CDA1D1B18D800259F4B /* MetadataTypes.swift in Sources */,
C6DF6CE01D1B18D800259F4B /* RegexManager.swift in Sources */,
C6DF6CD91D1B18D800259F4B /* MetadataManager.swift in Sources */,
Expand Down
1 change: 0 additions & 1 deletion PhoneNumberKit/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ struct PhoneNumberConstants {
static let defaultCountry = "US"
static let defaultExtnPrefix = " ext. "
static let longPhoneNumber = "999999999999999"
static let maxConcurrentOperationCount = 3
static let minLengthForNSN = 2
static let maxInputStringLength = 250
static let maxLengthCountryCode = 3
Expand Down
49 changes: 14 additions & 35 deletions PhoneNumberKit/ParseManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,49 +103,28 @@ class ParseManager {
*/
func parseMultiple(_ numberStrings: [String], withRegion region: String, ignoreType: Bool, testCallback: (()->())? = nil) -> [PhoneNumber] {
self.multiParseArray = SynchronizedArray<PhoneNumber>()
let queue = OperationQueue()
queue.maxConcurrentOperationCount = PhoneNumberConstants.maxConcurrentOperationCount
var operationArray: [ParseOperation<PhoneNumber>] = []
let completionOperation = ParseOperation<Bool>()
completionOperation.onStart { asyncOp in
asyncOp.finish(with: true)
}
completionOperation.whenFinished { asyncOp in
}
let group = DispatchGroup()
let queue = DispatchQueue(label: "com.phonenumberkit.multipleparse", qos: .default)
for (index, numberString) in numberStrings.enumerated() {
let parseTask = parseOperation(numberString, withRegion:region, ignoreType: ignoreType)
parseTask.whenFinished { [weak self] operation in
if let phoneNumber = operation.output.value {
self?.multiParseArray.append(phoneNumber)
}
}
operationArray.append(parseTask)
completionOperation.addDependency(parseTask)
group.enter()
queue.async(group: group, execute: {
[weak self] in
do {
if let phoneNumebr = try self?.parse(numberString, withRegion: region, ignoreType: ignoreType) {
self?.multiParseArray.append(phoneNumebr)
}
} catch {}
group.leave()
})
if index == numberStrings.count/2 {
testCallback?()
}
}
queue.addOperations(operationArray, waitUntilFinished: false)
queue.addOperations([completionOperation], waitUntilFinished: true)
group.wait()
let localMultiParseArray = self.multiParseArray
return localMultiParseArray.array
}

/**
Single parsing task, used as an element of parseMultiple.
- Parameter rawNumbers: An array of raw number strings.
- Parameter region: ISO 639 compliant region code.
- Returns: Parse operation with an implementation handler and no completion handler.
*/
func parseOperation(_ numberString: String, withRegion region: String, ignoreType: Bool) -> ParseOperation<PhoneNumber> {
let operation = ParseOperation<PhoneNumber>()
operation.onStart { asyncOp in
let phoneNumber = try self.parse(numberString, withRegion: region, ignoreType: ignoreType)
asyncOp.finish(with: phoneNumber)
}
return operation
}


func getRegionCode(of nationalNumber: UInt64, countryCode: UInt64, leadingZero: Bool) -> String? {
guard let regexManager = regexManager, let metadataManager = metadataManager, let regions = metadataManager.territoriesByCode[countryCode] else { return nil }

Expand Down
218 changes: 0 additions & 218 deletions PhoneNumberKit/ParseOperation.swift

This file was deleted.

0 comments on commit 8012d8e

Please sign in to comment.