在Simulator中可运行的Documents文件夹,但不适用于iPad-Swift 2.1 [英] Documents folder working in Simulator but not iPad - Swift 2.1

查看:100
本文介绍了在Simulator中可运行的Documents文件夹,但不适用于iPad-Swift 2.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码将图像文件夹解压缩到我创建的文件夹中.然后循环遍历并将其添加到数组中,然后循环遍历此数组并将这些文件名检索到图像数组中.

The below code unzips a folder of images into a folder I create. and then loops through it and adds the names to an array, then loops through this array and retrieves those file names into an array of images.

它可以在模拟器上完美运行,但是当我在iPad上执行操作时,数据为空,它什么也不会打印出来.我只能假设该文件夹在解压缩完成之前不可访问或正在搜索,但是不应该使用该文件夹,因为我正在使用带有完成块的NSOperationQueue.

It works perfectly on the simulator, but the data is empty when I do it on the iPad, it prints out nothing. I can only assume the folder isn't accessible or is being searched before the unzip has completed, but It shouldnt as I am using NSOperationQueue with a completion block.

func unzipData(objectData: NSManagedObject) {
        var paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
        let documentsDir = paths[0]
        let zipPath = documentsDir.stringByAppendingString("MyZipFiles")
        let folderPath = documentsDir.stringByAppendingString("/docLibFiles") // My folder name in document directory
        var optData = NSData(data: objectData.valueForKey("image") as! NSData)
        print(objectData.valueForKey("imageUrl") as! String)
        optData.writeToFile(zipPath, atomically: true)
        let success = fileManager.fileExistsAtPath(zipPath) as Bool
        if success == false {
            do {
                try! fileManager.createDirectoryAtPath(folderPath, withIntermediateDirectories: true, attributes: nil)
            }
        }
        queue.addOperationWithBlock { () -> Void in
            let operation1 = NSBlockOperation(block: {
                let unZipped = SSZipArchive.unzipFileAtPath(zipPath, toDestination: folderPath)

            })
            operation1.completionBlock = {
                    dispatch_async(dispatch_get_main_queue(), {
                        if queue.operationCount == 0 {
                            self.retrieveFiles()
                        }
                    })
            }
            queue.addOperation(operation1)
        }
    }

    func getDocumentsURL() -> NSURL {
        let documentsURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
        return documentsURL
    }

    func fileInDocumentsDirectory(filename: String) -> String {
        let fileURL = getDocumentsURL().URLByAppendingPathComponent(filename)
        return fileURL.path!
    }

    func retrieveFiles() {
        var paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
        let documentsDir = paths[0]
        let zipPath = documentsDir.stringByAppendingString("MyZipFiles")
        let folderPath = documentsDir.stringByAppendingString("/docLibFiles") // My folder name in document directory
        do {
            let filelist = try fileManager.contentsOfDirectoryAtPath(folderPath)
            print(filelist)
            print("filename")
            for filename in filelist {
                fileNameArray.append(filename)
            }
        } catch let error as NSError  {
            print("Could not save \(error)")
        }
        do {
            for item in fileNameArray {
                print("item \(item)")
                let imagePath = fileInDocumentsDirectory("docLibFiles/\(item)")
                imageArray.append(UIImage(contentsOfFile: imagePath)!)
            }
            print("filename array \(fileNameArray)")
            print("image array \(imageArray)")
            unzipDelegate!.unzipSet(imageArray)
        }
    }

推荐答案

我不确定为什么会出现最初的问题,但是我的代码肯定太复杂了.我已经对它进行了排序,希望它能对其他2.1版的用户有所帮助

Im not exactly sure why the initial problem was there, but my code was definitely overcomplicated. Ive sorted it now, hopefully it will help someone else in swift 2.1

class UnzipDocument {
let queue = NSOperationQueue() // BACKGROUND THREAD
    var imageArray = [UIImage]()
    var fileNameArray = [String]()
    var fileManager = NSFileManager.defaultManager()

let compressedFile = NSTemporaryDirectory().stringByAppendingString("MyZipFiles")
let uncompressedFolder = NSTemporaryDirectory().stringByAppendingString("MyUnzippedFiles")

func unzipData(objectData: NSManagedObject){ // THIS IS BASICALLY NSDATA FROM CORE DATA FOR ME
    let optData = NSData(data: objectData.valueForKey("image") as! NSData)
    let success = fileManager.fileExistsAtPath(uncompressedFolder) as Bool // CREATES THE FOLDER IF IT DOESNT EXIST
    if success == false {
        do {
            try! fileManager.createDirectoryAtPath(uncompressedFolder, withIntermediateDirectories: true, attributes: nil)
        }
    }
    optData.writeToFile(compressedFile, atomically: true)
    queue.addOperationWithBlock { () -> Void in
        let operation1 = NSBlockOperation(block: {
            SSZipArchive.unzipFileAtPath(self.compressedFile, toDestination: self.uncompressedFolder)
        })
        operation1.completionBlock = {
            if queue.operationCount == 0 {
                dispatch_async(dispatch_get_main_queue(), {
                    if queue.operationCount == 0 {
                        self.retrieveFiles()
                    }
                })
            }
        }
        queue.addOperation(operation1)
    }
}

func retrieveFiles() {
    do {
        let filelist = try fileManager.contentsOfDirectoryAtPath(uncompressedFolder)
        print(filelist)
        print("filename")
        for filename in filelist {
            self.fileNameArray.append(filename)
        }
    } catch let error as NSError  {
        print("Could not save \(error)")
    }
    do {
        for item in fileNameArray {
            print("item \(item)")
            let imagePath = uncompressedFolder.stringByAppendingString("/\(item)")
            imageArray.append(UIImage(contentsOfFile: imagePath)!)
        }
        print("filename array \(fileNameArray)")
        print("image array \(imageArray)")
        unzipDelegate!.unzipSet(imageArray)
    } catch {
    }
} 
}

,并在使用后删除临时文件/文件夹,以便可以在没有任何旧文档的情况下重复使用

and to delete the temp file/folder after use, so that it can be reused without any old documents remaining

var fileManager = NSFileManager.defaultManager()
        let compressedFile = NSTemporaryDirectory().stringByAppendingString("MyZipFiles")
        let uncompressedFolder = NSTemporaryDirectory().stringByAppendingString("MyUnzippedFiles")
        do {
           try fileManager.removeItemAtPath(compressedFile)
              try fileManager.removeItemAtPath(uncompressedFolder)
        } catch let error as NSError  {
            print("Could not save \(error)")
        }

这篇关于在Simulator中可运行的Documents文件夹,但不适用于iPad-Swift 2.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆