Skip to content

Commit

Permalink
Merge pull request #996 from planetary-social/fix-nsinternalconsisten…
Browse files Browse the repository at this point in the history
…cyexpection-in-pagednotedatasource

Probably fix crash on home feed
  • Loading branch information
mplorentz authored Apr 5, 2024
2 parents 723e996 + a5e83ae commit c1d57af
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,4 @@ fastlane/screenshots/**/*.png
fastlane/test_output

.idea
.vscode
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Updated dark theme colors for card backgrounds, primary text, and secondary text.
- Added a new UI for replying to messages that allows attaching images and setting an expiration date.
- Fixed an issue where Profile pages could display little or no content.
- Fixed a crash that often occurred after opening the app.

## [0.1.7] - 2024-03-21Z

Expand Down
6 changes: 3 additions & 3 deletions Nos.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1241,12 +1241,12 @@
C9DEC0042989477A0078B43A /* Fixtures */ = {
isa = PBXGroup;
children = (
C94B2D172B17F5EC002104B6 /* sample_repost.json */,
CD27177529A7C8B200AE8888 /* sample_replies.json */,
C9BD91882B61BBEF00FDA083 /* bad_contact_list.json */,
C9DEC005298947900078B43A /* sample_data.json */,
CD27177529A7C8B200AE8888 /* sample_replies.json */,
C94B2D172B17F5EC002104B6 /* sample_repost.json */,
C9ADB134299288230075E7F8 /* KeyFixture.swift */,
C90B16B72AFED96300CB4B85 /* URLExtensionTests.swift */,
C9BD91882B61BBEF00FDA083 /* bad_contact_list.json */,
);
path = Fixtures;
sourceTree = "<group>";
Expand Down
19 changes: 14 additions & 5 deletions Nos/Controller/PagedNoteDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,14 @@ class PagedNoteDataSource<Header: View, EmptyPlaceholder: View>: NSObject, UICol
// MARK: - UICollectionViewDataSource

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
let numberOfItems = fetchedResultsController.fetchedObjects?.count ?? 0
Log.debug("Number of items: \(numberOfItems) in section: \(section)")
return numberOfItems
let numberOfFetchedObjects = fetchedResultsController.fetchedObjects?.count ?? 0
// because we batch updates together to reduce animations but this function is called in between batches we
// need to account for the number of items queued for insertion or deletion. FetchedResultsController sees them
// but the collectionView doesn't yet.
let numberOfItemsInView = numberOfFetchedObjects - insertedIndexes.count + deletedIndexes.count
Log.debug("Number of items: \(numberOfFetchedObjects) in fetchedResultsController")
Log.debug("Number of items: \(numberOfItemsInView) in section: \(section)")
return numberOfItemsInView
}

func collectionView(
Expand Down Expand Up @@ -238,11 +243,15 @@ class PagedNoteDataSource<Header: View, EmptyPlaceholder: View>: NSObject, UICol
Log.debug("controllerDidChangeContent started.")
if !deletedIndexes.isEmpty { // it doesn't seem like this check should be necessary but it crashes otherwise
Log.debug("deleting indexPaths: \(deletedIndexes)")
collectionView.deleteItems(at: deletedIndexes)
let deletedIndexesCopy = deletedIndexes
deletedIndexes = [] // clear indexes so numberOfItemsInSection can calculate the correct number
collectionView.deleteItems(at: deletedIndexesCopy)
}
if !insertedIndexes.isEmpty {
Log.debug("inserting indexPaths: \(insertedIndexes)")
collectionView.insertItems(at: insertedIndexes)
let insertedIndexesCopy = insertedIndexes
insertedIndexes = [] // clear indexes so numberOfItemsInSection can calculate the correct number
collectionView.insertItems(at: insertedIndexesCopy)
}

Log.debug("moving indexes: \(movedIndexes)")
Expand Down

0 comments on commit c1d57af

Please sign in to comment.