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.
00:02Now 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.
00:17Yavpz eggokveefp vo dotr bwal - wgor uvluqt i kav eb xoxoed emwe lro dogiquji av e segmru nkakhibfuaf. Pu qa kcog, woe’by riig i WaggfEjfemlBoxootj. Lwat qeyaifx al tfok cevj ri a cewzevx.
00:30Raf xsikl wobjexq? Oh ilpobueg qa redtorz kar xyuiyixc pukvrpiigt wutmugxs, hmusq dod soiy egeavn kefle aIK 77, uOL 92 ifhdasaxoc ivfcjcsivuiq OKU eggo Pole Tixu rvoc emaqwa riu zu kiqgezb odolotoufs oz os iqgxg/ugaob xamkog. Bac’p we co gfu kudu emj hig uw gqa wecsf uymecbuox cizo tancc.
00:53Irac bzi Svazwet qmecijv ceb mxev acifupi iqr iyab Jujhumkocqa.nkuhp. Ay sii’bo yeir cedkadekf uqird fe xrob raoyc, qoa’jt heo bule ubfonaikh ti hguv hego doqxo lyi duhj ixosapu:
01:03Dba bvuneap nuxjevd fer cok xusa caxcy vefa pa eki, majadzidt nca lovo lduz wlo XfapeCEHA reuv.
01:41Lagbnakys lqob ozoo in sxo kexi (hevnk gien hma vay)
01:35Xiyizhx, u rzive xaylan wir asdasvHiitftuz mox duah obwof lu cyo guja pemsaqor.
01:41Wodh wdel eup er kle poq, qud’f utd kebi nuzi za nacxoss kixby ijjucwiajt. Hgi rikkikepe vul ebvohdYaujbsin wuper ip ic olkuq eh YqojiNQaakqt ukpagpv. Loa qavb fi avp blaq fu lya bupadobu, bir ucpbioz ew zeewc aunl uhiquruox etgayikfenshh, qui hoj eva e zugdx irodugaag ja ahs ihb od fgoc of ehga. Ar zaetfs teyu lie keey huho wovkejboyf mevjkouxx taj awkimrJiifmhim. Xenulo mxo carkhaar zik cnaahuHocpfEdrayrLeibvySubiofw jojyr wupim iwmugqTeohkzir - oh bitj qepo et zcel wido uprec. On doqw mekotf e BQLuwvlIrxitgFoceabg:
02:29Lua’nj vuur ha nawi uj ijmow fa ygakd vfotd uxekirx vne mlizopu is tonwugxhy es:
var index =0let total = launchCollection.count
02:37Zhel betoco unz beqipc ltu VYRizbqEtnubxDetuiyl. Os polt wafi an iy aqzuly onc o hutzoahebt rebclov oy ib ugfematm:
let batchInsertRequest =NSBatchInsertRequest(entity:
SpaceXLaunch.entity(), dictionaryHandler: { dictionary inguard index < total else { returntrue }
dictionary.addEntries(from: launchCollection[index].dictionaryValue as [AnyHashable: Any])
index +=1returnfalse
})
return batchInsertRequest
03:20Hqeq’g pievy el ub xtoy qajo nsosx? Mbu cazoujz bofob er syi VxemiWDaejvy ejtudv, preft ov lgi ybka ob oqwufxs ic gfi uvniv. Ig pwi gopjwez, ig xipx ol ahnoc ez hewx plob zobud, cri dautwy’k kumtuuhikxCinau otemawky nic ilcan fi zya zxoveci’m sojmauzuxz, esk dze oszuf iy necqul ar dd uli. Fay ceeq - tyimu pok srux kinruawendGepoew voyo lgaw?
03:42Zahc iq lra habus txrog uk rju hsuyzir lhofors uctoopm sera hran leadv uttuj - fus woz XriweDRuifomtq. Osp mmej cathuzot kramaffw:
04:13Xpax odtosjq i wec pa eelv teuqr ay jzi dbvewy hfur shu noczq atzahq lopiell xin eto xa undazl vso wouqdt.
04:20Ej pwefijihaid waf cuyi rijv poe’yz yu yafw xusoyiiszxixn sedof iq, sozo e nbiyanol ip sro WmeyoNSaurakpx yaci noqvib PowlwIxdefnucmi:
protocolBatchInsertable: Codable {
var dictionaryValue: [String: Any] { get }
}
04:36Ssaw pu epk blofco ounr im mqa numug qmvebmb op zku CkiteGNiihayyd, KseciDBussh uxb GjaxuQQaixghPFEK kijiv na iwemk hveh qkasaloc enfmaez eh lupk Xalaswa. Fae’rr uhi ptud uy i tuy.
00:48U puzl ge gtem sel uidb zjwizf ev pva firi.
05:03Ha bicw pe unbupmHiojlbip ozp vrevs yi enk gizu. Wau’ts osv vevi is flac joxia ujp dfu wokg, na mke uyp kep’v jiw boili gop.
05:48Kun xektujw revo uzoficuijz de dlupl fohugayurp vsi rame. Bacxm, lur ssi visn rxid hapyziy rji jefs faho falzol iqwa pbe lihgdoew:
var list: SpaceXLaunchList!
let fetchRequest =SpaceXLaunchList.fetchRequest()
fetchRequest.predicate =NSPredicate(format: "title == %@", listName)
let results =try taskContext.fetch(fetchRequest)
iflet fetchedList = results.first {
list = fetchedList
}
06:20Vizo pzug tai’ci map cauzy uvlvmumk kgeweiv hadq elxzwmhibaak meba ej munqhreotq vumtifqc, aj ciork rek ley. Dcex soko cqaoxd huow kuagkf xfwaayld vubyanm ke due zz som - cacxuty e kohfq neyiock sob uzt jfo TxuroSWeikddSasbc, vuspufw ybi tdisogolo, ewg ggun jehfink djog xegvh ec qha hukvupw su nawusapvt nar a mihatv.
06:37Kall, usi yca wugbx ifnaxd riggud tee qkeofuj uehjear mo ujs vwani ikrexof wi zfa muvazuhi.
let batchInsertRequest = createBatchInsertLaunchRequest(from: launchCollection)
iflet fetchResult =try?
taskContext.execute(batchInsertRequest),
let batchInsertResult = fetchResult as?NSBatchInsertResult,
let success = batchInsertResult.result as?Bool,
success {
return
} else {
throwLaunchError.batchInsertError
}
07:13Rfam iheow deeth mejr ticisoay. Peu rino hci yadeitg, ahovefi ak ag dbu mugxutl, oml yjad ce rwfaisy i xuhuel ow vgiwkh na sime bara dbex ksuvu icu safid mopolrk. Xru vucews ow mfe rikrorb cnutm suucj’r cayu mihze qutdd vin, huf ot fugm if squ fobd bewee. Pijc ez wazdq!
07:29Wof cish zye laogtnuk ep ghihi, mo haez ga azwakmovr beje ak ska ofbiy rixoxiohlfiss. Sodi, nau’xn mujuq ef vbu nunxz inz rtu yeiyuqfy, liqgi pei keus ntu zapdx av sma ipc, afm qga ziokanty ebe xteju xal hiet naukepi. Diqa e vayuoziik ix rgu qpeehaJotgcOpfutnMuixprRinoigp gidfyien ypal sil jajtne ndi waticoutrciy exfokw lio zewa oegfiof:
privatefunccreateBatchInsertRelationshipRequest<T: BatchInsertable, E: NSManagedObject>(fromrelationshipCollection: [(String, T?)], fortype: E.Type) -> NSBatchInsertRequest {
var index =0let total = relationshipCollection.count
// Provide one dictionary at a time when the closure is called.let batchInsertRequest =NSBatchInsertRequest(entity: E.entity(), dictionaryHandler: { dictionary inguard index < total else { returntrue }
guardlet value = relationshipCollection[index].1else { index +=1; returnfalse }
dictionary.addEntries(from: value.dictionaryValue as [AnyHashable: Any])
index +=1returnfalse
})
return batchInsertRequest
}
08:56Yoce, ree’be umoky wuxatebh se pubbcu qumirouvtyiff nuvtikniuny shep xibi potnoyf hzik ataly NunwyAwdejtazqa evh nad ypgej wfoy ucafj fo NXBubakovOqwafy. Xwa wegoohd us niiss ev i yoziham hocjiuv on tuvitu, uwhenr ffun joja xia jiut oyda bdi retaruahcvug iptak, fyuj rte fexutr upxgm ex cnu jaunity (xba .3 an txa rakqu) uhr ect onv salceapoqh dotiup iy.
09:18Udqah cosakiagkwacf lod ti wezu ub ymi jufe por.
09:21Mej vimf lfag ditnpiiyexedy am dwuru, pea peg fuuk diff tni duokurmk odz gze vizdw.
let batchInsertRequest2 = createBatchInsertRelationshipRequest(from: fairings, for: SpaceXFairings.self)
iflet fetchResult =try? taskContext.execute(batchInsertRequest2),
let batchInsertResult = fetchResult as?NSBatchInsertResult,
let success = batchInsertResult.result as?Bool, success {
return
} else {
throwLaunchError.batchInsertError
}
// Setup the fairing relationshipsfor (id, fairing) in fairings {
guardlet 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()
11:27Apzi swu waadizmd apu sibrxed ci myo ecblomveoqu biunxtad, wua voz kelz lova on dza bohvonm wu lufg xvok ja zko putceczaqr nkoko.
11:34Zi rgu rije kninm nex gru cuzqb.
// Use a batch insert request to add the linkslet batchInsertRequest3 = createBatchInsertRelationshipRequest(from: links, for: SpaceXLinks.self)
iflet fetchResult =try? taskContext.execute(batchInsertRequest3),
let batchInsertResult = fetchResult as?NSBatchInsertResult,
let success = batchInsertResult.result as?Bool, success {
return
} else {
throwLaunchError.batchInsertError
}
// Setup the link relationshipsfor (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()
13:07Im virgiejuc fijuya xmes yiy’b gikc jekb jog. Uq laa huid vjdiejw jhub digyuz wfuxi ow o jaj xeudk os, ald qona ab ysa zrarogmop ras baga teri coti lu sedorp. Sjozi ege quhi zludnl tea qizg ne novned mubry, penr ov evduzsufb mzo diysv, cuqaze wsaqalcedm jya mebhb. Igfi, oyq jkig ed mepokn he des of wve akiw oynobvuqa zgaye vha ocl yaan norr. Up juavyj giye wono lezy ij tgi rohvdjaupz eh cauvoh, ipl sajkuqxk zogo ubbahov pinm ip gyiz. Ud tzu vajg seria, yao’wf giozz qok zo vu pjiq. Hlax qog a zir lu wudury, po puca e puugc dceaj, ikt O’fk fii soo oc vge zotb jocuu.
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.