在NSAttributedString中添加图像并保存到Core Data性能 [英] Add images in NSAttributedString and save to Core Data peformance

查看:133
本文介绍了在NSAttributedString中添加图像并保存到Core Data性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将带有图像的NSAttrbutedString保存到Core Data时遇到了这个巨大的性能问题。

I'm encountering this huge performance issue while saving the NSAttrbutedString with images in it to Core Data.

有一个UITextView允许用户输入文本以及添加图像。当用户完成输入并单击完成按钮时,它将保存到CoreData,并显示在TableView中。

There is a UITextView where allows the user to input text as well as adding images. When user finishes typing and click 'done' button, it will be saved to CoreData, and shows in a TableView.

以下是点击完成按钮后保存内容的方式:

创建一个私有MOC并将AppDelegates managedObjectContext指定为其父MOC。

Create a private MOC and assign the AppDelegates managedObjectContext as its parent MOC.

privateMOC = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
privateMOC.parent = managedObjectContext

privateMOC.perform {
    do {
      try self.privateMOC.save()

      self.managedObjectContext.performAndWait {
        do {
          try self.managedObjectContext.save()
        } catch {
          fatalError("Failure to save context: \(error)")
        }
      }
    } catch {
      print("Could not save \(error)")
    }
  }

有两个问题:


  1. 取决于图像,当我点击完成按钮时,UI被阻止,并在视图解除并显示之前需要3-5秒n在TableView中。

  1. Depends on the images, when I click 'done' button, the UI is blocked and takes 3 - 5 seconds before the view dismissed and shown in the TableView.

我检查实际数据库.sqlite并找到,一个新添加的条目(UITextView中的文本和图像)使数据库大小增加几乎 12MB ! (也许图像占用太多空间?)

I check the actual database .sqlite and found, one single newly-added entry(text and images in UITextView) makes the database size increases almost 12MB! (Maybe the images take too much space?)

有任何建议如何解决这些问题?
谢谢!

Any suggestions how to tackle these issues? Thanks!

推荐答案

在您的用例中,这些问题很难预防。通过在包含图像的 NSAttributedString 上使用 NSCoding ,您要求的是二进制blob,您可以' t以任何方式控制编码过程。

These issues are going to be difficult to prevent in your use case. By using NSCoding on an NSAttributedString that includes images, what you're asking for is a binary blob where you can't control the encoding process in any way.


  • 可能是图像尺寸膨胀,例如因为您要保存未压缩的数据而不是PNG或JPEG。但你无法做任何事情,因为你无法控制 NSAttributedString 如何处理它。所以你可能会遇到大数据量。

  • 长处理时间可能与前一点有关 - 你无法控制 NSAttributedString 正在内部进行,但很可能很多时间都在处理图像。

  • It might be that the image size is inflated, e.g. because you're saving the uncompressed data instead of a PNG or JPEG. But you can't do anything about that because you can't control how NSAttributedString handles it. So you're probably stuck with the large data size.
  • The long processing time is probably connected to the previous point-- you can't control what NSAttributedString is doing internally, but it's likely that a lot of that time is taken up dealing with the image(s).

如果您自己编码属性字符串而不是使用可转换属性,则可能会改善保存时间。然后你可以在后台预先启动编码,而不是在保存数据时发生。您可能无法修复编码时间,但您可能能够更早地实现它并且不那么明显。我认为,只要您使用编码的属性字符串,就会遇到大小问题。

You might improve on the save time if you encoded the attributed strings on your own instead of using a transformable attribute. Then you could start the encoding in advance, in the background, instead of having it happen when you're saving data. You probably can't fix the encoding time, but you may be able to make it happen earlier and be less noticeable. I think you're stuck with the size as long as you're using encoded attributed strings, though.

我不知道您的应用正在做什么,但如果你可以远离潜在的大量 NSAttributedString ,你可以避免遇到的问题。

I don't know what your app is doing but if you could get away from potentially massive NSAttributedStrings you'll avoid the problems you're having.

这篇关于在NSAttributedString中添加图像并保存到Core Data性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆