Platform notes
Main thread and background work, memory warnings and large documents, app size and deployment notes for NexPDF for iOS and macOS (Swift).
Threading
- Every NexPDF call is synchronous and runs on the thread that calls it. Opening a large file, rendering, OCR, signing and conversion can take long enough to drop frames or freeze the UI, so call them off the main thread.
- A
NexPdfDocumentis not thread-safe. Use each document from one queue or actor at a time. Different documents can be used on different threads at the same time. The state shared between documents (the license and the renderer's caches) is thread-safe. copy()gives an independent document you can hand to another queue, for example to render while the user edits the original.- The license is process-wide: one
activateLicensecall at launch covers every thread. - Errors are tracked per thread, and the SDK reads them right after each call, so an error on one queue never leaks into another.
Wrap a document in an actor (or a serial DispatchQueue) and come back to the main actor for the UI:
import NexPdf
import UIKit
actor PdfPages {
private let doc: NexPdfDocument
init(data: Data) throws { doc = try NexPdfDocument.open(data: data) }
func image(page: Int, dpi: Double) throws -> UIImage? {
UIImage(data: try doc.renderPagePng(pageIndex: page, dpi: dpi))
}
func trim() -> Int { doc.trimMemory() }
}
@MainActor
func show(page: Int, of pages: PdfPages, in imageView: UIImageView) async {
let dpi = 72 * imageView.traitCollection.displayScale
imageView.image = try? await pages.image(page: page, dpi: dpi)
}The reader already does this: it runs every engine call of its document on one
serial background queue and renders pages as they scroll into view. Its controller,
NexPdfReaderController, is @MainActor; call it from the main thread. Its open(data:name:password:)
parses the document before it returns, so very large files can pause the UI for a moment.
The reader's configuration and event handler are shared by the process: the last
NexPdfReaderView or NexPdfReaderViewController created sets them. Show one reader at a time.
Memory
- A document is read fully into memory when it is opened.
open(url:)reads the file intoDataand the engine keeps its own copy, so budget at least twice the file size while it opens, plus the decoded pages you touch. - A page rendered with
renderPagePngis PNG data; decoded into aUIImageit takes width x height x 4 bytes. A full A4 page at 216 dpi (a 3x screen at 100 % zoom) is about 1786 x 2526 pixels, or 18 MB. Render at the size you show, userenderThumbPng(pageIndex:maxDim:)for thumbnails, and keep only the pages on screen. - The renderer keeps process-wide caches of parsed fonts, glyphs and decoded images, shared by every document so a font or logo used on many pages is decoded once. Each cache is bounded by size and drops its least recently used entries first; together they can hold up to about 1.3 GB in a process that renders many large, image-heavy files. Memory that stays high after rendering is this cache being reused, not a leak.
memoryUsageBytes()estimates what a document uses.trimMemory()compacts its internal buffers and drops objects nothing refers to any more, and returns the bytes reclaimed. The document stays usable. Releasing the last reference to aNexPdfDocumentfrees it.
Call trimMemory() when the app receives a memory warning, on the document's own queue or actor:
NotificationCenter.default.addObserver(
forName: UIApplication.didReceiveMemoryWarningNotification, object: nil, queue: .main
) { _ in
Task { _ = await pages.trim() }
imageCache.removeAllObjects() // and drop your own rendered images
}The reader keeps its rendered pages in an NSCache, which the system can evict under memory
pressure. For undo it keeps a full copy of the PDF bytes for each of the last 25 edits, plus the
file as it was opened, so editing a very large document in the reader uses several times its size.
App size
NexPdfCore.xcframeworkis a static library. There is no framework to embed or sign: the linker copies the engine code your app uses into the app binary. The xcframework download is much larger than what ships, because a static archive holds unlinked object files. Measure the real cost in Xcode's App Thinning Size Report for your app.- Device builds link only the
ios-arm64slice. The simulator slices (arm64 and x86_64) are used for simulator builds only and never ship. - The engine compiles in the Liberation fonts (about 4 MB) used for non-embedded standard fonts and
PDF/A.
NexPdf.hasBundledFonts()tells you which build you have. - No OCR model is bundled. Neural OCR models you add to the app bundle count in full (a mobile detection model is about 2.4 MB, plus a recognition model per script); downloading them into Application Support on first use keeps the download small. See OCR.
NexPdfUIis Swift source compiled into your app. Leave the product out if you do not use the reader.
Deployment
- iOS 15 or later on iPhone and iPad, and macOS 13 or later on Apple silicon. Mac Catalyst, visionOS and Intel Macs have no slice in the xcframework.
- iOS keys are bound to the bundle id: each app, and each bundle id variant (for example a
.betabuild), needs a key issued for it. See License. - Bundle the license file as a resource, or fetch the key from your server at launch and pass it to
activateLicense. Activation is offline either way.
Network
The engine makes no network calls of its own: license keys are verified offline and documents
never leave the app. Only signing options you set (tsa_url for a timestamp, ltv for OCSP and CRL
lookups) and NexPdf.addLtv(data:) contact the servers named in the options and the certificates.