简体   繁体   English

如何提高iPhone上Core Data对象插入的性能?

[英]How do I improve performance of Core Data object insert on iPhone?

I'm trying to import a large amount of data into a core data store on the iPhone. 我正在尝试将大量数据导入到iPhone上的核心数据存储中。 I'm using a SQLite backing for the core data store. 我正在使用SQLite支持核心数据存储。 It seems to be taking way longer than I would expect it to. 这似乎比我预期的要长。 I've trimmed down the routines so that it is basically just attempting to a fetch an object (to see if it already exists) and then create a new object if it doesn't (they never do since I am importing data). 我已经减少了例程,因此它基本上只是尝试获取一个对象(看它是否已经存在)然后创建一个新对象(如果它没有)(他们从来没有这样做,因为我导入数据)。 The fetching isn't the time consuming part, though. 但是,提取并不是耗时的部分。 It's the creation of the objects. 这是对象的创建。 Basically, the offending code is: 基本上,违规代码是:

MobileObject *newObject = (MobileObject *)[NSEntityDescription insertNewObjectForEntityForName:objDesc inManagedObjectContext:managedObjectContext];

I've noticed that on the simulator, it is fairly quick at the start with about 100 objects created a second. 我注意到在模拟器上,它开始时相当快,大约有100个对象创建了一秒钟。 It slows down though and by the time five thousand objects are created it's almost 2 seconds for 100 objects and by the time ten thousand objects are created, it's 4 seconds per 100 objects. 它虽然减慢了,但是当创建五千个对象时,对于100个对象几乎是2秒,并且当创建一万个对象时,每100个对象为4秒。 The whole group of 21000 objects takes more than 10 minutes. 整组21000个对象需要10分钟以上。 That is with all the actual useful code taken out (that's just a fetch and an object create). 这就是取出所有实际有用的代码(这只是一个fetch和一个对象创建)。 And it's much much slower on the actual device (by maybe 4 times as much). 它在实际设备上要慢很多(可能是4倍)。

What I don't understand is why core data starts off fast but then begins to slow down. 我不明白的是为什么核心数据快速启动但随后开始减速。 I've tried both with index and no indexes on my data. 我已经尝试了索引和我的数据没有索引。 I've tried creating my own autorelease pool which I periodically drain in my loop. 我已经尝试创建自己的自动释放池,我会定期在循环中耗尽。 I've tried saving after every object creation. 我在每个对象创建后都尝试过保存。 I've tried waiting until the end to save. 我试过等到最后才能保存。 But no matter what I do, the performance still seems miserable. 但无论我做什么,表现仍然看起来很悲惨。 Is it just that slow to add a new object to a core data store with a few thousand objects in it? 将新对象添加到核心数据存储中并且其中包含几千个对象,是否会很慢? Any suggestions? 有什么建议么?

It can be quite speedy but it depends on what you are doing. 它可以很快,但这取决于你在做什么。 As others have suggested you should be looking at Instruments and finding the actual hotspot. 正如其他人所建议你应该看看仪器并找到实际的热点。 Also posting the actual import code would help to identify the issue. 发布实际的导入代码也有助于识别问题。

I'm having this problem as well. 我也有这个问题。 I'm importing 100,000 simple objects from a remote data store, and have followed Apple's notes about importing in batches. 我正在从远程数据存储中导入100,000个简单对象,并且已经按照Apple关于批量导入的说明进行操作。 The first few batches (of 2000 objects each) start off reasonably, and then the saves slow to a crawl. 前几批(每批2000个对象)开始合理,然后保存缓慢爬行。

I've partitioned the data further into batches of 50, and it was still slow. 我已将数据进一步划分为50个批次,但仍然很慢。 Instruments shows that saves are taking longer and longer each time. 仪器显示每次保存时间越来越长。 Not sure why, I'm creating a new context for each batch, and the undo manager is set to nil. 不知道为什么,我正在为每个批处理创建一个新的上下文,并且撤消管理器设置为nil。

Unfortunately, I don't have anything useful to add to this conversation ... yet. 不幸的是,我没有任何有用的东西可以添加到这个对话中...... Will update later. 稍后会更新。

Try using Instruments. 尝试使用仪器。 Don't you save after inserting every single object? 插入每个对象后不保存吗? Actually, more insert-related code and scheme may be very useful. 实际上,更多与插入相关的代码和方案可能非常有用。

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

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