Does UIGraphicsEndPDFContext flush and close the context file?

I'm getting infrequent crashes when I try to show a newly created PDF. The PDF is file based, and shortly after UIGraphicsEndPDFContext its shown.

The crash reports make it appear that the file itself is being mutated after its displayed.

So my question: is the file (self.filePath) absolutely flushed and closed when UIGraphicsEndPDFContext returns?

If not, is there some way to detect when it has finished?

Thanks!

David

`func addPageNumbers() { let url = URL(fileURLWithPath: filePath) guard let document = CGPDFDocument(url as CFURL) else { return }

// You have to change the file path otherwise it can cause blank white pages to be drawn.
self.filePath = "\(filePath)-final.pdf"

UIGraphicsBeginPDFContextToFile(filePath, .zero, nil)

let pageCount = document.numberOfPages
for i in 1...pageCount {
    ...
 }
}

UIGraphicsEndPDFContext()
}

Accepted Reply

In the end I wrote some code that grabbed the last file modification date just before the end context, then immediately after, and once very 0.1 seconds for a minute. It never changed from the second read (immediately after the EndPDFContext).

I ran a slew of tests using different files being created, some tens of megabytes.

Thus I infer that the file is not modified after UIGraphicsEndPDFContext.

Replies

In the end I wrote some code that grabbed the last file modification date just before the end context, then immediately after, and once very 0.1 seconds for a minute. It never changed from the second read (immediately after the EndPDFContext).

I ran a slew of tests using different files being created, some tens of megabytes.

Thus I infer that the file is not modified after UIGraphicsEndPDFContext.