UITextView in collectionview cell on mac spikes cpu

I have a simple UIKit application that has a UITextView in a UICollectionViewCell. The app is designed for iOS/iPadOS and works just fine on those platforms. However, when run on Mac (Designed for iPad) as soon as I start scrolling the collectionview, the cpu usage spikes to ~85% and stays there indefinitely. The only way to lower the cpu is to click outside of the application window, but once it comes to the foreground again, the cpu usage jumps right back up. I've tried running on Mac in Catalyst mode too, but the same problem occurs with slightly less cpu usage (~45%).

Additionally the debugger constantly spits out [API] cannot add handler to 3 from 3 - dropping while scrolling.

Does anyone have an explanation or solutions for this?

I’m using Xcode Version 14.1 (14B47b) on macOS Ventura 13.0 (22A380).

class ViewController: UIViewController {
    var dataSource: UICollectionViewDiffableDataSource<Section, String>! = nil
    var collectionView: UICollectionView! = nil
    var items = Array(0...100).map{"Item \($0)"}
    
    enum Section: String {
        case main
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.title = "List"
        configureCollectionView()
        configureDataSource()
        applyInitialSnapshot()
    }
    
    private func createLayout() -> UICollectionViewLayout {
        return UICollectionViewCompositionalLayout { sectionIndex, layoutEnvironment in
            let size = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .estimated(100))
            let item = NSCollectionLayoutItem(layoutSize: size)
            let group = NSCollectionLayoutGroup.horizontal(layoutSize: size, subitems: [item])
            
            return NSCollectionLayoutSection(group: group)
        }
    }
    
    private func configureCollectionView() {
        collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: createLayout())
        collectionView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        collectionView.backgroundColor = .systemBackground
        view.addSubview(collectionView)
    }
    
    private func configureDataSource() {
        let cellRegistration = UICollectionView.CellRegistration<TestCell, String> { (cell, indexPath, item) in
            cell.configure(title: item, row: indexPath.item)
        }
        
        dataSource = UICollectionViewDiffableDataSource<Section, String>(collectionView: collectionView) {
            (collectionView, indexPath, identifier) -> UICollectionViewCell? in
            
            return collectionView.dequeueConfiguredReusableCell(using: cellRegistration, for: indexPath, item: identifier)
        }
    }
    
    private func applyInitialSnapshot() {
        var snapshot = NSDiffableDataSourceSnapshot<Section, String>()
        snapshot.appendSections([.main])
        snapshot.appendItems(items)
        
        dataSource.apply(snapshot, animatingDifferences: false)
    }
}
class TestCell: UICollectionViewCell {
    private let annotationsTextView = UITextView()

    override init(frame: CGRect) {
        super.init(frame: frame)
        addViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func configure(title: String, row: Int) {
        annotationsTextView.attributedText = .init(string: "Row: \(row) Item: \(title)", attributes: [.font: UIFont.preferredFont(forTextStyle: .title1)])
    }
    
    private func addViews() {
        annotationsTextView.isScrollEnabled = false
        annotationsTextView.isEditable = false
        annotationsTextView.translatesAutoresizingMaskIntoConstraints = false
        
        contentView.addSubview(annotationsTextView)
        
        NSLayoutConstraint.activate([
            annotationsTextView.topAnchor.constraint(equalTo: contentView.topAnchor),
            annotationsTextView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
            annotationsTextView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
            annotationsTextView.trailingAnchor.constraint(equalTo:  contentView.trailingAnchor),
        ])
    }
}
Post not yet marked as solved Up vote post of rspoon3 Down vote post of rspoon3
3.1k views

Replies

I am receiving the same message in my debugger: [API] cannot add handler to 3 from 3 - dropping. However, this is for a SwiftUI project. I am using a LazyVGrid, so that may be connected to the UICollectionView, but I don't have any UITextView or similar instances in my views. XCode Version 14.1 (14B47b), Ventura 13.0.

Maybe because the cells have text views in them, it is dropping handlers for gesture recognizers? My views have context menus on them which would also require gesture recognizers in the underlying API. Other than the Collection angle, that's the only connection I can see. However, the message seems pretty benign and I'm not getting the high CPU usage you are.

I have the same "[API] cannot add handler to 3 from 3 - dropping" message in my App, but I am working in SwiftUI. It looks like my problem is caused by each view which contains a "NavigationView". Changing the NavigationView to a NavigationStack solves the problem. I do not know the equivalent in UIKit. Hopes this will get someone on the right track.

Hey, I am having exactly the same problem on macCatalyst using the NavigationSplitView. Did you find a solution to solve that problem keep using the NavigationSplitView?

I'm seeing the same thing here. "[API] cannot add handler to 3 from 3 - dropping" filling my logs on a Mac Catalyst app build tin SwiftUI. Even with something like

var body: some View {
    List(list, id: \.id, selection: selection) { project in 
        text(verbatim: project.name)
    }
}

I'm seeing it appear when the list appears. Add in some swipeActions, contextMenu and a whole lot more will appear.

I am Seeing this without knowing what is the cause

I am seeing this too in a macCatalyst SwiftUI app. It happens when I move the window..

I'm also experiencing this in my catalyst app. This happens every time I enter a keystroke and add text to a text view.

I'm also experiencing this problem on my mac catalyst app. This problem didn't happen before.... it started to happen about 3-4 months ago.

Interestingly, it happens to some of my views but I still cannot figure out what causes it.