Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop a currently running writeToFile:

Tags:

cocoa

I have some code which writes some PDFDocument objects to a user-chosen destination. This works fine.

Perhaps some of these files (chosen by the user) may be pretty large (maybe hundreds of megabytes) and now I wonder whether there is a possibility to cancel the current writeToFile:withOptions: call (e.g. the user changed his mind and wants to stop it).


1 Answers

I doubt you can do it with that method, since it provides no canceling functionality.

I suggest you use the dataRepresentation method of PDFDocument to first get the PDF data. You can then split up the data using NSData’s subdataWithRange:. And then you can successively write out the data to a file using NSFileHandle’s fileHandleForWritingToURL:error:, writeData:, and closeFile methods.

Writing it out in chunks like this from a non-main thread, in a for-loop say, you can cancel it any time you wish.

like image 170
Shlok Datye Avatar answered Dec 24 '25 11:12

Shlok Datye