Repository navigation
Fix a crash when a post or page list changes during a long press - #26416
Conversation
UIKit builds a row's context menu about half a second after the long press starts. The Posts, Search and Pages lists read the row again at that point, so a sync that removed rows in between crashed the app or opened the menu for the next post. Keep the pressed post when the press starts, find its current row when building the menu, and show no menu if it is gone.
|
Version |
| return nil | ||
| } | ||
| let cell = self.tableView.cellForRow(at: indexPath) | ||
| return AbstractPostMenuHelper(post).makeMenu(presentingView: cell ?? UIView(), delegate: self) |
There was a problem hiding this comment.
Finding from Claude:
Not blocking: if a sync pushes the pressed post off screen before the menu opens, the menu has no actions.
What happens: the menu now looks up the cell at the post's new row. When that row is off screen there's no cell, so cell ?? UIView() passes a throwaway view. AbstractPostMenuHelper holds it weakly, so it's gone by the time the deferred element runs, and no actions are added.
Who is affected: rare. You'd need to press a row near the bottom while a sync inserts enough posts above it. Before this PR, the same case showed a menu for the wrong post. The page list and search use the same fallback.
Suggested fix: keep the pressed cell and fall back to it, then to the table view:
let pressedCell = tableView.cellForRow(at: indexPath)
// in the closure, with [weak pressedCell]:
let cell = self.tableView.cellForRow(at: indexPath) ?? pressedCell
return AbstractPostMenuHelper(post).makeMenu(presentingView: cell ?? self.tableView, delegate: self)How this was checked
A test added to PostListContextMenuTests inserted 30 newer posts after the press, which moved the pressed post from row 1 to row 31 (rows 0–18 visible). "View" was missing from the menu in 3 of 3 runs. With the fix above applied to the post list, that test and the existing three pass (iPhone 18 Pro, iOS 27.2). The page list and search fallbacks were read, not tested.
If a sync moves the pressed post off screen before UIKit builds the menu, the post has no cell to anchor the menu, and UIKit lifts the row now at the pressed position, which shows another post. The menu was stuck on "Loading…" with no actions. Show no menu instead, as when the post is gone.
Generated by 🚫 Danger |
Fixes JETPACK-IOS-1C5W, WORDPRESS-IOS-498X, JETPACK-IOS-1JHW and JETPACK-IOS-1K24.
UIKit builds a row's context menu about half a second after a long press starts. The Posts list, post search and Pages list read the row again at that point. If a sync removed rows in between, the app crashed or the menu acted on the next post. This change keeps the pressed post when the press starts, finds its current row when the menu is built, and opens no menu if the post is gone. New unit tests change the list between the two calls.
To test: