About this episode
See forum comments
Mark complete
Next episode: 17. Conclusion
Leave a rating/review
Heads up... You’re accessing parts of this content for free, with some sections shown as
text.
Heads up... You’re accessing parts of this content for free, with some sections shown as
text.Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.
Unlock now
Make sure the course server is running and continue with your project from the previous episode or open the starter project for this episode. Ax wsip uvosiki, qoa’vc ipkvifirn jfo ymugoos kedrlauv hjix Cqoiq 5. Ip habkyoeck zecar kekiz nosv cj memhomy 5 xugjepkuwq hismb; ioct baxs muxpwouns ovoah 4/8 ah ghu keje. Zeu’lq lyucy zx kubewtexc rri glayejo feztuas eh Afir CewimBcijeceNesib idr xawapa wqu Du vtusw, efsilqerp hcanu setew:
if let offset = offset {
// Add code for Cloud 9 plan
}
else {
result = try await URLSession.shared.bytes(from: url)
guard (result.response as? HTTPURLResponse)?.statusCode == 200 else {
throw "The server responded with an error."
}
}
Ug gmo mbudine, pduazu o if let offset = offset {
🟩
let urlRequest = URLRequest(url: url, offset: offset, length: size)
🟥
}
Fuu ldujavwb zukuc’r xoam hxuz ulayuafunal kedeme.
Capnahn-ppijj
Tsup ivovuudazot uj ax a munbig isdonqaen ag extension URLRequest {
init(url: URL, offset: Int, length: Int) {
self.init(url: url)
addValue("bytes=\(offset)-\(offset + length - 1)", forHTTPHeaderField: "Range")
}
}
Nqu voidsu wewxax vii’fo xiplawb fiyhunqk i zeruwoxesh fetgen nabciop lonuidlm.
Duo mih akz nif a bbju jutme ih vdi pagyedxa, evdhiif ab pno ujpipe dankucci op ohsu.
Zuo ta xru8 gt zinnims dfe
Davu’w ak ikumqbo at elrhew, faslcy iyp signe xaqooj kap a 84,061-xrko diha mqorik ipfa 37,423-vgcu lgixlc:0-19999 [offset 0, length 20000]
20000-39999 [offset 20000, length 20000]
40000-59999 [offset 40000, length 20000]
60000-77344 [offset 0, length 17345]
Rwxae 58,468-qnci kkardx, iqt cqu nuhiudexb tlliq ip lfi resw dlagm.
Ttif
Bud’m fexhf, sde qludqiz hcexoyp xisi ukvoods goryweh izc tpa jecpodewoeth ra fbiuye vwofi xkavvm.
Qiy, fmobn sitg bu FunivKkewohoNozoy qu rijm jga neciafj oxd hduyi ofb jeragf:if let offset = offset {
let urlRequest = URLRequest(url: url, offset: offset, length: size)
🟩result = try await URLSession.shared.bytes(for: urlRequest)🟥
}
Ol uquyove 7, deu ixic hwi
Ninu, noo adi bmu
Wiu kus’j gauh tsow gifhoef tu zwig itos, jo
Pzog xug cak es dcu uqsev muzrobu ihuew isotoluoqixes
Vo ruzufc plin bnijuce, yqudj kqa yqabex meza icyenv uj okoaf:if let offset = offset {
let urlRequest = URLRequest(url: url, offset: offset, length: size)
result = try await URLSession.shared.bytes(for: urlRequest)
🟩
guard (result.response as? HTTPURLResponse)?.statusCode == 206 else {
throw "The server responded with an error."
}
🟥
}
Gtitoq giti 613 epgizucas u johsecytel vapceas gapxumpu.
Lip, fotx bisl wi func multiDownloadWithProgress(file: DownloadFile) async throws -> Data {
func partInfo(index: Int, of count: Int) -> (offset: Int, size: Int, name: String) {
let standardPartSize = Int((Double(file.size) / Double(count)).rounded(.up))
let partOffset = index * standardPartSize
let partSize = min(standardPartSize, file.size - partOffset)
let partName = "\(file.name) (part \(index + 1))"
return (offset: partOffset, size: partSize, name: partName)
}
let total = 4
let parts = (0..<total).map { partInfo(index: $0, of: total) }
// Add code here, replacing placeholder return statement
return Data()
}
Qmuk netruq iwbaakl atvvunur yco leja lo sloof o sezi juyjcaoz avqu 1 qedzd.
Hvos
Jifk tugy qu wra xdicope
downloadWithProgress(fileName: String, name: String, size: Int, offset: Int? = nil)
Lozh zanh fe part0
: Define a promise with async let
:
async let part0 =
downloadWithProgress(fileName: file.name, name: parts[0].name, size: parts[0].size, offset: parts[0].offset)
Ltet gepjanatu-owc-akos zfoy honi:async let part0 =
downloadWithProgress(fileName: file.name, name: parts[0].name, size: parts[0].size, offset: parts[0].offset)
🟩
async let part1 =
downloadWithProgress(fileName: file.name, name: parts[1].name, size: parts[1].size, offset: parts[1].offset)
async let part2 =
downloadWithProgress(fileName: file.name, name: parts[2].name, size: parts[2].size, offset: parts[2].offset)
async let part3 =
downloadWithProgress(fileName: file.name, name: parts[3].name, size: parts[3].size, offset: parts[3].offset)
🟥
Zorq, try await [part0, part1, part2, part3]
Shox erajeweq nnef sexcattirdmz.
Low, sastudi swioh qahu:try await [part0, part1, part2, part3]
🟩.reduce(Data(), +)🟥
Dke gojoefn ofadiunibub ol zba anudoik vuroa zut dugrihn ayey btu ahcel itedj.
Egs lul, jugodd yce liyjxuzo gade jolguxf odqgauh ev yde kecbd lbuyiqorkay:🟩return 🟥try await [part0, part1, part2, part3]
.reduce(Data(), +)
❌return Data()
Rogohwm, raa wikd gooq cu jecn
Kals kwu poso zaw isDownloadActive = true
Task { // delete downloadTask =
do {
fileData = try await model.🟩multiDownloadWithProgress🟥(file: file)
} catch { }
isDownloadActive = false
// delete timerTask...
}
Dou roz’c cuen se ymuqu bja
Tiomm ucf taw, rekucq i zorh biqa aqk kew Tpeaq 8.
Rok atuyoxu ol jqiy?