[英]iOS how to do pre-filled Core data?
I need my application work as follows: 我需要我的申请工作如下:
1- User download and startup the application. 1-用户下载并启动应用程序。
2- The application will automatically fill the (based core data) with specific records. 2-应用程序将自动使用特定记录填充(基于核心数据)。 3- The application will works ok. 3-该应用程序将正常工作。
4- When the user close the application and restart it. 4-用户关闭应用程序并重新启动时。
5_ The application will not automatically fill the core data with the specific records because it already there. 5_应用程序不会自动使用特定记录填充核心数据,因为它已经存在。
6- The user will not be able to add/remove/update the data. 6-用户将无法添加/删除/更新数据。
Is there a good technique to do that using only core data and accepted by Apple. 是否有一种很好的技术可以仅使用核心数据并被Apple接受。
code: 码:
// Put down default db if it doesn't already exist
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:storePath]) {
NSString *defaultStorePath = [[NSBundle mainBundle]
pathForResource:@"MyDataFile" ofType:@"sqlite"];
if (defaultStorePath) {
[fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
}
}
Not shown, but critical: all the files you prepopulate data for must be marked as "do not backup", or you will be rejected, per the App Store guidelines. 未显示,但很关键:您预填充数据的所有文件必须标记为“不备份”,否则您将被拒绝,符合App Store指南。 See this SO question for some details on how to do this: Why was my application still rejected after excluding files from iCloud backup using this code? 有关如何执行此操作的详细信息,请参阅此SO问题: 为什么在使用此代码从iCloud备份中排除文件后,我的应用程序仍然被拒绝?
I can tell you one very easy way to do it. 我可以告诉你一个非常简单的方法。 In the app delegate, create a variable in NSUserDefaults and change its value when the application is loaded for the first time. 在app delegate中,在NSUserDefaults中创建一个变量,并在第一次加载应用程序时更改其值。 Then depending upon the value of NSUserDefaults, fill the data you want in the Core Data store. 然后,根据NSUserDefaults的值,在Core Data存储中填充所需的数据。 And this will happen only once because the value of NSUserDefaults variable is not going to change again. 这只会发生一次,因为NSUserDefaults变量的值不会再次改变。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.