UICollectionView is not scrolling smooth on reload data
You should only update your UI from the main thread, so do this instead...
Swift 2.x :
dispatch_async(dispatch_get_main_queue()), {
self.DataTableCollectionView.reloadData()
}
Swift 3 :
DispatchQueue.main.async {
self.DataTableCollectionView.reloadData()
}
If that doesn't solve the problem then you may ALSO need to call loadMoreData()
on a background thread...
Swift 2.x :
dispatch_async(dispatch_get_global_queue(QOS_CLASS_UTILITY, 0)){
self.loadMoreData()
}
Swift 3 :
DispatchQueue.global(qos: DispatchQoS.QoSClass.default).async {
self.loadMoreData()
}
It is unclear exactly how your code works since you have not shown us your loadMoreData()
function. It actually seems thats you may be meaning to call DataTableCollectionView.reloadData()
from inside loadMoreData
, but I cannot tell without seeing the function. Please add the additional code
Collection view not scroll smoothly when fetch data from firebase using Swift?
I found the solution for my bug. Seem the collectionView is reload a lot of time depend on number of sending message. So the solution is quite easy, just remove the collectionView reload data
Not scrolling smooth on UICollectionViewController for large data loading
Do not reload collectionView
in cellForItem
, delete this part:
DispatchQueue.main.async {
self.collectionView?.reloadData()
}
And delete most of those reloadData
calls that you have there. It's a heavyweight operation - call it only when you really need to reload it - meaning when the data model changes for the whole collection view.
Sidenote:
Don't use indexPath.row
, but rather indexPath.item
, row
is used for UITableView
s, not for UICollectionView
s.
UICollectionview Scrolling choppy when loading cells
So anybody having scrolling issues should do this
add these 2 lines after your dequeue
cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
CollectionView in TableViewCell scroll isn't smooth in swift3
Download Image Url Data in collectionView: UICollectionView, cellForItemAt indexPath: IndexPath
delegate - ASYNCHRONOUSLY
Create a Class
First cache the image Data with respect to key Image URL
Why do we need to cache the data?
Because - While scrolling we don't need to download Image Every Time so Cache the already downloaded Image Data and return the cache data
I created a class name: AsyncImageLoader
then I created a function which is a completion handler which returns the data after completion of downloading Image
class AsyncImageLoader {
static let cache = NSCache<NSString, NSData>()
class func image(for url: URL, completionHandler: @escaping(_ image: UIImage?) -> ()) {
DispatchQueue.global(qos: DispatchQoS.QoSClass.background).async {
if let data = self.cache.object(forKey: url.absoluteString as NSString) {
DispatchQueue.main.async { completionHandler(UIImage(data: data as Data)) }
return
}
guard let data = NSData(contentsOf: url) else {
DispatchQueue.main.async { completionHandler(nil) }
return
}
self.cache.setObject(data, forKey: url.absoluteString as NSString)
DispatchQueue.main.async { completionHandler(UIImage(data: data as Data)) }
}
}
}
How to use AsyncImageLoader Class?
See below How I used in collectionView: UICollectionView, cellForItemAt indexPath: IndexPath
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let yourCell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCollectionCell", for: indexPath) as! CustomCollectionCell
yourCell.url = URL(string: "\(item_ImageUrl)")
return yourCell
}
CustomCollectionCell
class CustomCollectionCell: UICollectionViewCell {
@IBOutlet weak var cellImageDisplayView: UIImageView!
var url: URL? {
didSet {
if let url = url {
cellImageDisplayView.image = nil
AsyncImageLoader.image(for: url, completionHandler: { (image) in
DispatchQueue.main.async {
if let img = image {
self.cellImageDisplayView.image = img
}else{
let dummyImage = UIImage(named: "img_u")
self.cellImageDisplayView.image = dummyImage
}
}
})
}else{
let dummyImage = UIImage(named: "img_u")
self.cellImageDisplayView.image = dummyImage
}
}
}
}
Related Topics
Implementing Ignoredproperties() on Both a Object Subclass and Its Own Subclass
Swift Sha256 Encryption Returns Different Encrypted String Compare to Objective C
Implement a Crosshair Kind Behaviour in Realitykit
Swiftui Wrapping Hstack with Images
Why Does a Simple Swift Arithmetic Operation Compile So Slow
Swift Flatmap on Array with Elements Are Optional Has Different Behavior
How to Put Gamecenter on Application with Swift
Typecase Regular Swift Function to Curry Function
What's The Correct Number Type for Financial Variables in Swift
Invalid Redeclaration of Typealias
Kvo Listener Issues in Swift 4
How to Use Gpx File for UI Tests Only
Pass and Print All Cases in an Enum in Swift
Public Default Init in Protocol
Swiftui Map Overlays Without UIviewrepresentable
Convert Input Data to Integer in Swift
Swift Casting Generic to Optional with a Nil Value Causes Fatalerror