Non-Deprecated Way to Copy Files Async?

Is there a way to copy files asynchronously? NSFileManager's method for copying files doesn't provide a way for you to do this, and all the functions on FSFileOperationRef are deprecated. Is there a non-deprecated way to do this?


Thanks.

Replies

I don't think so, but you can just do the synchronous file copy operation on a background thread to avoid blocking the main thread (or whatever other thread you don't want to block). That's pretty much what the old API had to do internally.

Yeah, I was thinking of doing that. The FSCopyObjectAsync allows you to provide a callback function you can use to update UI (progess indicator). I guess I can use an indeterminate indicator if I'm copying 1 file using a file manager.

Well, there's a chance (just maybe) that the NSFileManager methods support NSProgress implicitly. That is, if there's a current progress object, it might add a child.

I don't believe NSFileManager methods support NSProgress...I don't particularly like the NSProgress's loose API design...to me it feels kind of ugly like informal protocols...but tangent aside..


Doing the NSFileManager stuff on a background thread seems to work okay, but when copying a really large file like a 1 hour video... just throwing an indeterminate progress indicator up with no feedback takes around two minutes for copyItemAtURL:toURL:error: to return so it's not a great experience... I might use the deprecated function for now...it's marked deprecated but available.


I don't really mess with Applescript so I'd have to look into it, but I wonder if I could just tell Finder to do the copying and handle the work for me since this is an OS X app.


Edit: Ended up searching around and found a stackoverflow post where someone suggessted to fire off a timer and check the current file size against the full file size....this seems to work well.


Now I set my progress bar max value to be the total number of bytes for all the files being copied and do the file manager copying on a background thread. Before firing the copy method, I set a timer off to check the file size of the destination URL.


Not really fun to write. I increment my progress bar by the amount the file size has grown each time my timer fires the method to do the check. I'll take what I can get though.

Could you please share the github link for the sample?