English 中文(简体)
在多处地产上使用NSSeded Request 或NSSed Descriptionor? 使用NSSed Descriptionor 吗? 使用NSSed Descriptions吗? 使用NSSed Descriptions吗? 使用NSSed Descripor?
原标题:Using a NSFetchedRequest or a NSSortedDescriptior on a to-many property?

说我们有一个实体Company ,通过employenes employee 实体有多种关系。

如果我想找到最后一个创建的 Employee (assume Employenee NSDate 属性 created ), 这两种方法中哪一种更好?

  • create a NSFetchRequest on Employee, restricted to "company == %@", someCompany, sort by created, and set the fetchLimit to 1
  • or use sortedArrayUsingDescriptors: on someCompany.employees with a NSSortDescriptor on created and take the first?

第二种方法似乎更加直截了当,但我对实际处理方式感到担心。 第一种方法允许系统定制自定义查询,而第二种方法则进行一些模拟排序。 我猜它归结为许多关系是否懒惰。

最佳回答

根据我个人的经验,我将采取第一种办法。

首先,您不需要像第二步那样执行两步操作。 此外,您还可以对元素拥有完全控制( 您将抓取限制设定为 1 ) 。 您可以在抓取请求时抓取( 例如, 您可以指定要检索的属性, 使用 < code> setPropertysToFetch , 并授权 Core Data 来为您打开错误) 。

然后,当你做 someCompany.employenes 时,你取回了为数不多的有缺陷的物体(例如20个),它们虽然在记忆中,但却在记忆中,它们占据着它。当你应用了某种描述符(可能在此时会向他们开枪)时,你最终会发现19个在记忆中,但对于应用寿命周期没有用处的物体。

<强度 > 个人注释

即使我是一个好的程序员,我相信核心数据框架优化,我会采取第一种方式。

希望能帮上忙

问题回答

Core Data uses lazy loading for this type of relationship. Thus, the first approach seems to be better. But I recommend filling your database with a lot of records and testing both of them. You may also want to read Core Data Performance section.





相关问题
Asynchronous request to the server from background thread

I ve got the problem when I tried to do asynchronous requests to server from background thread. I ve never got results of those requests. Simple example which shows the problem: @protocol ...

objective-c: Calling a void function from another controller

i have a void, like -(void) doSomething in a specific controller. i can call it in this controller via [self doSomething], but i don t know how to call this void from another .m file. I want to call ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

NSUndoManager and runModalForWindow:

I have a simple Core Data app which displays a list of entities in the main window. To create or add new entities, I use a second modal window with a separate managed object context so changes can be ...

NSMutableArray values becoming "invalid"

I m trying to show a database information in a tableview and then the detailed information in a view my problem is as follow: I created a NSMutableArray: NSMutableArray *myArray = [[NSMutableArray ...

iPhone numberpad with decimal point

I am writing an iPhone application which requires the user to enter several values that may contain a decimal point (currency values, percentages etc.). The number of decimal places in the values ...

热门标签