快速核心数据导入问题

发布于 2024-11-04 05:39:04 字数 1794 浏览 0 评论 0原文

我在 iPhone 应用程序上使用 Core Data 来存储大约 1000 个对象。每个对象都分配有 5 个不同组之一。随着时间的推移,对象会更改组,并且在每个组中,这些组中对象的顺序会更改并存储在数组中。

然后我获取这个数组并使用属性“name”将其存储到 plist 中。当我重新启动应用程序时,我的 AppDelegate 会扫描数据库中的每个对象,然后将其与 plist 中的“名称”属性进行比较。完成后,我有 5 个核心数据对象数组,按组排序,每个数组都按原始保存的顺序排列。最终的结果是好的。完成这项任务所需的时间则不然。

我想让这更快。如果我可以将实际的核心数据对象数组保存到我的 plist 中,我就会这样做。但我只能保存属性给它。

苹果有 关于高效实现查找或创建的文档,特别是这段代码:

// get the names to parse in sorted order
NSArray *employeeIDs = [[listOfIDsAsString componentsSeparatedByString:@"\n"]
        sortedArrayUsingSelector: @selector(compare:)];

// create the fetch request to get all Employees matching the IDs
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
[fetchRequest setEntity:
        [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:aMOC]];
[fetchRequest setPredicate: [NSPredicate predicateWithFormat: @"(employeeID IN %@)", employeeIDs]];

// make sure the results are sorted as well
[fetchRequest setSortDescriptors: [NSArray arrayWithObject:
        [[[NSSortDescriptor alloc] initWithKey: @"employeeID"
                ascending:YES] autorelease]]];NSError *error = nil;

NSArray *employeesMatchingNames = [aMOC
        executeFetchRequest:fetchRequest error:&error];

然而,fetchRequest 设置了一个 SortDescriptor 并使用“employeeID”键进行排序。我试过这个。我有一个 ID 键属性,可以在将对象添加到组时保存数组索引位置。然而,由于对象在每个组中添加和删除,因此对象的索引不断变化。因此,每次更改后,我都必须重新扫描每个组并重置索引。对我来说,这只会将我的速度问题转移到程序的另一部分。

我的问题是:有没有更好的方法来保存数组中对象的索引位置?或者是否有其他地方可以存储核心数据对象数组?但如果我选择后者,因为我的应用程序已经在应用程序商店中,我的理解是,如果我向数据库添加属性或表,这可能会在用户升级时导致问题。

我希望我解释得很好。任何帮助将不胜感激,谢谢。

I use Core Data on my iPhone app to store about 1000 objects. Each object is assigned one of 5 different groups. Over time, the objects change groups, and within each group, the order of the objects in those groups change and is stored in an array.

I then take this array and use the attribute 'name' to store it to a plist. When I relaunch the app, my AppDelegate scans each object in the database, then compares it to the 'name' attribute in my plist. When this completes, I have 5 arrays of core data objects, sorted by group, each in the original saved order. The end result is good. The time it takes to complete this task is not.

I want to make this faster. If I could save the actual core data object array to my plist, I would do that. But I can only save attributes to it.

Apple has documentation on Implementing Find-or-Create Efficiently, specifically, this code:

// get the names to parse in sorted order
NSArray *employeeIDs = [[listOfIDsAsString componentsSeparatedByString:@"\n"]
        sortedArrayUsingSelector: @selector(compare:)];

// create the fetch request to get all Employees matching the IDs
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
[fetchRequest setEntity:
        [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:aMOC]];
[fetchRequest setPredicate: [NSPredicate predicateWithFormat: @"(employeeID IN %@)", employeeIDs]];

// make sure the results are sorted as well
[fetchRequest setSortDescriptors: [NSArray arrayWithObject:
        [[[NSSortDescriptor alloc] initWithKey: @"employeeID"
                ascending:YES] autorelease]]];NSError *error = nil;

NSArray *employeesMatchingNames = [aMOC
        executeFetchRequest:fetchRequest error:&error];

However, the fetchRequest sets a SortDescriptor and sorts using an 'employeeID' key. I tried this. I have an ID key attribute and can save the array index position when an object is added to a group. However, because objects are being added and removed from each group, the index of the object is constantly changing. So after each change, I would have to rescan each group and reset the index. To me, this would just move my speed problem to another part of my program.

My questions are: is there a better way to save the index position of objects in an array? or is there a different place I can store an array of core data objects? If I did the latter, though, because my app is already in the appStore, my understanding is if I add attributes or tables to the database, this can cause problems when a user upgrades.

I hope I explained this well. Any help would be appreciated, thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

佼人 2024-11-11 05:39:05

在 iPhone 上,除了存储索引之外,没有其他方法可以维护有序集合。该属性是一个整数,对吗?

如果添加属性,则需要以尝试自动迁移的方式设置 persistantStoreCoordinator。如果您之前从未创建过另一个模型版本,那么这将很棘手。你应该尝试一下,看看是否可以让它工作,因为有一天你可能不得不这样做......

我对 CoreData 的感觉是,迁移非常棘手,你应该始终将任何用户生成的数据保留在一边也类似于 plist,您可以从中重新构建数据库。听起来你可能已经在这样做了?如果是这样,您可以在创建 persistantStoreCoordinator 时检查是否失败,如果失败,则删除那里的数据库,再次创建 persistantStoreCoordinator,并从 plist 中填充该数据库。

On the iPhone there is no way to maintain an ordered collection other than storing an index. That property is an integer, correct?

If you add attributes what happens is you need to set up the persistantStoreCoordinator in a way that it attempts an auto-migration. If you never created another model version before though, this will be tricky. You should try that and see if you can get it to work as you are probably going to have to do it someday...

My feeling on CoreData is that the migration is tricky enough you should always keep any user generated data to the side in something like a plist also, that you can re-build a data base from. It sounds like you may already be doing that? If so, you can check when you create the persistantStoreCoordinator if it has failed, and if so then erase the database that is there, create the persistantStoreCoordinator again, and populate that database from your plists.

回忆凄美了谁 2024-11-11 05:39:04

你让这条路变得复杂了。您想要解决的问题(即基于属性值对对象进行排序)是一个非常常见的需求,例如每个表视图都需要这种类型的排序。

排序是您可以为提取提供一个或多个排序描述符的原因。在本例中,您想要对 employeeID 属性进行排序,因此您只需提供对该键的排序即可。获取返回的数组将根据该属性进行排序。

当您添加或删除对象时,您只需直接或通过使用通知重新运行提取即可。如果您使用 NSFetchedResultsController,这一切都会自动为您处理。

如果您需要某种任意顺序,例如用户的收藏夹列表,那么您需要直接在数据模型中对该任意顺序进行建模。根据所需订购的具体类型,有多种方法可以实现这一点。

使用 Core Data 进行设计时,有一条很好的经验法则:如果除了获取返回的数组之外还有任何其他数据结构来保存或排序托管对象,那么您就做错了。

如果使用得当,Core Data 可以在没有任何外部结构的情况下管理整个数据模型。如果您发现自己将额外的数据结构焊接到核心数据上,那么您在数据模型设计中遗漏了一些东西。

You are making this way to complicated. The problem you want to solve i.e. ordering objects based on an attribute value, is a very common need e.g. every single tableview requires this type of ordering.

Ordering is why you can provide one or more sort descriptors to a fetch. In this case you want to order on an employeeID attribute so you just provide a sort that on that key. The array returned by the fetch will be sorted on that attribute.

When you add or remove objects, you just rerun the fetch again either directly or by using notifications. If you use a NSFetchedResultsController, this is all handled for you automatically.

If you need some kind of arbitrary ordering, say a user's list of favorites, then you need to model that arbitrary order directly in the data model. There are various means of doing so depending on the specific type of ordering needed.

Here is a good rule of thumb when designing with Core Data: If you have any other data structure to hold or order managed objects beyond the arrays returned by fetches, you've done something wrong.

Used properly, Core Data can managed the entire data model without any external structures. If you find yourself welding extra data structures onto Core Data, then you've missed something in your data model design.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文