Crash (EXC_BAD_ACCESS (code=2, address=0x6048a280aff8)) with OperationQueue

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.

Replies

Hi,


I thought about something : all FileManager function are synchronous but I created my FW with asynchronous operations...


So maybe it could produce that bug ?...


Moreover, is there a way to have asynchronous for this kind of task ? Because for the moment, I can only check when an operation is finished and not have its progression...


Thx.