Skip to content

Commit

Permalink
Fixed photoLibraryDidChange crash.
Browse files Browse the repository at this point in the history
UICollectionView doesn't like when we initialize a new data source.
So instead just update the fetch result on the data source.
  • Loading branch information
mikaoj committed Apr 23, 2020
1 parent 7bcf1b6 commit 0d72b2d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Sources/Scene/Assets/AssetsCollectionViewDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class AssetsCollectionViewDataSource : NSObject, UICollectionViewDataSource {
private static let videoCellIdentifier = "VideoCell"

var settings: Settings!
var fetchResult: PHFetchResult<PHAsset>

private let fetchResult: PHFetchResult<PHAsset>
private let imageManager = PHCachingImageManager.default()
private let durationFormatter = DateComponentsFormatter()
private let store: AssetStore
Expand Down
13 changes: 5 additions & 8 deletions Sources/Scene/Assets/AssetsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,23 @@ protocol AssetsViewControllerDelegate: class {
class AssetsViewController: UIViewController {
weak var delegate: AssetsViewControllerDelegate?
var settings: Settings! {
didSet { dataSource?.settings = settings }
didSet { dataSource.settings = settings }
}

private let store: AssetStore
private let collectionView: UICollectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
private var fetchResult: PHFetchResult<PHAsset> = PHFetchResult<PHAsset>() {
didSet {
dataSource = AssetsCollectionViewDataSource(fetchResult: fetchResult, store: store)
}
}
private var dataSource: AssetsCollectionViewDataSource? {
didSet {
dataSource?.settings = settings
collectionView.dataSource = dataSource
dataSource.fetchResult = fetchResult
}
}
private let dataSource: AssetsCollectionViewDataSource

private let selectionFeedback = UISelectionFeedbackGenerator()

init(store: AssetStore) {
self.store = store
dataSource = AssetsCollectionViewDataSource(fetchResult: fetchResult, store: store)
super.init(nibName: nil, bundle: nil)
}

Expand All @@ -79,6 +75,7 @@ class AssetsViewController: UIViewController {
collectionView.alwaysBounceVertical = true
collectionView.backgroundColor = settings.theme.backgroundColor
collectionView.delegate = self
collectionView.dataSource = dataSource
AssetsCollectionViewDataSource.registerCellIdentifiersForCollectionView(collectionView)

let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(AssetsViewController.collectionViewLongPressed(_:)))
Expand Down

0 comments on commit 0d72b2d

Please sign in to comment.