فهرست منبع

improved code

marinofaggiana 4 سال پیش
والد
کامیت
ddc90e4c2c
1فایلهای تغییر یافته به همراه37 افزوده شده و 21 حذف شده
  1. 37 21
      iOSClient/Transfers/NCTransfers.swift

+ 37 - 21
iOSClient/Transfers/NCTransfers.swift

@@ -89,11 +89,15 @@ class NCTransfers: NCCollectionViewCommon, NCTransferCellDelegate  {
                 if let metadata = NCManageDatabase.sharedInstance.getMetadataFromOcId(ocId) {
                     if let row = dataSource.addMetadata(metadata) {
                         let indexPath = IndexPath(row: row, section: 0)
-                        collectionView?.performBatchUpdates({
-                            collectionView?.insertItems(at: [indexPath])
-                        }, completion: { (_) in
-                            self.reloadDataSource()
-                        })
+                        if indexPath.section < collectionView.numberOfSections && indexPath.row < collectionView.numberOfItems(inSection: indexPath.section) {
+                            collectionView?.performBatchUpdates({
+                                collectionView?.insertItems(at: [indexPath])
+                            }, completion: { (_) in
+                                self.reloadDataSource()
+                            })
+                        } else {
+                            reloadDataSource()
+                        }
                     }
                 }
             }
@@ -110,11 +114,15 @@ class NCTransfers: NCCollectionViewCommon, NCTransferCellDelegate  {
                         
                         if let row = dataSource.deleteMetadata(ocId: metadata.ocId) {
                             let indexPath = IndexPath(row: row, section: 0)
-                            collectionView?.performBatchUpdates({
-                                collectionView?.deleteItems(at: [indexPath])
-                            }, completion: { (_) in
-                                self.collectionView?.reloadData()
-                            })
+                            if indexPath.section < collectionView.numberOfSections && indexPath.row < collectionView.numberOfItems(inSection: indexPath.section) {
+                                collectionView?.performBatchUpdates({
+                                    collectionView?.deleteItems(at: [indexPath])
+                                }, completion: { (_) in
+                                    self.collectionView?.reloadData()
+                                })
+                            } else {
+                                reloadDataSource()
+                            }
                         } else {
                             reloadDataSource()
                         }
@@ -123,11 +131,15 @@ class NCTransfers: NCCollectionViewCommon, NCTransferCellDelegate  {
                         
                         if let row = dataSource.reloadMetadata(ocId: metadata.ocId, ocIdTemp: ocIdTemp) {
                             let indexPath = IndexPath(row: row, section: 0)
-                            collectionView?.performBatchUpdates({
-                                collectionView?.reloadItems(at: [indexPath])
-                            }, completion: { (_) in
-                                self.collectionView?.reloadData()
-                            })
+                            if indexPath.section < collectionView.numberOfSections && indexPath.row < collectionView.numberOfItems(inSection: indexPath.section) {
+                                collectionView?.performBatchUpdates({
+                                    collectionView?.reloadItems(at: [indexPath])
+                                }, completion: { (_) in
+                                    self.collectionView?.reloadData()
+                                })
+                            } else {
+                                reloadDataSource()
+                            }
                         } else {
                             reloadDataSource()
                         }
@@ -145,13 +157,17 @@ class NCTransfers: NCCollectionViewCommon, NCTransferCellDelegate  {
                 if let metadata = NCManageDatabase.sharedInstance.getMetadataFromOcId(ocId) {
                     if let row = dataSource.deleteMetadata(ocId: metadata.ocId) {
                         let indexPath = IndexPath(row: row, section: 0)
-                        collectionView?.performBatchUpdates({
-                            collectionView?.deleteItems(at: [indexPath])
-                        }, completion: { (_) in
-                            self.collectionView?.reloadData()
-                        })
+                        if indexPath.section < collectionView.numberOfSections && indexPath.row < collectionView.numberOfItems(inSection: indexPath.section) {
+                            collectionView?.performBatchUpdates({
+                                collectionView?.deleteItems(at: [indexPath])
+                            }, completion: { (_) in
+                                self.collectionView?.reloadData()
+                            })
+                        } else {
+                            reloadDataSource()
+                        }
                     } else {
-                        self.reloadDataSource()
+                        reloadDataSource()
                     }
                 }
             }