Post

Replies

Boosts

Views

Activity

Getting an 'NSInternalInconsistencyException', reason: 'Invalid section 0.' when doing a search on multiple sections
I am getting an exception when doing a search on multiple section. It occurs when applying a snapshot on the datasource. Background: I have (pre-defined) sections, and each section has a collection of items. Sections won't appear in the viewController if there are no items in section. Items are added by a function of the app. Once an item is added in one of the section, datasource update is called and will show the section with the item added. Problem: Encountering this issue when trying to search for a non-existent item twice. To reproduce, you can enter a non-existent item, then delete the search string via a backspace, then input a non-existing item again, then error will be thrown on the dataSource.apply(). Hoping someone can help. TIA! Here is the code: func updateData(on searchItem: String = "") { 				snapshot = NSDiffableDataSourceSnapshot<Section, Item>() 				 				Manager.shared.getAllSections().forEach { section in 						 						let items = section.items 						//if search string is empty, we just assign the items of the section, 						//else we filter it based on the searchItem 						var filteredItems = searchItem.isEmpty ? items : 								items.filter { $0.itemName.lowercased().contains(searchItem.lowercased()) } 						 						//if theres no items filtered, we ain't appending any section and items 						if filteredItems.count > 0 { 								snapshot.appendSections([section]) 								snapshot.appendItems(filteredItems) 						} 				} 				 				//I get the exception when calling apply on dataSource 				dataSource.apply(snapshot, animatingDifferences: false) 		} 		 //Here is the updateSearchResults delegate method triggered when typing something in the search bar 		func updateSearchResults(for searchController: UISearchController) { 				guard let searchedItem = searchController.searchBar.text, !searchedItem.isEmpty else {	 						updateData() 						return 				} 				 				updateData(on: searchedItem) 		}
2
0
653
Jul ’20