以某些数据预填充核心数据的方式 [英] Way to Prepopulate Core Data with Certain Data

查看:138
本文介绍了以某些数据预填充核心数据的方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常我用来自互联网的数据填充我的核心数据。

Usually I populate my core data with data from internet.

但是我想要用数据预填充它,而不从互联网上下载。

But say I want to prepopulate it with data without downloading it from internet.

说我要先存储所有城市或所有标签。

Say I want to store all cities, or all tags first.

这样做的标准方法是什么?

What would be the standard way to do so?

我应该在plist上存储城市吗?
我应该硬编码插入所有数据吗?
我应该在coredata中设置一个变量来告诉它是否已经初始化了?

Should I store the cities on plist? Should I hardcode inserting all data? Should I set a variable in coredata to tell whether it has been initialized or not?

你们做什么?

推荐答案

如果是大量数据,最快的方法是提供一个填充的存储。假设你使用一个sqlite数据库,你可以简单地使用模拟器生成一次,然后把它放在你的应用程序中。

If it's a lot of data, the fastest way is to provide a populated store. Assuming you're using a sqlite database, you can simply use the Simulator to generate it once, and then put that in your app.

如果是少量的数据,我在第一次开始插入数据,同时从某种形式的xml,csv或甚至硬编码(脚本生成)插入语句读取它。

If it's a small amount of data, I'd insert the data at first start, while reading it from some form of xml, csv, or even hardcoded (script-generated) insert statements. It all depends on the situation.

当使用Core Data不是只读时,应该将数据库复制到允许写入的位置,当应用程序第一次启动。在只读情况下,您只能使用包中的数据库。

When using Core Data not read-only, you should copy the database to a place where you're allowed to write, when the app starts for the first time. In a read only situation, you can just use the database from the bundle.

这篇关于以某些数据预填充核心数据的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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