ios核心数据如何实现sql事务函数?

编程入门 行业动态 更新时间:2024-10-26 23:34:52
本文介绍了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事务函数?

本文发布于:2023-11-17 11:38:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1609889.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何实现   函数   核心   事务   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!