Reputation: 659
I try to fetch all GIF in Photos in async block like this:
DispatchQueue.main.async {
allImageFetchResult.enumerateObjects(options: .concurrent, using: {(asset, _, _) in
if asset.isGIF {
assetSet.insert(asset)
}
})
}
But, it seems block user interface, the interface doesn't response to my touch actions until the block task is complete.
Upvotes: 0
Views: 335
Reputation: 3013
Make completion handler to get data once fetching is done.Like below check it out..
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
self.fetchAllPhotos{ data in
print(data)
dispatch_async(dispatch_get_main_queue()) {
// update the user interface..
print("updating UI")
}
}
}
func fetchAllPhotos(completion: (data:PHAsset) -> ())
{
allImageFetchResult.enumerateObjects(options: .concurrent, using: {(asset, _, _) in
if asset.isGIF {
assetSet.insert(asset)
}
})
completion(assetSet)
}
Upvotes: 0
Reputation: 518
DispatchQueue.main.async allows the UI updates to be on the main thread asynchronously. When you enumerate the objects, you are doing a synchronous task. See https://developer.apple.com/documentation/photos/phfetchresult/1620998-enumerateobjects
Discussion: This method executes synchronously.
So what you should do is enumerate beforehand, and only update the UI in the DispatchQueue.
Upvotes: 1
Reputation: 2714
Here you are fetching GIFs from the main thread and that's the reason why your UI is not responding to touches. Instead of main queue use DispatchQueue.global fetch GIFs from background thread. Try using
DispatchQueue.global().async {
allImageFetchResult.enumerateObjects(options: .concurrent, using: {(asset, _, _) in
if asset.isGIF {
assetSet.insert(asset)
}
})
}
Upvotes: 1
Reputation: 3802
The UI is non-responsive because you are fetching your data on the UI thread (main queue). If you want your UI to continue to be responsive, you need to move the fetch code onto another thread (probably background queue)
Upvotes: 0