在主ui线程中加载Realm对象是否可以接受? [英] Is it acceptable to load Realm objects in the main ui thread?

查看:119
本文介绍了在主ui线程中加载Realm对象是否可以接受?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在iOS应用中将(Swift)Realm用作数据存储,到目前为止,我们对此感到非常满意.关于使用Realm和多线程检索和存储对象的设计存在一个问题:

we are adopting (Swift)Realm as a data store in our iOS app and we are really pleased with it so far. We have a question around the design for the retrieval and storage of objects with Realm and multi-threading:

在主ui线程中加载对象是否可以接受?

  • 我们知道约束所带的对象,这些对象无法在线程之间共享.
  • 我们还没有看到任何性能问题,但是到目前为止,我们的方法是在后台线程中加载各种资源.
  • 在我们加载和过滤一些数据并注册一个通知块的情况下,使用主ui线程不会出现问题,但是我们将如何处理这种情况,例如我们想在其中显示所有数据表格视图?

推荐答案

在主ui线程中加载对象是否可以接受?

是的,在大多数情况下*可以接受并且足够快.如果从数据库中进行读取会阻塞用户,这是不可接受的,但是由于没有诸如错误之类的概念,因此读取访问总是可以快速预测的. 仅当您有一个非常复杂的对象图时,您需要进行大量的预处理才能在UI上显示对象,才有意义的是使用后台线程和/或缓存来保证良好的用户体验.

Yes, it is in most cases* acceptable and fast enough. It wouldn't be acceptable if reading from the database would block the user, but as there is no concept like faults, read access is always predictable fast. Only if you have a really complex object graph, where you need to do heavy pre-processing to be able to display the objects on the UI, it would make sense to employ a background thread and/or caching to warrant a good user experience.

在我们加载和过滤一些数据并注册一个通知块的情况下,使用主ui线程不会出现问题,但是我们将如何处理这种情况,例如我们想在其中显示所有数据表格视图?

In the case where we load and filter some data and register a notification block, we don't see problems with using the main ui thread, but how would we handle a situation, where we for example want to display all data in a table view?

A UITableView仅请求屏幕上当前可见的那些单元格,并重新使用视图容器. Realm集合在这种懒惰的性质上类似,如果不对其进行过滤,则不会增加内存压力,因为对于从其中拉出的那些对象,您只会获得对象访问器.只要您依赖内置的Realm ResultsList集合,就不需要分页.仅当您需要以Realm不支持的方式应用自定义且复杂的过滤器时,才有必要在后台线程上对其进行处理.

A UITableView only request those cells which are currently visible on the screen and reuses the view containers. A Realm collection is similar in this lazy nature, when you don't filter it, it doesn't enlarge the memory pressure, because you get only object accessors for those objects which you pull out of it. There is no need for pagination as long as you rely on the builtin Realm Results or List collections. Only if you need to apply a custom and complex filter in a way which isn't supported by Realm, it might be necessary to process that on a background thread.

这篇关于在主ui线程中加载Realm对象是否可以接受?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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