Alamofire download in MacOS. Looks fine, progresses is working, but no file? - swift

I have Alamofire added through cocoapods and I have a method going to download a zip file (approx 50Mb).
While downloading everything looks perfect. I can see in Activity Monitor that 50Mb is downloaded for my app, I can see the progressbar zinging across. But I can never find the file.
Right now I have it set to use the current directory, but have tried others just in case. I have even searched the entire drive by data modified and never find anything.
Here is my code.
func downloadAndInstall(){
log.info("Downloading and Installing.....")
displayToUser(content: "Downloading and Installing.....")
let urlString = updatePackageURL //(This is http://xxxx.com/xxxxpackage.zip)
let fileManager = FileManager.default
currentDir = fileManager.currentDirectoryPath
let fileURL: URL = URL(string: currentDir + "/package.zip")!
let destination: DownloadRequest.DownloadFileDestination = { _, _ in (fileURL, []) }
log.info("FILEURL: \(fileURL)")
var progressValues: [Double] = []
var response: DefaultDownloadResponse?
Alamofire.download(urlString, to: destination)
.downloadProgress { progress in
progressValues.append(progress.fractionCompleted)
log.info("Latest Progress Value: \(progress.fractionCompleted)")
self.progBar.doubleValue = progress.fractionCompleted
}
.response { resp in
response = resp
if progressValues.last != 1.0 {
//backout of the process, something went wrong
log.debug("Something went wrong downloading the file. Close and try again.")
self.displayToUser(content: "Something went wrong downloading the file. Close and try again.")
self.exitpoorly()
}
else{
log.info("Download Finished")
self.displayToUser(content: "Download Finished")
self.extractpackage()
}
}
var previousProgress: Double = progressValues.first ?? 0.0
for progress in progressValues {
previousProgress = progress
}
if let lastProgressValue = progressValues.last {
log.info("Current Download Value: \(lastProgressValue, 1.0)")
} else {
//Fail
}
}

I'd suggest checking for any errors, e.g.:
Alamofire.download(urlString, to: destination)
.downloadProgress { progress in
...
}
.response { response in
guard response.error == nil else {
//backout of the process, something went wrong
log.debug("Something went wrong downloading the file. Close and try again.")
log.debug(response.error!.localizedDescription)
...
self.exitpoorly()
return
}
log.info("Download Finished")
...
}
Maybe the app is sandboxed, or perhaps you don't have permissions for that folder. It's hard to say without seeing the error.

Related

How to print progress with ZIPFoundation

How can I track and print progress percentage to console on each progress update using ZIPFoundation in a class file that is not a controller?
I am quite new to swift and I have been trying to understand how Progress works for quite a long time because it seems like it can be used to track the progress of unzip process using ZIPFoundation module (unfortunately without a luck so far). For the info, the unzipping itself works fine for me.
As far as I know it should be something similar to this, but I am not sure how to track it:
class MyClass {
#objc var unzipProgress: Progress?
func unzipFile(from: URL, to: URL) -> Bool {
let fileManager = FileManager()
print("Unzipping...")
do {
// How to track unzipProgress?
try fileManager.unzipItem(at: from, to: to, progress: unzipProgress)
print("Extraction success from \(from.path) to \(to.path)")
} catch {
print("Extraction of ZIP archive failed with error:\(error)")
}
}
Edit: I am familiar with this post but it does not help me as it uses JGProgressHUD and a controller class.
The documentation for ZIPFoundation tells you how to monitor progress.
You need to create a Progress object:
let unzipProgress = Progress()
Then you need to setup an observer for the fractionCompleted property:
let observation = unzipProgress.observe(\.fractionCompleted) { progress, _ in
print("progress: ", progress.fractionCompleted)
}
Then pass in your unzipProgress to one of the zip methods:
try fileManager.unzipItem(at: from, to: to, progress: unzipProgress)
Then you can cleanup the observation:
observation.invalidate()
Your unzipFile function becomes something like this:
func unzipFile(from: URL, to: URL) -> Bool {
let fileManager = FileManager()
let unzipProgress = Progress()
let observation = unzipProgress.observe(\.fractionCompleted) { progress, _ in
print("Extraction progress: ", progress.fractionCompleted)
}
print("Unzipping...")
do {
// How to track unzipProgress?
try fileManager.unzipItem(at: from, to: to, progress: unzipProgress)
print("Extraction success from \(from.path) to \(to.path)")
} catch {
print("Extraction of ZIP archive failed with error:\(error)")
}
observation.invalidate()
}
You can remove your unzipProgress property from your class since you only need the local variable.

SwiftUI and Firebase - Stream error: 'Not found: No document to update:

So, I have a program that, when it opens, looks for a specific document name in a specific collection (both specified) and, when it is found, copies the document name and starts a listener. If it doesn't find the document name after 5 x 5 second intervals, the app stops. For some reason, when I run the code, after it does the first check I get about a thousand writes of this error:
[Firebase/Firestore][I-FST000001] WriteStream (7ffcbec0eac8) Stream error: 'Not found: No document to update:
Here's the code I'm using to call firestore:
let capturedCode: String? = "party"
.onAppear(perform: {
Timer.scheduledTimer(withTimeInterval: 5, repeats: true) { timer in
print("running code check sequence")
if let code = capturedCode {
calcCloud.checkSessionCode(code)
if env.doesCodeExist {
print("code found! applying to environment!")
env.currentSessionCode = code
calcCloud.watchCloudDataAndUpdate()
allClear(env: env)
timer.invalidate()
}
else if timerCycles < 5 {
timerCycles += 1
print("code not found, this is cycle \(timerCycles) of 5")
} else {
print("could not find document on firebase, now committing suicide")
let x = ""
let _ = Int(x)!
}
}
}
})
here is the code I'm using to check firebase:
func checkSessionCode(_ code: String) {
print("checkSessionCode running")
let docRef = self.env.db.collection(K.sessions).document(code)
docRef.getDocument { (document, error) in
if document!.exists {
print("Document data: \(document!.data())")
self.env.doesCodeExist = true
} else {
print("Document does not exist")
self.env.doesCodeExist = false
}
}
}
and here is the code that should be executed if the code is found and applied:
func watchCloudDataAndUpdate() {
env.db.collection(K.sessions).document(env.currentSessionCode!).addSnapshotListener { (documentSnapshot, error) in
guard let document = documentSnapshot else {
print("Error fetching snapshot: \(error!)")
return
}
guard let data = document.data() else {
print("Document data was empty.")
return
}
Where did I go wrong, and what is this error all about...thanks in advance :)
EDIT: For clarity, it seems that the errors begin once the onAppear finishes executing...
This is why I need to stop coding after 1am...on my simulator, I deleted my app and relaunched and everything started working again...sometimes the simplest answers are the right ones...

File couldn’t be opened because you don’t have permission to view it error

I have Googled and poked around Stack Overflow and can't seem to find a solution for this. I have:
let fileURL = URL( string: "file:///Users/me/file.txt" )
var rawDataString: String
var errorString: String?
do {
rawDataString = try String( contentsOf: fileURL!, encoding: String.Encoding.utf8 )
} catch let error as NSError {
errorString = error.description
print( errorString! )
return
}
and it's erroring out with
Error Domain=NSCocoaErrorDomain Code=257 "The file “file.txt” couldn’t
be opened because you don’t have permission to view it."
Permissions are read for all users:
$ ls -al file.txt
-rw-r--r--# 1 me staff 348306 Dec 13 2016 file.txt
Any ideas would be most welcome.
Anyone coming across this thread, #LeoDabus pointed me to where to turn off sandbox, which worked:
He also cleaned up my code a bit:
let fileURL = URL( fileURLWithPath: "/Users/me/file.txt" )
var rawDataString: String
var errorString: String?
do {
rawDataString = try String( contentsOf: fileURL, encoding: .utf8 )
} catch let error as NSError {
errorString = error.description
rawDataString = ""
return
}
(For iOS)
Sadly #Dribbler´s answer didn't work for me because I didn't have App Sandbox enabled and it still didn't work. In my case I used the UIDocumentPickerViewController and I was unable to access the file.
Adding url.startAccessingSecurityScopedResource() before working with the file resolved the issue for me.
Here is an example of the didPickDocumentsAt delegate function from a UIDocumentPickerViewController:
guard let url = urls.first else {
return
}
guard url.startAccessingSecurityScopedResource() else { // Notice this line right here
return
}
do {
let data = try Data(contentsOf: url)
} catch let error {
print(error.localizedDescription)
}
After adding that line it worked for me.
I actually was not able to get the preferred answers above to work for me in swift playground.
Another solution is just to create a command line app in Xcode. Then paste the above and it should work fine.

AVMIDIPlayer DLSBankManager::AddBank: Bank load failed

When I use AVMIDIPlayer to play a MusicSequence with only one note message. Most of times it works fine but sometimes it has no sound and logged as below:
DLSBankManager::AddBank: Bank load failed
Error Domain=com.apple.coreaudio.avfaudio Code=-10871 "(null)"
It works well on iOS9, but when i test it on iOS10 it runs into this issue.
I'm sure that the sf2 sound bank file url is set properly.
I paste the code as below:
func playAVMIDIPlayerPreview(_ musicSequence:MusicSequence) {
guard let bankURL = Bundle.main.url(forResource: "FluidR3 GM2-2", withExtension: "sf2") else {
fatalError("soundbank file not found.")
}
var status = OSStatus(noErr)
var data:Unmanaged<CFData>?
status = MusicSequenceFileCreateData (musicSequence,
MusicSequenceFileTypeID.midiType,
MusicSequenceFileFlags.eraseFile,
480, &data)
if status != OSStatus(noErr) {
print("bad status \(status)")
}
if let md = data {
let midiData = md.takeUnretainedValue() as Data
do {
try self.midiPlayerPreview = AVMIDIPlayer(data: midiData, soundBankURL: bankURL)
} catch let error as NSError {
print("Error \(error)")
}
data?.release()
self.midiPlayerPreview?.play({ () -> Void in
self.midiPlayerPreview = nil
self.musicSequencePreview = nil
})
}
}
The error is occur on this line:
try self.midiPlayerPreview = AVMIDIPlayer(data: midiData, soundBankURL: bankURL)
Try setting the global variable errno to 0 errno = 0 before loading the soundfont with
try self.midiPlayerPreview = AVMIDIPlayer(data: midiData, soundBankURL: bankURL)
We experienced the same issue and at the same time this one.
So we tried to apply the fix of the other issue to this one and it just worked.

keep track of already downloaded files with `alamofire`

how can i keep a track or record of already downloaded files in alamofire swift 2.1 so that i don t have to download the same file again ? do we have any native method for that provided by alamofire or we have to do a check before downloading any file on our directory if we already have file with that name there ???? i'm confused on how to accomplish this with a proper approach
if anybody would clear my confusion about this then it'll be so helpful for me thanks
UPDATE:
let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
let fileUrl = documentsURL.URLByAppendingPathComponent(suggestedFileName)
print(fileUrl)
if !(NSFileManager.defaultManager().fileExistsAtPath(fileUrl.path!)){
self.suggestedFileName = (self.request?.response?.suggestedFilename)! // here how can i get the suggested download name before starting the download preocess ???
print("\(destination)")
request = Alamofire.download(.GET, "http://contentserver.adobe.com/store/books/GeographyofBliss_oneChapter.epub", destination: destination)
.progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
print(totalBytesRead)
// This closure is NOT called on the main queue for performance
// reasons. To update your ui, dispatch to the main queue.
dispatch_async(dispatch_get_main_queue()) {
print("Total bytes read on main queue: \(totalBytesRead)")
self.progressView.setProgress(Float(totalBytesRead) / Float(totalBytesExpectedToRead), animated: true)
}
}
.response { _, _, _, error in
if let error = error {
print("Failed with error: \(error)")
} else {
print("Downloaded file successfully")
}
}
}else {
print("file already exists")
}
in the above update am trying to get the suggestedFileName which is generated by alamofire but there's one problem when am trying to get sugestedFileName like this : suggestedFileName = (request?.response?.suggestedFilename)! in viewdidload am getting a null exception off course because there's no suggestedFileName because download not yet started so my question is that how can i get the suggestedFileName from response before starting the download ??
According to the docs https://github.com/Alamofire/Alamofire#downloading, you can download to a file. If your file destinations names are predictable, you could simply check to see if the contents of the file exists. For example if your are downloading data:
if let data = NSData(contentsOfURL: yourDestinationURL) {
//Do your stuff here
}
else {
//Download it
}
If you want consistency between names I suggest you avoid the Alamofire suggested destination and do this instead:
let path = NSFileManager.defaultManager().URLsForDirectory(.ApplicationSupportDirectory, inDomains: .UserDomainMask)[0] as NSURL
let newPath = path.URLByAppendingPathComponent(fileName)
Alamofire.download(.GET, "https://httpbin.org/stream/100", destination: { _ in
newPath //You have to give the destination in this closure. We could say 'return newPath' instead, they're the same thing.
})
.progress({ _ in
//progress stuff
})
.response { _, _, data, _ in
//Handle response once it's all over
}