喜欢标题。我使用核心数据插入项目,我插入100项,它太慢。如何提高插入速度?核心数据有交易功能?
解决方案您正在寻找的是在Core Data中 save 所有100个对象已经被插入,而不是在每个插入之后。
当对象被插入到核心数据中时,它们只存在于内存中。要将新对象保存到光盘中,您应该保存所有更改(插入,更新和删除),并将它们一起写入光盘。
如果您查看 -insertObject 的文档(下面)你可以读取插入一个对象只注册要保存更改时插入的对象。
insertObject:在下次保存更改时注册要插入接收器持久存储的对象。 p>
- (void)insertObject:(NSManagedObject *)object参数
b
托管对象。
进一步查看 -save的文档: (下面),您将确保(尝试)保存所有未保存的更改,(在您的情况下所有100个插入的项目)。
save:尝试将已保存的更改提交到其持久存储库。
- (BOOL)save:(NSError **)error参数
一个 NSError 对象。您不需要创建 NSError 对象。如果您通过 NULL ,保存操作将在第一个失败后中止。
返回值<
是如果保存成功,否则 NO 。
like title. I use core data to insert item, i insert 100 items , it`s too slow. How to up the insert speed? Core Data whicher has transaction funcation?
解决方案What you are looking for is to save in Core Data after all 100 objects have been inserted as opposed to after each insert.
When objects are inserted into Core Data they are only present in memory. To persist your new objects to disc you should save which will take all changes (inserts, updates and deletes) and write them to disc together.
If you look at the documentation for -insertObject (below) you can read that inserting an object only registers the object for being inserted when changes are saved.
insertObject:Registers an object to be inserted in the receiver’s persistent store the next time changes are saved.
- (void)insertObject:(NSManagedObject *)objectParameters
object
A managed object.
By further looking at the documentation for -save: (below) you will se that it will (attempt to) save all the unsaved changed, in your case all 100 inserted items.
save:Attempts to commit unsaved changes to registered objects to their persistent store.
- (BOOL)save:(NSError **)errorParameters
error
A pointer to an NSError object. You do not need to create an NSError object. The save operation aborts after the first failure if you pass NULL.
Return Value
YES if the save succeeds, otherwise NO.
更多推荐
ios核心数据如何实现sql事务函数?
发布评论