         super.init()
     }
     
+    deinit {
+//        photosManager.stopCachingImagesForAllAssets()
+    }
+    
     func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
-        return assets.count
+        if let assets = assets {
+            return assets.count
+        }
+        
+        return 0
     }
     
     func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

            photosManager.cancelImageRequest(PHImageRequestID(cell.tag))
        }
        
        if let imageSize = imageSize {
            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() {
        photosManager.stopCachingImagesForAllAssets()
    }
    
    private func startCaching() {
        if let imageSize = imageSize {
            photosManager.startCachingImagesForAssets(self.assets, targetSize: imageSize, contentMode: imageContentMode, options: nil)
        }
    }
}

import Photos

internal class PhotosDataSource : NSObject, UICollectionViewDataSource {
    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()
        }
    }
    
    override init() {
