简体   繁体   English

CoreData:大量实体还是将数组存储为可变形?

[英]CoreData: Large quantity of entities or store an array as Transformable?

I am making a game where every creature can have 3 attacks and every attack can have many effects at the same time. 我正在制作一个游戏,其中每个生物可以进行3次攻击,并且每种攻击可以同时具有多种效果。 I'm using CoreData to hold all of the data. 我正在使用CoreData来保存所有数据。

A basic example of what my problem is can be seen with three entities: 我的问题所在的一个基本示例可以通过三个实体看到:

  1. Creature : contains a to-many relationship to Attack entity Creature :包含与Attack实体的多对多关系
  2. Attack : contains a to-many relationship to Effect entity Attack :包含与Effect实体的多对多关系
  3. Effect : contains the details of the effect (damage, confuse, poison, etc) Effect :包含效果的详细信息(损坏,混淆,毒药等)

So to begin I need to have three unique Attack entities for each unique Creature entity. 因此,开始时,我需要为每个唯一的Creature实体拥有三个唯一的Attack实体。 Then I would need to add the Effect entities to each attack. 然后,我需要将Effect实体添加到每个攻击中。 My problem is that each effect can have a different value but it is basically the same effect as all the other creatures: It is still "Damage" but the value of the damage is different from each creature. 我的问题是,每种效果可以具有不同的值,但与所有其他生物基本上具有相同的效果:它仍然是“伤害”,但伤害值与每种生物都不相同。

There are only two ways i've thought of doing this: 我想到的只有两种方法:

  1. Create a unique Effect for each Attack for each Creature , this way I could store the effect value inside the entity. 为每个Creature每次Attack创建唯一的Effect ,这样我就可以将效果值存储在实体中。 The disadvantage of this would be that the data could grow big: 3x3x400 = 3,600 Effect entities that only exist to have a single relationship each. 这样做的缺点是数据可能会变得很大:3x3x400 = 3,600 Effect实体,每个实体仅存在一个单一的关系。

  2. Create one Effect for each type (damage, confuse, etc) and use a Transformable data type in the Attack entity where I store an array of dictionaries containing each Effect entity identifier (so that they can be found when needed) and the value for that effect. 为每种类型(损坏,混乱等)创建一个Effect ,并在Attack实体中使用Transformable数据类型,在其中存储包含每个Effect实体标识符(以便在需要时可以找到它们)和值的字典的数组影响。 This can be easily handled by NSCoding automatically and would reduce the amount of Entity object from 3,600 to probably 20. 可以通过NSCoding自动轻松地处理,并将Entity对象的数量从3600个减少到大约20个。

The Effect & Attack entities are rarely accessed (only when the creature is in a battle) and I do not need to search inside them, therefore I think that would not be a noticeable performance impact. EffectAttack实体很少被访问(仅当该生物处于战斗状态时),并且我不需要在它们内部进行搜索,因此,我认为这不会对性能产生明显影响。 I have also read in many websites that they suggest not to store arrays and dictionaries in CoreData. 我还读过许多网站,他们建议不要在CoreData中存储数组和字典。 Is there any other way of doing this? 还有其他方法吗? and if not, what option would be better? 如果没有,哪种选择会更好?

PS: Sorry for the non descriptive title, I could not figure out what to put. PS:很抱歉,这是非描述性标题,我不知道该写些什么。

If you don't plan to change the kinds of your effects you could have an entity that you could call CreatureAttack where each row completely specifies the effects of a creature's attack. 如果您不打算更改效果的种类,则可以拥有一个可以称为CreatureAttack的实体,其中每一行都完全指定了生物攻击的效果。 The columns might be: foreign key to a Creature , foreign key to an Attack , Damage , Confuse , Poison , etc. For each effect column a row would have a value of the effect for this attack and creature. 这些列可能是: Creature外键, AttackDamageConfusePoison等的外键。对于每个效果列,一行将具有此攻击和生物的效果值。

This entity will only have at most # Creature x # Attack rows and you can find out everything about a creature's attack by pulling just one row. 该实体最多只有#个Creature x#个Attack行,而您只需拉动一行就可以找出有关生物攻击的所有信息。

Otherwise I think solution 1 is cleaner and the performance hit of having a few thousand rows should be negligible especially if you put an index on the Creature foreign key and/or a compound index on both Creature and Attack foreign keys (supported in iOS5). 否则,我认为解决方案1会更干净一些,尤其是如果您在Creature外键上添加索引和/或在CreatureAttack外键上都添加复合索引(iOS5支持),则具有数千行的性能影响应该可以忽略不计。

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

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