Swift iOS -How to Handle GCD Sending Data To Firebase When App Is Shut Down Because Phone Is Shut Down? -
i use gcd dispatchworkitem send data firebase. first send pic firebase storage , receive metadata in completion handler , turn url , string. send url string firebase database. i've tested gcd method while going background, commenting out firstcall it's problemcall fires, , commenting out secondcall it's problemcall fires etc , works fine. i'm unsure of if app stops while 1 of network calls being performed because user shuts off phone.
i'll explain in steps:
the user presses sendbutton, image gets initialized, converted data. have global flag gets sets true when button pressed. set false , gets set false if there problem or if reaches callback of secondcall.
the firstcall dispatchworkitem(dwi) take data , sends fb storage.
at end of firstcall call firstcall.perform() can run. right below initialize problemcall dwi fires off in 10 secs. if fires cancel firstcall, change global flag false, , show error alert.
inside callback firstcall, if it's reached cancel problemcall.
assuming firstcall completed in less 10 seconds initialize secondcall dwi.
inside secondcall take url firstcall , send fb database.
at bottom of secondcall call secondcall.perform() can run. right below again initialize problemcall dwi fire off in 20 secs. if fires off cancel secondcall, change global flag false, , show alert
inside callback secondcall, if it's reached cancel problemcall , self.weregoodnowdosomething() runs, , change globe class false.
aclass:
let storageref = myfbstorageref() let dbref = myfbdatabaseref() var firstcall: dispatchworkitem? var secondcall: dispatchworkitem? var problemcall: dispatchworkitem? var myimage: uiimage? var imageurlstr: string? var someglobalcass: someglobalclass() @ibaction fileprivate func sendbuttonpressed(_ sender: uibutton){ self.someglobalclass.flag = true self.myimage = somefuncreturnssomeimage() if let convertimage = self.myimage, let imagedata = uiimagejpegrepresentation(convertimage, 0.2){ self.firstcall = dispatchworkitem{ [weak self] in self?.storageref.putdata(imagedata, metadata: nil, completion: { (metadata, error) in //if gets here in less 10 secs cancel problemcall firstcall self.problemcall?.cancel() if let url = metadata?.downloadurl()?.absolutestring{ self.imageurlstr = url } self.secondcall = dispatchworkitem{ [weak self] in var imagedict = [string:any]() imagedict.updatevalue(self.imageurlstr, forkey: "imageurlstr") self?.dbref.updatechildvalues(imagedict(), withcompletionblock: { (error, ref) in //if gets here in less 10 secs cancel problemcall secondcall self.problemcall?.cancel() //if reaches here self.weregoodnowdosomething() self?.someglobalclass.flag = false } } /*>>>>>>app gets shut here down before secondcall can perform<<<<<<*/ self.secondcall?.perform() //if secondcall doesn't complete in 20 secs cancel self.problemcall = dispatchworkitem{ [weak self] in self?.secondcall?.cancel() self?.someglobalclass.flag = false return } dispatchqueue.main.asyncafter(deadline: .now() + 20, execute: self.problemcall!) } self.firstcall.perform() //if firstcall doesn't run in 10 secs cancel self.problemcall = dispatchworkitem{ [weak self] in dispatchqueue.main.async { self?.firstcall?.cancel() self?.someglobalclass.flag = false //show alert error return } return } dispatchqueue.main.asyncafter(deadline: .now() + 10, execute: self.problemcall!) } }
how handle app getting shut down between steps 6 , 7 while firstcall completes, secondcall running never completes , problemcall never fires because app shut down?
the issue have image data sitting inside fb storage, url string never got sent database, , global flag set true when never reached point change false.
btw in callback excluded if error != nil. i'd cancel problemcall there, set global flag false, , show alert.
Comments
Post a Comment