0

I have some HTML data which I store it in CoreData as Binary Data. I display it as NSMutableAttributedString in UITableViewCells as below:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSManagedObject *row = [self.messagesFRC objectAtIndexPath:indexPath];

    MessageLTableViewCell *cell = (MessageLTableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:@"MSGCELLID" forIndexPath:indexPath];

    NSString *messageText = [[NSString alloc] initWithData:[row valueForKey:@"message_text"] encoding:NSUTF8StringEncoding];

    messageText = [NSString stringWithFormat:@"<style>body{font-family: '%@';direction:rtl;float:right; font-size:%fpx;}</style>%@", FONT_TEXT, 17.0, messageText];

    NSError *err = nil;
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc]
      initWithData:[messageText dataUsingEncoding:NSUTF8StringEncoding]
      options:
      @{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,
        NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding),
    }
    documentAttributes: nil
    error: &err];


    if(err)
        NSLog(@"Unable to parse label text: %@", err);
    cell.messageText.attributedText = attrStr;


    return cell;

}

The problem is that it causes high CPU load and also the UITableView freezes in scrolling. What is the best practice solutions to handle it with best performance?

Soheil Novinfard
  • 1,358
  • 1
  • 16
  • 43
  • Why are you storing it binary if you then need to convert it to html and then attributed string? So much runtime work for every cell... – Wain Feb 07 '16 at 09:17
  • @Wain I need to store it in CoreData, and I could not find any proper type other than the "Binary Data" for it, what do you recommend ? – Soheil Novinfard Feb 07 '16 at 09:20

1 Answers1

1

First, you should have this logic in your managed object subclass. This kind of heavy lifting with data manipulation does not belong in a table view data source. Give your managed object subclass a convenience method attributedString for this.

Second, you can store the HTML as text, i.e. String type. This will have the added advantage that it will become searchable.

As for performance, it seems that the data method with NSHTMLTextDocumentType is really very slow (someone mentions a test in the comments here). The alternative is to use NSRanges which is feasible if the HTML strings are not too complicated.

Community
  • 1
  • 1
Mundi
  • 79,884
  • 17
  • 117
  • 140
  • Thanks, I could not store as `String` type, due to I have long text and `String` is very limited. – Soheil Novinfard Feb 07 '16 at 09:55
  • 1
    Not sure what you mean. There are no real limits to the string length in SQLite, certainly not in the dimension of what you would display in a UITableViewCell. The general advise for too long strings is to break them up somehow (see [here](http://stackoverflow.com/a/10327081/427083)). – Mundi Feb 07 '16 at 10:25
  • 1
    Or store in a file and display in a web view. As said long text and table cells don't go well together really... – Wain Feb 07 '16 at 13:03
  • @Mundi Could you explain more about "First" in your answer? Any good example is appreciated. I understood the two others and they are useful. Thanks. – Soheil Novinfard Feb 08 '16 at 09:29
  • 1
    It is simply about code organization. You should enforce the principle of encapsulation, meaning that functionality should be split up and assigned to the units where it belongs. In order to *reuse* the database-to-attributed-text functionality it needs to be part of the managed object. Otherwise you would have to copy-paste your extraction code to the next view controller that wants to use it, making it impossible to maintain after a while. – Mundi Feb 09 '16 at 06:25