The documentation for UIImageWriteToSavedPhotosAlbum is sparse when it comes to whether it will block or not. I understand that if you pass completion selectors in, it will return immediately and run to completion asynchronously, calling the given selectors when complete. My question is, if I pass nil for the completion selectors, will it complete before returning?
I believe the answer is Yes, however I would love to know for sure.
For reference, here is the stack of the function call on a background thread:
Thread 7:
0 libsystem_kernel.dylib 0x3304a060 semaphore_wait_trap
1 libdispatch.dylib 0x3264644a _dispatch_thread_semaphore_wait$VARIANT$up
2 libdispatch.dylib 0x326446ae _dispatch_barrier_sync_f_slow
3 libdispatch.dylib 0x3264462c dispatch_barrier_sync_f$VARIANT$up
4 libdispatch.dylib 0x32644288 dispatch_sync_f$VARIANT$up
5 CoreData 0x3536417c -[NSManagedObjectContext performBlockAndWait:]
6 CoreData 0x352fcf30 -[NSManagedObjectContext setPersistentStoreCoordinator:]
7 PhotoLibraryServices 0x31f28cb8 -[PLManagedObjectContext initWithConcurrencyType:useSharedPersistentStoreCoordinator:]
8 PhotoLibraryServices 0x31f28ae4 +[PLManagedObjectContext contextForPhotoLibrary:]
9 PhotoLibraryServices 0x31f28706 -[PLPhotoLibrary(Protected) loadDatabase]
10 PhotoLibraryServices 0x31ef220e -[PLPhotoLibrary initWithPath:canTriggerDatabaseUpdate:]
11 PhotoLibraryServices 0x31f3777e __42+[PLSharedPhotoLibrary sharedPhotoLibrary]_block_invoke_0
12 libdispatch.dylib 0x32646570 dispatch_once_f$VARIANT$up
13 PhotoLibraryServices 0x31f3773c +[PLSharedPhotoLibrary sharedPhotoLibrary]
14 PhotoLibraryServices 0x31ef9d5c __withSavedPhotosAlbumUUID_block_invoke_0
15 libdispatch.dylib 0x32646570 dispatch_once_f$VARIANT$up
16 PhotoLibraryServices 0x31ef9124 withSavedPhotosAlbumUUID
17 PhotoLibraryServices 0x31ef9066 PLSaveImageToCameraRoll
18 UIKit 0x333cf5e6 UIImageWriteToSavedPhotosAlbum
Of course it’s asynchronous. The completion arguments are only optional because you may not care to be notified. But no, there’s no way to make this operation synchronous, nor would that even be a good idea. Locking up your main thread for an indeterminate amount of time is bad.