Skip to content

Commit

Permalink
使用Codable编码解码,把URLString改为URLConvertible,优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniels committed May 15, 2019
2 parents baaad63 + 7aea796 commit 0ea9199
Show file tree
Hide file tree
Showing 22 changed files with 633 additions and 244 deletions.
2 changes: 1 addition & 1 deletion Demo/BaseViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class BaseViewController: UIViewController {
guard let self = self,
let downloadManager = self.sessionManager
else { return }
self.downloadURLStrings = downloadManager.tasks.map({ $0.URLString })
self.downloadURLStrings = downloadManager.tasks.map({ $0.url.absoluteString })
self.tableView.reloadData()
self.updateUI()

Expand Down
2 changes: 1 addition & 1 deletion Demo/DownloadTaskCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class DownloadTaskCell: UITableViewCell {
tapClosure?(self)
}

func updateProgress(_ task: Task) {
func updateProgress(_ task: DownloadTask) {
progressView.progress = Float(task.progress.fractionCompleted)
bytesLabel.text = "\(task.progress.completedUnitCount.tr.convertBytesToString())/\(task.progress.totalUnitCount.tr.convertBytesToString())"
speedLabel.text = task.speed.tr.convertSpeedToString()
Expand Down
2 changes: 1 addition & 1 deletion Demo/DownloadViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class DownloadViewController: BaseViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
guard let downloadManager = sessionManager else { return }
downloadURLStrings = downloadManager.tasks.map({ $0.URLString })
downloadURLStrings = downloadManager.tasks.map({ $0.url.absoluteString })
updateUI()
tableView.reloadData()
}
Expand Down
4 changes: 2 additions & 2 deletions Demo/ViewController1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ class ViewController1: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()

if let task = sessionManager.tasks.safeObject(at: 0) as? DownloadTask {
if let task = sessionManager.tasks.safeObject(at: 0) {
updateUI(task)
}
}

private func updateUI(_ task: Task) {
private func updateUI(_ task: DownloadTask) {
let per = task.progress.fractionCompleted
progressLabel.text = "progress: \(String(format: "%.2f", per * 100))%"
progressView.progress = Float(per)
Expand Down
6 changes: 3 additions & 3 deletions Demo/ViewController2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class ViewController2: BaseViewController {
sessionManager = appDelegate.sessionManager2


URLStrings = ["https://officecdn-microsoft-com.akamaized.net/pr/C1297A47-86C4-4C1F-97FA-950631F94777/OfficeMac/Microsoft_Office_2016_16.10.18021001_Installer.pkg",
"http://dl1sw.baidu.com/client/20150922/Xcode_7.1_beta.dmg",
URLStrings = ["https://officecdn-microsoft-com.akamaized.net/pr/C1297A47-86C4-4C1F-97FA-950631F94777/MacAutoupdate/Microsoft_Office_16.24.19041401_Installer.pkg",
"http://m6.pc6.com/xuh6/navicatpre12115.zip",
"http://dldir1.qq.com/qqfile/QQforMac/QQ_V6.5.2.dmg",
"http://m4.pc6.com/cjh3/VicomsoftFTPClient.dmg",
"https://qd.myapp.com/myapp/qqteam/pcqq/QQ9.0.8_2.exe",
Expand Down Expand Up @@ -47,7 +47,7 @@ class ViewController2: BaseViewController {

setupManager()

downloadURLStrings = downloadManager.tasks.map({ $0.URLString })
downloadURLStrings = downloadManager.tasks.map({ $0.url.absoluteString })

updateUI()
tableView.reloadData()
Expand Down
6 changes: 3 additions & 3 deletions Demo/ViewController3.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class ViewController3: BaseViewController {

sessionManager = appDelegate.sessionManager3

URLStrings = ["https://officecdn-microsoft-com.akamaized.net/pr/C1297A47-86C4-4C1F-97FA-950631F94777/OfficeMac/Microsoft_Office_2016_16.10.18021001_Installer.pkg",
"http://dl1sw.baidu.com/client/20150922/Xcode_7.1_beta.dmg",
URLStrings = ["https://officecdn-microsoft-com.akamaized.net/pr/C1297A47-86C4-4C1F-97FA-950631F94777/MacAutoupdate/Microsoft_Office_16.24.19041401_Installer.pkg",
"http://m6.pc6.com/xuh6/navicatpre12115.zip",
"http://dldir1.qq.com/qqfile/QQforMac/QQ_V6.5.2.dmg",
"http://m4.pc6.com/cjh3/VicomsoftFTPClient.dmg",
"https://qd.myapp.com/myapp/qqteam/pcqq/QQ9.0.8_2.exe",
Expand Down Expand Up @@ -49,7 +49,7 @@ class ViewController3: BaseViewController {

setupManager()

downloadURLStrings = downloadManager.tasks.map({ $0.URLString })
downloadURLStrings = downloadManager.tasks.map({ $0.url.absoluteString })

updateUI()
tableView.reloadData()
Expand Down
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ func application(_ application: UIApplication, handleEventsForBackgroundURLSessi
一行代码开启下载

```swift
// 创建下载任务并且开启下载,同时返回可选类型的DownloadTask实例,如果URLString无效,则返回nil
// 创建下载任务并且开启下载,同时返回可选类型的DownloadTask实例,如果url无效,则返回nil
let task = sessionManager.download("http://api.gfs100.cn/upload/20171219/201712191530562229.mp4")

// 批量创建下载任务并且开启下载,返回有效URLString对应的任务数组,URLStrings需要跟fileNames一一对应
// 批量创建下载任务并且开启下载,返回有效url对应的任务数组,url需要跟fileNames一一对应
let tasks = sessionManager.multiDownload(URLStrings)
```

Expand Down Expand Up @@ -175,12 +175,12 @@ task?.progress(onMainQueue: true, { (task) in
}
```

下载任务的管理和操作。**在Tiercel中,URLString是下载任务的唯一标识,如果需要对下载任务进行操作,则使用SessionManager实例对URLString进行操作** 暂停下载、取消下载、移除下载的操作可以添加回调,并且可以选择是否在主线程上执行该回调。
下载任务的管理和操作。**在Tiercel中,url是下载任务的唯一标识,如果需要对下载任务进行操作,则使用SessionManager实例对url进行操作** 暂停下载、取消下载、移除下载的操作可以添加回调,并且可以选择是否在主线程上执行该回调。

```swift
let URLString = "http://api.gfs100.cn/upload/20171219/201712191530562229.mp4"

// 创建下载任务并且开启下载,同时返回可选类型的DownloadTask实例,如果URLString无效,则返回nil
// 创建下载任务并且开启下载,同时返回可选类型的DownloadTask实例,如果url无效,则返回nil
let task = sessionManager.download(URLString)
// 根据URLString查找下载任务,返回可选类型的Task实例,如果不存在,则返回nil
let task = sessionManager.fetchTask(URLString)
Expand Down Expand Up @@ -408,19 +408,19 @@ DispatchQueue.main.asyncAfter(deadline: .now() + 1) {

### DownloadTask

DownloadTask是Tiercel中的下载任务类,继承自Task。**在Tiercel中,URLString是下载任务的唯一标识,URLString代表着任务,如果需要对下载任务进行操作,则使用SessionManager实例对URLString进行操作。** 所以DownloadTask实例都是由SessionManager实例创建,单独创建没有意义
DownloadTask是Tiercel中的下载任务类,继承自Task。**在Tiercel中,url是下载任务的唯一标识,url代表着任务,如果需要对下载任务进行操作,则使用SessionManager实例对url进行操作。** 所以DownloadTask实例都是由SessionManager实例创建,单独创建没有意义

主要属性

```swift
// 保存到沙盒的下载文件的文件名,如果在下载的时候没有设置,则默认为url的md5加上文件扩展名
public internal(set) var fileName: String
// 下载任务对应的URLString
public let URLString: String
// 下载任务对应的url
public let url: URL
// 下载任务的状态
public var status: TRStatus
public var status: Status
// 下载文件的校验状态
public var validation: TRValidation
public var validation: Validation
// 下载任务的进度
public var progress: Progress = Progress()
// 下载任务的开始日期
Expand Down
2 changes: 1 addition & 1 deletion Tiercel.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Pod::Spec.new do |s|
s.name = 'Tiercel'
s.version = '2.2.1'
s.version = '2.3.0'
s.swift_version = '5.0'
s.summary = 'Tiercel is a lightweight, pure-Swift download framework.'

Expand Down
18 changes: 16 additions & 2 deletions Tiercel.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
/* Begin PBXBuildFile section */
E10B78A721E73C3E00DFF2B3 /* ResumeDataHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = E10B78A521E73C3E00DFF2B3 /* ResumeDataHelper.swift */; };
E10B78A821E73C3E00DFF2B3 /* SessionConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = E10B78A621E73C3E00DFF2B3 /* SessionConfiguration.swift */; };
E18F465B228B207A0086BBDF /* CodingUserInfoKey+Cache.swift in Sources */ = {isa = PBXBuildFile; fileRef = E18F465A228B207A0086BBDF /* CodingUserInfoKey+Cache.swift */; };
E18F465C228B207A0086BBDF /* CodingUserInfoKey+Cache.swift in Sources */ = {isa = PBXBuildFile; fileRef = E18F465A228B207A0086BBDF /* CodingUserInfoKey+Cache.swift */; };
E1A14406228A95FC00B4CD6D /* URLConvertible.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1A14405228A95FC00B4CD6D /* URLConvertible.swift */; };
E1A14408228A9CA100B4CD6D /* TiercelError.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1A14407228A9CA100B4CD6D /* TiercelError.swift */; };
E1C1DD3E206C91EF003665A9 /* Tiercel.h in Headers */ = {isa = PBXBuildFile; fileRef = E1C1DD3C206C91EF003665A9 /* Tiercel.h */; settings = {ATTRIBUTES = (Public, ); }; };
E1C1DD4A206C9241003665A9 /* DownloadTask.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1C1DD44206C9241003665A9 /* DownloadTask.swift */; };
E1C1DD4B206C9241003665A9 /* Cache.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1C1DD45206C9241003665A9 /* Cache.swift */; };
Expand Down Expand Up @@ -72,6 +76,9 @@
/* Begin PBXFileReference section */
E10B78A521E73C3E00DFF2B3 /* ResumeDataHelper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ResumeDataHelper.swift; sourceTree = "<group>"; };
E10B78A621E73C3E00DFF2B3 /* SessionConfiguration.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SessionConfiguration.swift; sourceTree = "<group>"; };
E18F465A228B207A0086BBDF /* CodingUserInfoKey+Cache.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CodingUserInfoKey+Cache.swift"; sourceTree = "<group>"; };
E1A14405228A95FC00B4CD6D /* URLConvertible.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = URLConvertible.swift; sourceTree = "<group>"; };
E1A14407228A9CA100B4CD6D /* TiercelError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TiercelError.swift; sourceTree = "<group>"; };
E1C1DD39206C91EF003665A9 /* Tiercel.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Tiercel.framework; sourceTree = BUILT_PRODUCTS_DIR; };
E1C1DD3C206C91EF003665A9 /* Tiercel.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Tiercel.h; sourceTree = "<group>"; };
E1C1DD3D206C91EF003665A9 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -193,6 +200,7 @@
E1D8EFA321F6D2B3004B70C5 /* Array+Safe.swift */,
E1D8EFA521F6D328004B70C5 /* UIDevice+Free.swift */,
E1C6A3D922775F23009C0935 /* OperationQueue+DispatchQueue.swift */,
E18F465A228B207A0086BBDF /* CodingUserInfoKey+Cache.swift */,
);
path = Extensions;
sourceTree = "<group>";
Expand All @@ -208,6 +216,8 @@
E10B78A621E73C3E00DFF2B3 /* SessionConfiguration.swift */,
E1C1DD48206C9241003665A9 /* SessionManager.swift */,
E1C1DD49206C9241003665A9 /* SessionDelegate.swift */,
E1A14405228A95FC00B4CD6D /* URLConvertible.swift */,
E1A14407228A9CA100B4CD6D /* TiercelError.swift */,
);
path = General;
sourceTree = "<group>";
Expand Down Expand Up @@ -347,6 +357,7 @@
E10B78A721E73C3E00DFF2B3 /* ResumeDataHelper.swift in Sources */,
E1D8EF9C21F6CEFB004B70C5 /* URLSession+ResumeData.swift in Sources */,
E1D8EFA421F6D2B3004B70C5 /* Array+Safe.swift in Sources */,
E18F465B228B207A0086BBDF /* CodingUserInfoKey+Cache.swift in Sources */,
E1C1DD4E206C9241003665A9 /* SessionManager.swift in Sources */,
E1C6A3D522775C33009C0935 /* Executer.swift in Sources */,
E1D8EF9A21F6CE5C004B70C5 /* Data+Hash.swift in Sources */,
Expand All @@ -359,6 +370,8 @@
E10B78A821E73C3E00DFF2B3 /* SessionConfiguration.swift in Sources */,
E1C1DD4F206C9241003665A9 /* SessionDelegate.swift in Sources */,
E1D8EFA221F6CFE9004B70C5 /* DispatchQueue+Safe.swift in Sources */,
E1A14408228A9CA100B4CD6D /* TiercelError.swift in Sources */,
E1A14406228A95FC00B4CD6D /* URLConvertible.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -371,6 +384,7 @@
E1C1DD6F206C937B003665A9 /* ViewController3.swift in Sources */,
E1C1DD74206C9385003665A9 /* DownloadViewController.swift in Sources */,
E1C1DD75206C9385003665A9 /* DownloadTaskCell.swift in Sources */,
E18F465C228B207A0086BBDF /* CodingUserInfoKey+Cache.swift in Sources */,
E1C1DD76206C9385003665A9 /* BaseViewController.swift in Sources */,
E1C1DD6E206C937B003665A9 /* ListViewCell.swift in Sources */,
E1C1DD6B206C937B003665A9 /* ViewController2.swift in Sources */,
Expand Down Expand Up @@ -575,7 +589,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_IDENTITY = "iPhone Developer";
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = Y3437T5EGX;
DEVELOPMENT_TEAM = 938M2GJR4V;
INFOPLIST_FILE = Demo/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
Expand All @@ -594,7 +608,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_IDENTITY = "iPhone Developer";
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = Y3437T5EGX;
DEVELOPMENT_TEAM = 938M2GJR4V;
INFOPLIST_FILE = Demo/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
Expand Down
91 changes: 91 additions & 0 deletions Tiercel.xcodeproj/xcshareddata/xcschemes/Demo.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1020"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "E1C1DD53206C92FE003665A9"
BuildableName = "Demo.app"
BlueprintName = "Demo"
ReferencedContainer = "container:Tiercel.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "E1C1DD53206C92FE003665A9"
BuildableName = "Demo.app"
BlueprintName = "Demo"
ReferencedContainer = "container:Tiercel.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "E1C1DD53206C92FE003665A9"
BuildableName = "Demo.app"
BlueprintName = "Demo"
ReferencedContainer = "container:Tiercel.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "E1C1DD53206C92FE003665A9"
BuildableName = "Demo.app"
BlueprintName = "Demo"
ReferencedContainer = "container:Tiercel.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
80 changes: 80 additions & 0 deletions Tiercel.xcodeproj/xcshareddata/xcschemes/Tiercel.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1020"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "E1C1DD38206C91EF003665A9"
BuildableName = "Tiercel.framework"
BlueprintName = "Tiercel"
ReferencedContainer = "container:Tiercel.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "E1C1DD38206C91EF003665A9"
BuildableName = "Tiercel.framework"
BlueprintName = "Tiercel"
ReferencedContainer = "container:Tiercel.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "E1C1DD38206C91EF003665A9"
BuildableName = "Tiercel.framework"
BlueprintName = "Tiercel"
ReferencedContainer = "container:Tiercel.xcodeproj">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
Loading

0 comments on commit 0ea9199

Please sign in to comment.