Swift: +[Catransaction Synchronize] Called Within Transaction While Decoding HTML Entities

SWIFT: +[CATransaction synchronize] called within transaction while decoding HTML entities

This problem occurred during decoding HTML entities, so i looked for another way to decode and used the following code:

 func configureCell(cell: UITableViewCell, atIndexPath indexPath: NSIndexPath) {
let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as NSManagedObject

let eTitle:NSString = object.valueForKey("title")!.description
let deTitle = eTitle.stringByDecodingHTMLEntities()
cell.textLabel?.text = deTitle
}

Earlier, the stringByDecodingHTMLEntities() was missing. So I had taken this approach.

Note: To Get stringByDecodingHTMLEntities() we need to import NSString+HTML.h, from here NSString category for HTML

How to display HTML text in TextView

This works for me. Remember that the NSAttributedString constructor now throws an NSError object:

Swift 3:

do {
let str = try NSAttributedString(data: def.data(using: String.Encoding.unicode, allowLossyConversion: true)!, options: [NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType], documentAttributes: nil)
} catch {
print(error)
}

Swift 2.x:

do {
let str = try NSAttributedString(data: def.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!, options: [NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType], documentAttributes: nil)
} catch {
print(error)
}


Related Topics



Leave a reply



Submit