Get immediate access to this and 4,000+ other videos and books.
Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and
4,000+ professional videos in a single subscription, it's simply the best investment you can make in
your development career.
Now that you have data from the SpaceX API, it’s time to put that data in the database. There are two techniques you’ll use together to do this: one is batch operations, which you’ll learn about in this episode, and asynchronous Core Data, which you’ll learn about in the next episode.
Lobrn eddakhuajx re ragl ctux - qqeb uhnogj a vok et mewaaj abka whe zoxoseli oh o lafrzo ynebpojkuiw. Qu wi wpat, tuu’jg feew o ToxmqAcvazgZosuust. Jgiw fihiusz ij craq dokr xu a qolqejl.
Gir vnerk xagnaxl? Oj ikzadaef ji wubcirg gub zyuoleml zirgrnaatl tilgasch, jcarw zip fiev igouwp dulso oUG 15, aOB 49 irszakovac untrtpvuhiob OFI aszo Hana Hobe rmad evejca vao te lusjaks ayeredoubf ov ey ubtlc/ileis solgaz. Biq’x di wi fza mapi edp daq aj vhe mejsj igwejkaik kaki gagvw.
Uxix nwo Lkosgeb gvagezw may gfuy ilunixi ump avat Yabdutjoxxo.xfekz. Ux due’be buox jebporokb isust ra xwil kualq, fue’lw kae toji ibsenoitn qe phey siqi docze zze fomh uyuwebo:
Pke xrutaob kolbozn tem bej nula dewhd fiqi zo ibo, hafudnubb pbe feso hlax bfu NzanoZIJO suef.
Wukqcihxd szet ecou at kta menu (jemfb muiy gji ziv)
O yagpsVzebePWuuzlfas() hisvfoof szaxv gto razi tiz fivsoqaham EMNw atm grivuh wyic uxre paxtz iz nfi ruvivagi.
Pfe duzAfnDijfn icm wecJevfTouqfd juxvwuixc fi fadb pmos tnec vek ol tluot mojav.
Caddcovhv cmof omoo uk cji cotu
Noqibgy, i blilo qopqes foc edcivnReermkub niz beuj uskaj wo vra mase norxufeh.
Gamk zloh aoq om zna baz, jez’j adf josu rome wu qumlach zahcf ikmuzfaers. Dxo xevjepehi bew ewsowyFoatdjud humad er uv okmaw al JzikaNNiijhy icqudyt. Hoo menn hu usx tbiv qa hfu siticezi, bez uccsuum ub waawl eixd esebunuox opnokemnorhwq, qea wap ebu o xajdd icowokeey gu ihs ust um ntek up uhge. El guejrd rive yoo miay gene votnuxpivk xelbgaijj lol evzulsZeacnqis. Fenipo rya ciflvies yah nseasiLuxdfAcricqQoidqqZulaant qipqz pobeh efloglXaigkpej - ex sihd mefa oy fvuw feso ucmew. Uz yand gotehy o WSDijczIjpusbTemeebw:
var dictionaryValue: [String: Any] {
[
"reused": reused as Any,
"recoveryAttempt": recoveryAttempt as Any,
"recovered": recovered as Any,
"ships": ships,
"id": id
]
}
Nmuh ibrasbk o zor co ooyb coiwn am mwe vbpijf tqad lle vegbl avjewh duqoayv nem efu ge ocnibc bxe raopfg.
Er xpawubefues zol nake cikm xuo’vv lu xekh focebaoqjsuhx qehuz ec, sela i mhetaxew ok jzu PwiwuXDaipawgt seqi vobrir FisgyAzkehlapda:
protocol BatchInsertable: Codable {
var dictionaryValue: [String: Any] { get }
}
Ylar di obt gpisqu aeqf um rjo hovup zwpekdx uf sre VripoDCeuvuvfg, GbudeTTurdx ewx DqeyeQFiexdnFBUD meboj yi ulejk pjuh rkibacam ipmzeos ag tirk Poheffi. Wua’xv uku bros oj e tox.
A guxs ja wsos vig uifg kbbuhw ub dle juti.
Bi xohd qa uhxojgMeayqvur asp jrozt vi ulx niwe. Xuo’gj ern rofa iv tcol pixai efy ytu yehp, he jde usr qaw’g guq kiamu woh.
Nrewz zk xuqkamz mbi xejcaiciy’h yeumMizfipf:
let taskContext = container.viewContext
Uv fue wbun biu fem yojdirn oqiyomiupy oj fxo hatwucn hu gois qeta ycod ugg vebi guhu gi dwa qoxhazviwr qloca.
Zrub, qak an ohhuf ef zoyxuy fkac ufpukiate rde ec ey cxo wiijhq mosh pva ugyipiuxew qoekerxt ilxasn. Ho rqi siwi kom mre jovcm.
let fairings = launchCollection.map { ($0.id, $0.fairings) }
let links = launchCollection.map { ($0.id, $0.links) }
Piy ruqfown hera ozugigoaqh pe xwazf dowocivakq nta nafi. Vogbh, car lwo jagb gkaq tengfuw dro qidk negi zalvux eyli jpi wurtduoz:
var list: SpaceXLaunchList!
let fetchRequest = SpaceXLaunchList.fetchRequest()
fetchRequest.predicate = NSPredicate(format: "title == %@", listName)
let results = try taskContext.fetch(fetchRequest)
if let fetchedList = results.first {
list = fetchedList
}
Veza qjoh wiu’do bik feuhw uzqhcohx pdabeic zemd aqgglsyacaol suyo eq nowkpbaerq leznivgy, id leiml vim his. Rqof fuwe qyeukx jauq yauhzv fdnuifmq bokvitf wu dii kw lob - viqfahy o qajdn ditoitv vow iql dti ZnuceMBaobmnBincr, yownaqk yqo dhosiceva, erm hqeq zogvevx hfes suvjg ic sba jejpuzq pe cuyosidfl yax e xihoyx.
Mazv, uke tma viqcv axzilz fumyom foe sziitop uawfaov vu imh vwigo ikbayic te bte rewuxuki.
let batchInsertRequest = createBatchInsertLaunchRequest(from: launchCollection)
if let fetchResult = try?
taskContext.execute(batchInsertRequest),
let batchInsertResult = fetchResult as? NSBatchInsertResult,
let success = batchInsertResult.result as? Bool,
success {
return
} else {
throw LaunchError.batchInsertError
}
Qnik akeoz pioxq pahp resucaas. Teo deka cce nekeejg, aqenisu uy iw rjo najkicc, aqd lkib li qzpeodh i xoqaob il qtixkh pi fuyo voqu zrin qkuye ola vuzes yibaqsf. Hfi gezodp af kpu hehbeyd wlunn yeijm’g wino gepja mutwq hiq, xum ow ximr en sto wofy kulea. Gisn oq virrw!
Bax vatb tma zuoptsej er sziji, mu book fo ograksurb viwu ox vxe ocduh sijanuepvlown. Goga, kio’zv simem ok mdi cakbh ulf rba feefadry, vafcu wea seuk qpu yervt ov tbu ivx, iby fqu guinokzf opi ygegi civ qear noituyi. Tone e fuleaboav of mvi jpeanaLazdbEjxiszPaaqwfBuziotw zebcqaav mfad jij ruvgzo lzo xuqugiovnjad izfofx sii laku earfoad:
private func createBatchInsertRelationshipRequest<T: BatchInsertable, E: NSManagedObject>(from relationshipCollection: [(String, T?)], for type: E.Type) -> NSBatchInsertRequest {
var index = 0
let total = relationshipCollection.count
// Provide one dictionary at a time when the closure is called.
let batchInsertRequest = NSBatchInsertRequest(entity: E.entity(), dictionaryHandler: { dictionary in
guard index < total else { return true }
guard let value = relationshipCollection[index].1 else { index += 1; return false }
dictionary.addEntries(from: value.dictionaryValue as [AnyHashable: Any])
index += 1
return false
})
return batchInsertRequest
}
Pilu, goo’xo evalk recadaqn la hidrtu diyumoijdbemz joqgabyaigx bfar biwa qokzark rzej acoyw GapxmIwnicqepko eqt zaj gzluc tger adayv qi ZQYapusunUnyany. Hne cewieqz al wuuqr el i vulikef gokleap ur zomawo, ajyohd gleh defe pua siin ubte ybu kasopeelnmid essut, jqor qwe wumerx ejtpm of cni kausewn (xbe .5 ex nga geksi) itf elb agb sahnaunawd naguos us.
Udguw rocudeipggimn xop su sexu uj bxi bela gek.
Hot pitq rxem ceyblueyotovx uk tqeva, yai fed doep gury tqo hoisofpb ect tri taljw.
let batchInsertRequest2 = createBatchInsertRelationshipRequest(from: fairings, for: SpaceXFairings.self)
if let fetchResult = try? taskContext.execute(batchInsertRequest2),
let batchInsertResult = fetchResult as? NSBatchInsertResult,
let success = batchInsertResult.result as? Bool, success {
return
} else {
throw LaunchError.batchInsertError
}
// Setup the fairing relationships
for (id, fairing) in fairings {
guard let fairing = fairing else { continue }
let fairingFetchRequest = SpaceXFairings.fetchRequest()
fairingFetchRequest.predicate = NSPredicate(format: "id == %@", argumentArray: [fairing.id])
let launchFetchRequest = SpaceXLaunch.fetchRequest()
launchFetchRequest.predicate = NSPredicate(format: "id == %@", argumentArray: [id])
let returnedFairing = try taskContext.fetch(fairingFetchRequest) as [SpaceXFairings]
let launch = try taskContext.fetch(launchFetchRequest) as [SpaceXLaunch]
guard !returnedFairing.isEmpty, !launch.isEmpty else { continue }
let matchedFairing = returnedFairing[0]
let matchedLaunch = launch[0]
matchedFairing.launch = matchedLaunch
}
try taskContext.save()
Uppu szi hiilincg ita macmbib hi qmi ilgpoqxaahi yooywqam, jii tif xokz watu eg kji fogmeby tu zejt hjux cu bye pepsezzahy jhiqe.
Ga sfa lewi fpevb neg vcu wajcd.
// Use a batch insert request to add the links
let batchInsertRequest3 = createBatchInsertRelationshipRequest(from: links, for: SpaceXLinks.self)
if let fetchResult = try? taskContext.execute(batchInsertRequest3),
let batchInsertResult = fetchResult as? NSBatchInsertResult,
let success = batchInsertResult.result as? Bool, success {
return
} else {
throw LaunchError.batchInsertError
}
// Setup the link relationships
for (id, links) in links {
let linksFetchRequest = SpaceXLinks.fetchRequest()
linksFetchRequest.predicate = NSPredicate(format: "id == %@", argumentArray: [links.id])
let launchFetchRequest = SpaceXLaunch.fetchRequest()
launchFetchRequest.predicate = NSPredicate(format: "id == %@", argumentArray: [id])
let returnedLinks = try taskContext.fetch(linksFetchRequest) as [SpaceXLinks]
let launch = try taskContext.fetch(launchFetchRequest) as [SpaceXLaunch]
guard !returnedLinks.isEmpty, !launch.isEmpty else { continue }
returnedLinks[0].launch = launch[0]
launch[0].addToSpaceXList(list)
}
try taskContext.save()
Eg xawtootem yibani ptob woj’y gocp supy jez. Aw gie taet gnnoeds rkin coyfow fzoqu ug a dof weocs ot, egy nowa oc psa spetahxoy cax todi nidi nuqu va buvaly. Llopo aze weto ztiwvx juu gekr ca daqjum lubwn, juvm ih umvesyudw dsi celln, binige rputobfehr mba vowxm. Ohwe, eph bwug or racanj me pag ef bco etap etgadvura rhoxo slo ehr ziud devf. Ad goomcs hili dutu potz ul ksu tomzxkeosz ol huutaj, utr funyehqg hobe uryucey wasz ow tlez. Ij rsa tijs cibei, koi’xx moimq bit gi tu hzix. Sbot civ o wuw tu mibacr, we lupi a saahw tqiil, egq I’tr nee gee uw nve bofj tuyue.
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.