I am making a multiple selection feature for my collection view which shows photos from the user's library. I keep track of the selected indexPaths in an array and I want to update them in case a photo library change observer event happens in the middle of selecting cells. for example, if a user has selected indexes 3 and 4 and a change observer event removes indexes 1 and 2 from the collection view, selected indexes should change to 1 and 2.
I am trying to do it manually using these functions:
fileprivate func removeIndicesFromSelections(indicesToRemove:IndexSet){
var itemToRemove: Int?
for (_, removeableIndex) in indicesToRemove.map({$0}).enumerated() {
itemToRemove = nil
for (itemIndex,indexPath) in selectedIndices.enumerated() {
//deduct 1 from indices after the deletion index
if (indexPath.item > removeableIndex) && (indexPath.item > 0) {
selectedIndices[itemIndex] = IndexPath(item: indexPath.item - 1, section: 0)
} else if indexPath.item == removeableIndex {
itemToRemove = itemIndex
}
}
if let remove = itemToRemove {
selectedIndices.remove(at: remove)
disableDeleteButtonIfNeeded()
}
}
}
fileprivate func moveSelectedIndicesAfterInsertion (insertedIndices:IndexSet){
for (_, insertedIndex) in insertedIndices.map({$0}).enumerated() {
for (itemIndex,indexPath) in selectedIndices.enumerated() {
//add 1 to indices after the insertion index
if (indexPath.item >= insertedIndex) {
selectedIndices[itemIndex] = IndexPath(item: indexPath.item + 1, section: 0)
}
}
}
}
However, these are getting more complicated than I expected and I keep finding bugs in them. Is there any better way to handle this situation (such as any built in collection view capabilities) or I just have to come up with my own functions like above?
You're on the right path, but you should store a reference to what object the user actually selected, not where they selected it (since that can change).
In this case, you should keep a reference to the selected photos' identifiers (see docs) and then you can determine what cell/index-path should be selected. You can compare your selection array against your image datasource to determine what the most up-to-date index path is.
There is a solution provided by Apple. You can find more information in official documentation page:
Bacically you want to adopt PHPhotoLibraryChangeObserver and implement the following function:
func photoLibraryDidChange(_ changeInstance: PHChange) {
guard let collectionView = self.collectionView else { return }
// Change notifications may be made on a background queue.
// Re-dispatch to the main queue to update the UI.
DispatchQueue.main.sync {
// Check for changes to the displayed album itself
// (its existence and metadata, not its member assets).
if let albumChanges = changeInstance.changeDetails(for: assetCollection) {
// Fetch the new album and update the UI accordingly.
assetCollection = albumChanges.objectAfterChanges! as! PHAssetCollection
navigationController?.navigationItem.title = assetCollection.localizedTitle
}
// Check for changes to the list of assets (insertions, deletions, moves, or updates).
if let changes = changeInstance.changeDetails(for: fetchResult) {
// Keep the new fetch result for future use.
fetchResult = changes.fetchResultAfterChanges
if changes.hasIncrementalChanges {
// If there are incremental diffs, animate them in the collection view.
collectionView.performBatchUpdates({
// For indexes to make sense, updates must be in this order:
// delete, insert, reload, move
if let removed = changes.removedIndexes where removed.count > 0 {
collectionView.deleteItems(at: removed.map { IndexPath(item: $0, section:0) })
}
if let inserted = changes.insertedIndexes where inserted.count > 0 {
collectionView.insertItems(at: inserted.map { IndexPath(item: $0, section:0) })
}
if let changed = changes.changedIndexes where changed.count > 0 {
collectionView.reloadItems(at: changed.map { IndexPath(item: $0, section:0) })
}
changes.enumerateMoves { fromIndex, toIndex in
collectionView.moveItem(at: IndexPath(item: fromIndex, section: 0),
to: IndexPath(item: toIndex, section: 0))
}
})
} else {
// Reload the collection view if incremental diffs are not available.
collectionView.reloadData()
}
}
}
}
Related
I have a collection view made with UICollectionViewCompositionalLayout, with different sections and cells.
I fetch some data through my API and then I insert this data in my dataSource like this:
if let index = self.dataSource.firstIndex(where: { $0.id == "Cell with loader" }) {
self.dataSource.remove(at: index)
if !data.isEmpty {
self.dataSource.insert(data, at: index)
self.reloadCollectionViewSection?(.recapCards, .insertAndRemove)
} else {
self.reloadCollectionViewSection?(.recapCards, .remove)
}
}
This is my method to reload data in sections:
viewModel.reloadCollectionViewSection = { [weak self] section, action in
guard let self = self else { return }
switch action {
case .remove:
self.collectionView.performBatchUpdates({
let indexSet = IndexSet(integer: section.rawValue)
self.collectionView.deleteSections(indexSet)
}, completion: nil)
case .insert:
self.collectionView.performBatchUpdates({
let indexSet = IndexSet(integer: section.rawValue)
self.collectionView.insertSections(indexSet)
}, completion: nil)
case .insertAndRemove:
self.collectionView.performBatchUpdates({
let indexSet = IndexSet(integer: section.rawValue)
self.collectionView.deleteSections(indexSet)
self.collectionView.insertSections(indexSet)
}, completion: nil)
}
}
My question is: what if an API finish before another and I insert a section at an index that doesn't exist? I need that those section are ordered (that's the main reason of my insert), but is there a safer approach? I don't know if section 3 is added or not (it depends from my api), so how can I add ordered sections maximising my performance?
I can sort the section when everything is done, but I don't like this approach. How can I insert, remove and update section in the safest way?
I have two buttons in my tableviewCell which is confirm and ignore these two button should remove the cell when click tho it successfully removing the cell but when i refresh it goes back. How can i permanently removed the data in the array when clicking ignore or confirm.
Firstly the array was solely in activityArray but i need to filter it in order to get the pending activity.
if activities.count > 0 {
let filtered = activities.filter { $0.activityTypeAction == .FriendRequest || $0.activityTypeAction == .EventRSVPInvite }
if self.kCurrentPage == 0 {
self.activityArray.removeAll()
self.pendingActivityArray.removeAll()
}
self.activityArray.append(contentsOf: activities)
self.pendingActivityArray.append(contentsOf: filtered)
}
This is how i removing the cell.
cell.onClinkedIgnoreCallback = {
self.pendingActivityArray.remove(at: indexPath.row)
self.NotificationTableView.deleteRows(at: [indexPath], with: .left)
self.NotificationTableView.reloadData()
}
cell.onClickedConfirmCallback = {
self.pendingActivityArray.remove(at: indexPath.row)
self.NotificationTableView.deleteRows(at: [indexPath], with: .left)
self.NotificationTableView.reloadData()
}
you don't need to call that function to refresh because it again fillups the data in the respected arrays so if you want to refresh it just call tableview.reloadData()
When I dismiss my customVC while inserting items inside collection view, the app crashes with index out of range error. It doesnt matter how much I remove all collectionView data sources, it still crashes. Heres what I do to insert items:
DispatchQueue.global(qos: .background).async { // [weak self] doesn't do much
for customs in Global.titles.prefix(8) {
autoreleasepool {
let data = self.getData(name: customs)
Global.customs.append(data)
DispatchQueue.main.async { [weak self] in
self?.insertItems()
}
}
}
}
func insertItems() { // this function helps me insert items after getting data and it works fine
let currentNumbers = collectionView.numberOfItems(inSection: 0)
let updatedNumber = Global.customs.count
let insertedNumber = updatedNumber - currentNumbers
if insertedNumber > 0 {
let array = Array(0...insertedNumber-1)
var indexPaths = [IndexPath]()
for item in array {
let indexPath = IndexPath(item: currentNumbers + item, section: 0)
indexPaths.append(indexPath)
}
collectionView.insertItems(at: indexPaths)
}
}
I tried to remove all items in customs array and reload collection view before dismissing but still getting error:
Global.customs.removeAll()
collectionView.reloadData()
dismiss(animated: true, completion: nil)
I suspect that since I load data using a background thread, the collection view inserts the items even when the view is unloaded (nil) but using DispatchQueue.main.async { [weak self] in self?.insertItems() } doesn't help either.
I am using realm swift for my messaging app. My base view for messaging is a custom UICollectionView and my i use realm swift for data storage. Unfortunately i couldn't find any official example from realm for updating collection view with realm notification. So i implemented like this when i get messages from realm getting notificationToken by adding an observer for that realm list so when update comes from that notification i perform batch updates and delete, insert and modify indexes that realm told me to do in this notification. my app works well in testing environment but in production i got some crashes that reported my by fabric that told me app crashing and it is caused by this batch updates. their messages are mostly like
invalid update: invalid number of sections. The number of sections contained in the collection view after the update (1) must be equal to the number of sections contained in the collection view before the update (1), plus or minus the number of sections inserted or deleted (0 inserted, 1 deleted).
or
invalid number of items in section after update and so on .
I am so confused now. any ideas?
My code for doing this updates
notificationToken = dbmsgs?.observe { [weak self] (changes: RealmCollectionChange) in
switch changes{
case .initial:
print("intial")
self?.collectionView.reloadData()
case .update(_,let deletions,let insertions,let modifications):
self?.collectionView.performBatchUpdates({
if deletions.count > 0{
print("deletions count\(deletions.count)")
if (self?.collectionView.numberOfItems(inSection: 0) ?? 0) - deletions.count > 0 {
self?.collectionView.deleteItems(at: deletions.map { IndexPath(row: $0, section: 0) })
}
else{
self?.collectionView.deleteSections(IndexSet(integer: 0))
}
}
if insertions.count > 0{
print("insertaions count\(insertions.count)")
for index in insertions{
guard let lastdbmsg = self?.dbmsgs?[index] else{
return
}
let sectionIndex = 0
let itemIndex = ( self?.dbmsgs != nil) ? (self?.dbmsgs?.count)! - 1 : 0
if itemIndex == 0{
self?.collectionView.insertSections([0])
}
self?.collectionView.insertItems(at: [IndexPath(item: itemIndex, section: sectionIndex)])
if itemIndex != 0{
self?.collectionView.reloadItems(at: [IndexPath(row: itemIndex-1, section: 0)])
}
}
}
if modifications.count > 0{
self?.collectionView.reloadItems(at: modifications.map({ IndexPath(row: $0, section: 0)}))
}
},completion: { (_) in
})
case .error(let error):
// An error occurred while opening the Realm file on the background worker thread
fatalError("\(error)")
}
}
When I have instantiated the third cell, I will add more to my items to my model array and then I will update the collection view data with:
DispatchQueue.main.async(execute: {
self.collectionView.reloadData()
})
Everything works as expected. However, when I reload the data for my collectionView, it will instantiate cells that are currently visible or hold in memory (2,3). Unfortunately, I have some expensive server requests which consume a lot of time.
Instaniated 0
Instaniated 2
Instaniated 3
******polluting more data: size of cells 10
Instaniated 2
Instaniated 3
Instaniated 4
How can I reload the data without reCreating visible cells or those who are in memory?
Thanks a lot.
Instead of reloading the cells, try inserting or reloading the once that have actually changed. You can use UIColletionViews performBatchUpdates(_:) for this: Link
An example:
collectionView.performBatchUpdates {
self.collectionView.insertItems(at: [IndexPath(row: 1, section: 1)])
}
This ensures that only the new cells are loaded. You can also move around cells and sections in this method and even delete cells. The linked page contains documentation for all of this.
Why can't you go with below approach
1) I hope you have declared dataSource and collectionView objects as global to the class
let collectionView = UICollectionView()
var dataSource = [Any]()
2) Have one function to get the initial results from the API response
func getInitialPosts(){
// call api
// store your initial response in the local array object and reload collectionview
let results:[Any] = {response from the server call}
self.dataSource.append(contentsOf: results)
DispatchQueue.main.async(execute: {
self.collectionView.reloadData()
})
}
3) For the next call, you can have another function
func getPostsForPage(page:Int){
// call api with page number
let newResults = {response from the server call}
self.dataSource.append(contentsOf: newResults)
var indexPathsToReload = [IndexPath]()
let section = 0
var row = self.dataSource.count - 1
//add new data from server response
for _ in newResults {
let indexPath = IndexPath(row: row, section: section)
row+=1
indexPathsToReload.append(indexPath)
}
// perform reload action
DispatchQueue.main.async(execute: {
self.collectionView.insertItems(at: indexPathsToReload)
})
}
Suppose from your network adapter you are calling delegate function fetchData with new data. Here you have to check if your data is empty or not to check if you need to add new data, or reload entire CollectionView.
Then you create all indexPaths that you need to fetch more, in order to let already fetched cell stay as they are. And finally use insertItems(at: IndexPaths).
I use page in order to paginate new data with page number in the future. Strictly for my use case. Good luck!
func fetchData(with videos: [VideoModel], page: Int) {
guard self.data.count == 0 else{
self.addNewData(with: videos, page: page)
return
}
self.data = videos
DispatchQueue.main.async {
self.isPaging = false
self.collectionView?.reloadData()
self.page = page
}
}
func addNewData(with videos: [VideoModel], page: Int){
var indexPathsToReload = [IndexPath]()
let section = 0
var row = self.data.count - 1
self.data += videos
for _ in videos {
print(row)
let indexPath = IndexPath(row: row, section: section)
row+=1
indexPathsToReload.append(indexPath)
}
DispatchQueue.main.async{
self.isPaging = false
self.collectionView!.insertItems(at: indexPathsToReload)
self.page = page
}
}