Save large PDF file with UIDocument

Hi, I'm working with UIDocument to store a PDF file. This is code I use for saving a file.

document.close(completionHandler: { (success) in
            if success {
                document.save(to: url,
                              for: .forOverwriting,
                              completionHandler: { (success) in
                    if success {
                        print("Saved file successfully")
                    }
                })
            } 
        })

This code works well with a small file size. But if it's a large file like 100MB, it takes around 2-5 minutes to save the file. Is there a way to save file with the changes only? Or create an auto save function triggered whenever the user edit the file?

There isn't a way to update a PDF file, but the time it takes to write is going to be heavily correlated to how your generating the PDF.

For example if your generating a lot of images (which would also be the case if you did something like calling CALayer.render(in:) or UIView.drawViewHierarchy(in:afterScreenUpdates:) to draw the content) then it will indeed take a very long time to render and safe a large document. PDF is natively a vector graphics format and performs best when storing that kind of content.

The first thing to understand is what's actually taking up all that time. You're assuming here that the problem is actually writing the data to disk:

Is there any better way to handle this because every time I tried to close the file, it took really long time to save a 100MB file and the app block the main thread(probably because UIDocument is a library from UIKit)?

...but that is definitely not the case. These day, 100 MB just isn't very much data. Something like a REALLY slow USB thumb drive will still do 20+ MB/s, which means the I/O time is only going to be a few seconds.

The issue with here isn't writing the data itself:

So in a case, we need to store the PDF file, although the user only added 1 small update to the file like adding ink or an image annotation, we have to rewrite 100% the PDF file to store it on disk?

...it's generating the data you're going to write out. That's what he was talking about here:

For example if your generating a lot of images (which would also be the case if you did something like calling CALayer.render(in:) or UIView.drawViewHierarchy(in:afterScreenUpdates:) to draw the content) then it will indeed take a very long time to render and safe a large document. PDF is natively a vector graphics format and performs best when storing that kind of content.

That's just a simple example of the broader issue, which is that you need to speed up the process of actually generating the data you're writing to disk.

__
Kevin Elliott
DTS Engineer, CoreOS/Hardware

Save large PDF file with UIDocument
 
 
Q