核心数据:持久存储在Xcode中但不在设备上 [英] Core Data: Persistent Store Works in Xcode but not on Device

查看:90
本文介绍了核心数据:持久存储在Xcode中但不在设备上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的iPhone应用程式中使用Core Data。它工作正常,当我在模拟器中使用它,但当我部署到设备,我的Core Data存储的值没有找到我的应用程序。如果我在iPhone上插入一个值,它会在Core Data中找到值。



我需要在部署到iPhone时保留Core Data的值。



请帮助我


b b b b b b b ul>
  • 准备您的CoreData数据库文件

  • 将其命名为InitialData.sqlite

  • 将其添加到应用程序包



  • ...并且在NSPersistentStoreCreation分配&

      NSString * storePath = ...永久存储协调器的存储路径...; 

    NSFileManager * fileManager = [NSFileManager defaultManager];
    if(![fileManager fileExistsAtPath:storePath]){
    NSString * initialStorePath = [[NSBundle mainBundle] pathForResource:@InitialDataofType:@sqlite];
    if(defaultStorePath){
    [fileManager copyItemAtPath:initialStorePath toPath:storePath error:NULL];
    }
    }

    我假设您要将一些默认数据复制到应用。这是我可以从你的问题中推断出来 - 这是不清楚你真正想要什么。


    I am using Core Data in my iPhone apps. It works fine when I use it in the simulator but when I deploy it to device, the value of my Core Data store is not found to my application. If I insert a value on the iPhone it finds the value in Core Data.

    I need to keep the value of Core Data while deploying it to iPhone.

    Please help me if anyone knows how to do this.

    解决方案

    It's quite easy ...

    • prepare your CoreData database file
    • name it, InitialData.sqlite for example
    • add it to your application bundle

    ... and add something like to this just before NSPersistentStoreCreation allocation & initialization.

    NSString *storePath = ... your store path for persistent store coordinator ...;
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:storePath]) {
      NSString *initialStorePath = [[NSBundle mainBundle] pathForResource:@"InitialData" ofType:@"sqlite"];
      if (defaultStorePath) {
        [fileManager copyItemAtPath:initialStorePath toPath:storePath error:NULL];
      }
    }
    

    I assume you want to copy some default data to your application. That's all I can deduce from your question - it's quite unclear what you really want.

    这篇关于核心数据:持久存储在Xcode中但不在设备上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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