Commit e57f2e6
Fix iOS PHImageManager crash from QoS background queue access (#1324)
PHImageManager methods were being called from QoS background queues (`QOS_CLASS_USER_INTERACTIVE`, etc.), violating Apple's thread safety requirements and causing crashes with `objc_storeWeak` deadlocks.
**Changes**
Dispatch all PHImageManager/PHCachingImageManager method calls to main queue:
- Image requests: `requestImageForAsset`, `requestAVAssetForVideo`
- Cache operations: `startCachingImagesForAssets`, `stopCachingImagesForAllAssets`
- Cancellation: `cancelImageRequest`
**Example**
Before (crashes on background queue):
```objc
- (void)fetchThumb:(PHAsset *)asset {
PHImageRequestID requestId = [self.cachingManager requestImageForAsset:asset ...];
}
```
After (safe main thread execution):
```objc
- (void)fetchThumb:(PHAsset *)asset {
dispatch_async(dispatch_get_main_queue(), ^{
PHImageRequestID requestId = [self.cachingManager requestImageForAsset:asset ...];
});
}
```
**Performance Impact**
No UI blocking occurs because PHImageManager API calls are non-blocking - they return immediately after submitting the request. Only the lightweight API call submission is dispatched to the main queue; the actual image processing work happens asynchronously on PHImageManager's own background threads.
QoS prioritization remains intact—background work still executes on appropriate queues, only the PHImageManager API calls are marshaled to the main thread.
---------
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: Alex Li <[email protected]>1 parent 08752fa commit e57f2e6
2 files changed
+204
-166
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
| 33 | + | |
| 34 | + | |
33 | 35 | | |
34 | 36 | | |
35 | 37 | | |
| |||
0 commit comments