Hi,
I'm working on a custom framework to copy files asynchronously with FileManger.
I use NSOperation and NSOperationQueue like in this article.
I build all my CopyOperations in a loop where I pass the srcPath, dstPath and other parameters, set the dependency to the next operation to the previous and add the operations to a CopyFilesOperationQueue which inherit from NSOperationQueue :
_theCopyFilesOperationQueue = CopyFilesOperationQueue()
_theCopyFilesOperationQueue?.name = "exportFiles"
var dp: FileOperation?
let fm: FileManager = FileManager.default
for i in 0...theTracksList.count - 1 {
let theTrack: Track = theTracksList[i]
let file: URL = theTrack.location!
let theSourcePathStr: String = file.path
let theDestinationPattern: String = _getDestinationPattern(theTrack: theTrack, fileNameType: theFileNameType)
let theDestinationPathStr: String = exportTo.path + "/" + theDestinationPattern
let theCopyFileOperation: FileOperation = FileOperation(fileManager: fm, srcPath: theSourcePathStr, dstPath: theDestinationPathStr, ifFileAlreadyExistsType: theChosenIfFileAlreadyExistsType)
theCopyFileOperation.index = i
theCopyFileOperation.addObserver(self, forKeyPath: Operation.FINISHED, options: .new, context: nil)
theCopyFileOperation.addObserver(self, forKeyPath: Operation.EXECUTING, options: .new, context: nil)
theCopyFileOperation.operationQueue = _theCopyFilesOperationQueue
if dp != nil {
theCopyFileOperation.addDependency(dp!)
}
_theCopyFilesOperationQueue!.addOperation(theCopyFileOperation)
dp = theCopyFileOperation
}
I add Observers to know when an operation is finished to refresh the UI but the same crash appears when I disable the Observers so it is not about that...
Any idea ?
Thx.