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

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

问题描述

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

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


  1. 我在xcode项目中有一个xcdatamodeld

  2. 在启动时,我的应用程序解析XML(项目资源)以填充Core Data Store(SQLLite)。

  3. 我添加,删除,更新该商店的数据

现在,我想停止在设备上执行那么繁重的XML解析过程, a。包含所需数据的商店。

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?

  • 我读过,这样做会阻止我的商店写入...我想我必须复制它在正确的地方在启动时(Core Data实用程序教程是一个伟大的帮助)。我是对吗?

感谢您的提示。 URL或其他SO问题将非常感谢!

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

Kheraud

推荐答案

您可以在应用程序中包含商店文件(大多数时候都是sqlite db)。
然后在您的应用程序委托编辑persistentStoreCoordinator getter方法:

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天全站免登陆