Reed 1.0.0

Reed 1.0.0

周瑞发维护。



 
依赖
CocoaLumberjack/Swift>= 0
ActiveSQLite= 0.5.1
ZKCommonCrypto>= 0
 

Reed 1.0.0

  • 作者
  • taozui

Reed

Version

License Platform

Reed是一个Swift的下载数据库框架。它具有许多功能,稳定性高,速度快,低耦合,易于扩展。它使用ActiveSQLite持久化数据。

中文版

功能

  • 多下载任务
  • 在断点处恢复
  • 最大并发数
  • 验证MD5文件
  • 检查完整空间
  • 线程安全
  • 多范围
  • 使用CocoaLumberjack记录日志
  • 后台下载
  • OperationQueue

演示

设计

- 核心下载器 -> 纯下载器

核心下载器只存在内存中,不使用数据库。可以在没有Reed层的情况下使用它进行下载。

- Reed 下载管理器 -> 下载管理器

它在数据库中保存下载模型,处理下载状态变化,处理错误,并发布通知。

关键词

1 下载信息模型

这是一个简化的模型版本:

class ReedInfo {
    var key:String = "" //Identifier
    var url:String = ""
    var md5:String?
    var totalBytes:NSNumber = 0
    var writedBytes:NSNumber = 0
    var destFilePath = "" 
    var cacheFilePath = "" 
    
    var context = "" 
    var downloadListKey = ""  
    
    var downloadStatus:ReedDownloadStatus = .waiting 
    var downloadFailReason:ReedDownloadFailReason? 
    
}

2 下载状态

public enum ReedDownloadStatus: String {
    case waiting
    case downloading
    case pause
    case completed
    case faild
}

3 下载失败原因

public enum ReedDownloadFailReason:String{
    case unknown
    case timeout
    case noNetwork
    case fullSpace
    case md5Verification
}

使用方法

开始下载

1 直接下载

/// key: Identifier
/// url: 
/// destinationFilePath:eg:”Documents/xxxx/filename.pdf“
/// downloadListKey : Default value is nil. can exist many than one downloadlist
/// md5: Value of md5. Default value is nil.
/// isReplace: Replace the old download destination file and cache file. Default value is false
Reed.shared.start(key:"key", url:"url", destinationFilePath:"Documents/filename.pdf",downloadListKey:"PDF_DOWNLOAD_LIST",md5:nil,isReplace:false)

2 首先将任务添加到下载列表,然后开始它。

Reed.shared.addToStartList(key:"key", url:"url", destinationFilePath:"Documents/filename.xxx")


Reed.shared.start(key: "key")

3 首先将任务添加到下载列表,然后检查并启动(推荐)

Reed.shared.addToStartList(key:"key", url:"url", destinationFilePath:"Documents/filename.xxx")

Reed.shared.checkToStart()

介绍 checkToStart()

定义:

checkToStart(downloadListKey:String? == nil)

描述:

检查设备上的可用空间;

检查下载列表中的最大并发下载数量;

启动状态的等待下载任务,并按添加日期排序;

用例:

1,网络可用。相反关闭。

2,用户登录。相反关闭。

3,如果需要,启动应用程序。

4. 当下载任务状态改变(完成、暂停、失败、删除)时,Reed会自动调用。下载计数减1,可以启动其他等待的任务。

作用域

第一 第二 第三
上下文1
下载列表键1 键1
键2
键3
下载列表键2 键4
键5
键6
键7

1 上下文

上下文是第一级作用域

ReedInfo.context的值来自Reed.shared.context的值。它们是相同的。

例如:应用程序启动 -> 设置Reed.shared.context -> 开始下载。

例如:shutDown() -> 设置Reed.shared.context -> 开始下载。

上下文通常用于区分不同的用户。

例如:logout() -> shutDown() -> login() -> 设置Reed.shared.context。

2 下载列表键

downloadListKey是ReedInfo的一个属性。

一对上下文和downloadListKey定义了一个downloadListKey,它有自己的最大并发下载数量。

downloadListKey通常用于不同的业务,例如:PDF_LIST、MP3_LIST。

3 键

键是ReedInfo的一个属性,你必须为每个任务设置不同的标识符键。

通知

Noti_ReedDownload_Add_To_Downlaod_List

Noti_ReedDownload_Start

Noti_ReedDownload_Progress

Noti_ReedDownload_Complete

Noti_ReedDownload_Fails

Noti_ReedDownload_Waiting

Noti_ReedDownload_Pause

Noti_ReedDownload_Delete

Noti_ReedDownload_FullSpace

每个通知包含一个对象,其类型为ReedInfo。

如果空间已满,Reed将发布一个或多个对象为ReedInfo的通知,并发布一个对象为nil的通知。

最常用的API

与控制相关的API

	///Add a task to downloadlist
	public func addToStartList(key:String, url:String, destinationFilePath:String,downloadListKey:String? = nil,md5:String? = nil,isReplace:Bool = false)

    /// start download when added a task to downloadlist
    public func start(_ key:String, isReplace:Bool = false)
    
    /// Direct download. not recommend. Recommend use addToStartList and checkToStart.
    public func start(key:String, url:String, destinationFilePath:String, downloadListKey:String? = nil,md5:String? = nil,isReplace:Bool = false)
    
    /// batch start download
    public func startBatch(keys:[String])
    
    /// check to start. Go to "Introduce checkToStart()" on this ReadMe.
    public func checkToStart()
    
    /// check to start on this downloadList.
    public func checkToStart(downloadListKey:String)
    
    ///isTryStart meens:If call checkToStart() after the action. usually use default value.
        
    public func pause(_ key:String, isTryStart:Bool? = true)

    public func pauseBatch(keys:[String],isTryStart:Bool? = true)

    public func deleteBatch(keys:[String],isTryStart:Bool? = true)
    
    public func delete(_ key:String,isTryStart:Bool? = true)
    
    //Shutdown all tasks. But all status of tasks are not changed. 
    //Execute this when:logout;no network;go to background.
    public func shutDown()
    

与状态相关的API

    /// is downloading progress
    /// (Downloading, waiting, pause, failed)
    public func isDownloadingProgress(_ key:String) -> Bool
    
    /// is undownload (un add to download list, un start, no record on database)
    public func isUnDownload(_ key:String) -> Bool
    
    public func isComplete(_ key:String) -> Bool
    
    public func isDownloading(_ key:String) -> Bool
    
    public func isPause(_ key:String) -> Bool
    
    public func isWaiting(_ key:String) -> Bool
    
    public func isFailed(_ key:String) -> Bool
    
    /// get download model with key
    public func getDownloadInfo(key:String) -> ReedInfo?
    
    /// get all models on this downloadListKey
    public func getDownloadInfos(downloadListKey:String? = nil) -> [ReedInfo]
    
    /// get all models on download progress(Downloading,Waiting,Pause,Failed)
    public func getDownloadProgressInfos(downloadListKey:String? = nil) -> [ReedInfo]
    
    /// get all complete models
    public func getCompleteInfos(downloadListKey:String? = nil) -> [ReedInfo]
    
    @objc public func getDownloadProgressCount(downloadListKey:String? = nil) -> Int
    
    @objc public func getDownloadingCount(downloadListKey:String? = nil) -> Int

    @objc public func getWaitingCount(downloadListKey:String? = nil) -> Int
    
    @objc public func getPauseCount(downloadListKey:String? = nil) -> Int
    

配置

 	 /// Concurrent max download count on one downloadlist,default value is 3
    Reed.shared.maxCount
    
    /// Retry count when md5 invalidate,default value is 3
    Reed.shared.maxRetryCount 

    /// Set it before start download, default value is ""
    public var context
    
    /// min interval between two download progress notifications been post, default value is 0.3 seconds
    Reed.shared.progressPostTimeInterval:TimeInterval
    
    /// If you use CocoaLumberjack,Reed would use your CocoaLumberjack configration.
    /// If you not use CocoaLumberjack, Reed would not print whenever what value of showLogger.
    Reed.shared.showLogger:Bool = true
    
    /// If you not use CocoaLumberjack,Call this method to config CocoaLumberjack
    /// timeFormatter:Date message of every log,default value is "yyyy-MM-dd'T'HH:mm:ss.SSSZ" of Date()
    public func configLogger(level:DDLogLevel? = .info, timeFormatter:(()->String)? = nil)

要求

  • iOS 8.0+
  • Xcode 10.2
  • Swift 5

安装

CocoaPods

将以下行添加到您的Podfile中:

pod "Reed"

作者

周凯文

许可协议

Reed项目可在MIT许可下使用。更多详情请参阅LICENSE文件。