ios - Crash and Received memory warning -
i have below loop download files server ipad 3 device. running hundred of files, got error , app crash. console shown me "received memory warning. same logic running on ipad air passed. can adivse how resolve problem.
ipad 3 -> ios 9.3.5 ipad air -> ios 10.3.3
func download() { (index, subjson): (string, json) in serverjson! { (_, subjson): (string, json) in subjson { let filepath = subjson["path"].stringvalue let nupdated = subjson["updated"].stringvalue if let oupdated = localjson?[index].array?.filter({ $0["path"].string == filepath}).first?["updated"].stringvalue { if (oupdated == nupdated) { dispatchqueue.main.async { () -> void in self.progressview.progress = float(self.count) / float(self.totalcount) } count += 1 continue } } var abspath = filepath let stridx = abspath.index(abspath.startindex, offsetby: 2) if (abspath.hasprefix("./")) { abspath = abspath.substring(from: stridx) } let sourceurl = url(string: self.sourceurl.appending(abspath)) { let filedata = try nsdata(contentsof: sourceurl!, options: nsdata.readingoptions()) try writefiletolocal(abspath, filedata) } catch { print(error) } dispatchqueue.main.async { () -> void in self.progressview.progress = float(self.count) / float(self.totalcount) } //print("path: \(abspath)") //print("%: \(float(count) / float(totalcount))") count += 1 } } { // remove temp json file first if exists. if filemanager.fileexists(atpath: oldmanifestpath) { try? filemanager.removeitem(atpath: oldmanifestpath) } // write temp json file local. try data?.write(to: oldmanifesturl) self.defaults.set(hashes, forkey: "lasthash") } catch { print(error) } dispatchqueue.main.async { self.progressview.ishidden = true self.changeviewproperties(2) } } } private func writefiletolocal(_ url:string, _ data:nsdata) throws { let url = url(string: url) let path = url?.deletinglastpathcomponent().relativepath let file = url?.lastpathcomponent let documentspath = nsurl(fileurlwithpath: nssearchpathfordirectoriesindomains(.documentdirectory, .userdomainmask, true)[0]) let filepath = documentspath.appendingpathcomponent(path!) var isdir: objcbool = false if !filemanager.default.fileexists(atpath: (filepath?.path)!, isdirectory:&isdir) { try filemanager.default.createdirectory(atpath: filepath!.path, withintermediatedirectories: true, attributes: nil) } filemanager.default.changecurrentdirectorypath((filepath?.path)!) try data.write(tofile: file!, options: .atomicwrite) print("update: \(filepath!), \(file!)") filemanager.default.changecurrentdirectorypath(documentspath.path!) }
then call function "download" in dispatchqueue.
dispatchqueue.global().async { self.downloadfiles() }
Comments
Post a Comment