 import Photos
 
 internal class PhotosDataSource : NSObject, UICollectionViewDataSource {
-    internal var imageSize: CGSize? {
-        willSet { stopCaching() }
-        didSet { startCaching() }
+    internal var imageSize: CGSize = CGSize(width: 0, height: 0) {
+        willSet {
+            if CGSizeEqualToSize(newValue, imageSize) == false {
+                stopCaching()
+            }
+        }
+        didSet {
+            if CGSizeEqualToSize(oldValue, imageSize) == false {
+                startCaching()
+            }
+        }
     }
     
     private let photoCellIdentifier = "photoCellIdentifier"
     private let photosManager = PHCachingImageManager()
     private let imageContentMode: PHImageContentMode = .AspectFill
-    private var assets: [PHAsset] = [] {
-        willSet { stopCaching() }
-        didSet { startCaching() }
+    private var assets: [PHAsset]? {
+        willSet {
+            stopCaching()
+        }
+        didSet {
+            startCaching()
+        }
     }
     
     override init() {

        super.init()
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return assets.count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

            photosManager.cancelImageRequest(PHImageRequestID(cell.tag))
        }
        
        if let assets = assets {
            let asset = assets[indexPath.row]
            cell.tag = Int(photosManager.requestImageForAsset(asset, targetSize: imageSize, contentMode: imageContentMode, options: nil) { (result, _) in
                cell.imageView.image = result
            })
        }
        
        return cell
    }
    
    private func stopCaching() {
//        if let assets = assets {
//            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
//                self.photosManager.stopCachingImagesForAssets(assets, targetSize: self.imageSize, contentMode: self.imageContentMode, options: nil)
//            }
//        }
    }
    
    private func startCaching() {
//        if let assets = assets {
//            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
//                self.photosManager.startCachingImagesForAssets(assets, targetSize: self.imageSize, contentMode: self.imageContentMode, options: nil)
//            }
//        }
    }
}
