为什么iOS通过UIManagedDocument保存Core数据时出现延迟? [英] Why does iOS delay when saving Core Data via a UIManagedDocument?

查看:99
本文介绍了为什么iOS通过UIManagedDocument保存Core数据时出现延迟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Core Data在SQLite数据库中保留一些信息。我使用模拟器进行测试,并使用 sqlite3 查询数据库,并验证我是否存储了我的期望。

I'm using Core Data to persist some information in a SQLite database. I'm testing with the simulator, and using sqlite3 to query the database and verify that I'm storing what I expect.

我看到的是,数据不会出现在SQLite数据库中,直到一个好的15-20秒后,我保存它。

What I'm seeing is that the data doesn't appear in the SQLite database until a good 15-20 seconds after I've saved it.

这里是我把我的保存到下面的代码:

Here's the code that I've cut my save down to:

NSEntityDescription *customerType = [NSEntityDescription entityForName:@"CustomerType" inManagedObjectContext:context];
CustomerType *ct = [[CustomerType alloc]initWithEntity:customerType insertIntoManagedObjectContext:context ];
ct.code = code;

NSError* error = nil;
if (![context save:&error] || error)
    NSLog(@"Saved new customer (error=%@)", [error debugDescription]);

保存操作完成无误。

任何提示为什么我看到这个延迟?我的保存操作未正常运行,或者是延迟保存数据是模拟器的工件吗?

Any hints as to why I'm seeing this delay? Is my save operation not running properly, or is the delay in persisting the data an artifact of the simulator?

推荐答案

解决方案! / h2>

我通过 UIManagedDocument 创建 NSManagedObjectContext 。为了及时持久化数据,我需要保存托管文档,而不仅仅是保存对托管对象上下文中对象图所做的更改。

Solution!

I create the NSManagedObjectContext via a UIManagedDocument. To persist the data in a timely manner I need to save the managed document, not just save the changes I've made to the object graph in the managed object context.

这篇关于为什么iOS通过UIManagedDocument保存Core数据时出现延迟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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