App Bundle 中包含的核心数据存储 [英] Core Data Store included in App Bundle

查看:24
本文介绍了App Bundle 中包含的核心数据存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Apple 文档中找不到对这些步骤的清晰描述...

I can't find a clear description of these steps in Apple docs...

  1. 我的 xcode 项目中有一个 xcdatamodeld
  2. 在启动时,我的应用解析 XML(项目资源)以填充核心数据存储 (SQLLite)
  3. 在我的应用程序的生命周期内,我添加、删除、更新该商店的数据

现在,我想停止在设备上进行繁重的 XML 解析过程,而是直接包含一个包含所需数据的 Store.

Now, I want to stop doing that heavy XML parsing process on device and directly include a Store containing the required data.

我对此有一些疑问:

  • 我可以使用 OS X 应用程序填充商店,然后将该商店包含在我的 XCode-iOs 项目中吗?
  • 我的商店没有出现在 Xcode 中.实际上它是在运行时创建的.如何在项目中添加商店并将其链接到我的 xcdatamodeld ?
  • 我读到这样做会阻止我的商店可写...我想我必须在启动时将它复制到正确的位置(核心数据实用程序教程对此有很大帮助).我说得对吗?

感谢您的提示.URL 或其他 SO 问题将不胜感激!

Thanks for your hints. URL or other SO questions would be really appreciate !

凯洛

推荐答案

您可以在您的应用程序中包含商店文件(大多数情况下是 sqlite db).然后在您的应用程序委托中编辑persistentStoreCoordinator getter merhod :

You can include the store file (sqlite db most of the time) in your app. Then in your app delegate edit the persistentStoreCoordinator getter merhod :

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

    if (persistentStoreCoordinator_ != nil) {
        return persistentStoreCoordinator_;
    }

    NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"CoreDataStore.sqlite"];

    // Check if the store exists in. 
    if (![[NSFileManager defaultManager] fileExistsAtPath:storePath]) {
        // copy the payload to the store location.
        NSString *bundleStore = [[NSBundle mainBundle] pathForResource:@"YourPayload" ofType:@"sqlite"];

        NSError *error = nil;
        [[NSFileManager defaultManager] copyItemAtPath:bundleStore toPath:storePath error:&error];

        if (error){
            NSLog(@"Error copying payload: %@", error);
        }
    }

    NSError *error = nil;
    NSURL *storeURL = [NSURL fileURLWithPath:storePath];
    persistentStoreCoordinator_ = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![persistentStoreCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }    

    return persistentStoreCoordinator_;
}

这篇关于App Bundle 中包含的核心数据存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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