Naoto Ida
Naoto Ida

Reputation: 1295

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.

Upvotes: 7

Views: 2167

Answers (1)

Sam Soffes
Sam Soffes

Reputation: 14935

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.

Upvotes: 6

Related Questions