Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Requesting caretRectForPosition: while the NSTextStorage has outstanding changes

I've been recently getting the error:

requesting caretRectForPosition: while the NSTextStorage has oustanding changes {x, x}

* "Oustanding" is literally what it says, and is not my typo.

This is being called when I am iterating through the NSTextStorage of a subclass of NSTextView with the enumerateAttribute() method and manipulating the NSTextAttachments in the text view after every change in the text view.

func manipulateText() {
    let text = customTextView.textStorage
    text.enumerateAttribute(NSAttachmentAttributeName, inRange: NSMakeRange(0, text.length), options: NSAttributedStringEnumerationOptions(rawValue: 0)) {
    //
    }
}

extension ThisViewController: UITextViewDelegate {
    func textViewDidChange(textView: UITextView) {
        manipulateText()
    }
}

Questions such as this seem to be online, but I have yet to find any occurrences of this and seems to be relevant to iOS 9 only.

This only happens when using a physical keyboard on iPad.

like image 502
Naoto Ida Avatar asked Dec 02 '15 07:12

Naoto Ida


1 Answers

This happens if you call caretRectForPosition (or any method that calls that like firstRectForRange) while the text storage has edits.

I was able to prevent these logs by deferring some stuff until after endEditing is called in the NSTextStorage and dispatch_async to the main queue to do my work. There aren't any visible UI flashes or anything as a result of the async.

There has to be a better way to solve this, but this is all I could figure out.

like image 86
Sam Soffes Avatar answered Oct 17 '22 13:10

Sam Soffes