简体   繁体   English

iOS /核心数据-从NSSet关系返回数组的最佳方法?

[英]iOS / Core Data - Best way to return an array from a NSSet relationship?

I'm creating an app and I'm storing the data using core data. 我正在创建一个应用程序,并使用核心数据存储数据。 It works fine but I was wondering what's the best way to return a sorted array from an set ? 它工作正常,但我想知道从集合中返回排序数组的最佳方法是什么?

For example, I have an entity A (Button) that contains a list of entity B (Image). 例如,我有一个实体A(按钮),其中包含实体B(图像)的列表。 As we all know core data is storing the to-many relationship using an NSSet. 众所周知,核心数据正在使用NSSet存储多对多关系。 But I want to keep track of the initial order so I added a property "order" for that purpose. 但是我想跟踪初始顺序,因此为此添加了一个属性“ order”。 The problem is that in my code I need the list of images sorted so in a category file (Button+Create) I created a method like this one : 问题在于,在我的代码中,我需要对图像列表进行排序,因此在类别文件(Button + Create)中,我创建了一个类似于以下方法:

- (NSArray*)imagesArray
{
    return [self.images sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDeacriptorWithKey:@"order" ascending:YES]]];
}

But I don't think that's the best way to do because if a need to access this list from multiple area in my code (different classes) then I need to sort the set x times ... Is there a way to add an array (property) in my category in order to sort once and then only retrieve the property ? 但是我不认为这是最好的方法,因为如果需要从我的代码中的多个区域(不同的类)访问此列表,则需要对集合x进行排序...是否可以添加数组(属性)在我的类别中,以便排序一次,然后仅检索该属性?

What are your thoughts about that ? 您对此有何看法?

Thanks 谢谢

You can add properties to categories: http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/ObjectiveC/Chapters/ocAssociativeReferences.html 您可以将属性添加到类别: http : //developer.apple.com/library/ios/#documentation/cocoa/Conceptual/ObjectiveC/Chapters/ocAssociativeReferences.html

Here is another example: 这是另一个示例:

How to add properties to NSMutableArray via category extension? 如何通过类别扩展将属性添加到NSMutableArray?

But

Be aware that you will be responsible to observe changes (eg via KVO) to the initial coredata NSSet property. 请注意,您将负责观察对初始coredata NSSet属性的更改(例如,通过KVO)。
So if changes occur you have to "recalculate" your property properly... 因此,如果发生更改,您必须正确地“重新计算”您的财产...
If your target was iOS 5.0 or higher, I would recommend to use Ordered Sets, but due to the fact that you are not, I could imagine using the way described in the links above 如果您的目标是iOS 5.0或更高版本,我建议您使用有序集,但由于事实并非如此,我可以想象使用上面链接中描述的方式

I think your approach with order property is correct. 我认为您使用订单属性的方法是正确的。 I'd rather create a method in my model something like -(NSArray *)sortedItems which would have an NSFetchRequest with appropriate sort descriptor. 我宁愿在模型中创建一个-(NSArray *)sortedItems之类的方法,该方法将具有带有适当排序描述符的NSFetchRequest。 If you want you can cache the result - save this array in memory. 如果需要,可以缓存结果-将此数组保存在内存中。 Your NSManagedObject can also have properties that are not stored in the database, just in memory. 您的NSManagedObject还可以具有未存储在数据库中的属性,仅存储在内存中。 But I really don't think this is necessary considering the speed and performance of Core Data. 但是考虑到Core Data的速度和性能,我真的没有必要这样做。

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

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