ios核心数据如何实现sql事务函数? [英] ios core data how to implement the sql transaction function?

查看:140
本文介绍了ios核心数据如何实现sql事务函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

喜欢标题。我使用核心数据插入项目,我插入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 *)object

Parameters

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 **)error

Parameters

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事务函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆