简体   繁体   English

核心数据:强制一个属性在其他属性之前加载

[英]Core Data: force one attribute to load before others

I have some transient attributes in my Entity that are derived from one other transient attribute (call is X).我的实体中有一些从另一个瞬态属性派生的瞬态属性(调用是 X)。 When I do a fetch, it seems to try to load all attributes in no particular order, so if X is taking some time to calculate, the other derived attributes try to use X and it's null so it causes issues.当我进行提取时,它似乎试图以没有特定顺序的方式加载所有属性,所以如果 X 需要一些时间来计算,其他派生属性会尝试使用 X 并且它是 null 所以它会导致问题。

Is there any way to force Core Data to ensure loading one attribute before other attributes can read from it?有什么方法可以强制 Core Data 确保在其他属性可以读取之前加载一个属性? Or force the other attributes to reload once attribute X has done loading?还是在属性 X 完成加载后强制其他属性重新加载? Any other technique anyone would recommend?任何人会推荐任何其他技术? I'm using this in an iOS tableView backed by an NSFetchedResultsController.我在 NSFetchedResultsController 支持的 iOS tableView 中使用它。

No you can't force attributes to "load" in order because attributes don't load.不,您不能强制属性按顺序“加载”,因为属性不会加载。 You are initializing objects here and not reading cells in a procedural database.您在这里初始化对象,而不是读取程序数据库中的单元格。

I'm going to guess that your trying to calculate some value based on the attributes or counts of external objects.我猜你试图根据外部对象的属性或计数来计算一些值。 If so, then you can get serious slow downs while the other objects are faulted in so that their values can be accessed.如果是这样,那么当其他对象出现故障时,您可能会严重减速,以便可以访问它们的值。

If so, then you've got a data model design problem.如果是这样,那么您就有了数据 model 设计问题。 I've never seen a transient value so complex that its calculation impaired operation.我从未见过如此复杂的瞬态值,以至于它的计算会影响操作。 You probably need to break it apart or even move it to its own object.您可能需要将其拆开,甚至将其移至自己的 object。

Eg Suppose you've got a data model that simulates a group of people with Person objects.例如,假设您有一个数据 model 来模拟一组具有Person对象的人。 You want some behavior of each Person object to change depending on how many people their are.您希望每个Person object 的某些行为根据他们的人数而改变。 So, when there are less than five each person does "X" if there are 6 to 10 you do "Y" and so on.因此,当每个人少于 5 个时,如果有 6 到 10 个,则每个人都做“X”,你做“Y”等等。

In that circumstance, it would be best to create a second entity related to all the Person objects that tracks the number of Person objects total.在这种情况下,最好创建与所有Person对象相关的第二个实体,以跟踪Person对象的总数。 Then getting the count of all Person objects or the collective sum of one of the Person entity attributes becomes fast and trivial.然后获取所有Person对象的计数或Person实体属性之一的总和变得快速而简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM