核心数据:NSCocoaErrorDomain = 134040只发生在真实电话,而不是模拟器 [英] Core Data: NSCocoaErrorDomain=134040 Only Occurs On Real Phone, Not Simulator

查看:128
本文介绍了核心数据:NSCocoaErrorDomain = 134040只发生在真实电话,而不是模拟器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Core Data将持久存储保存在我的应用程序的文档目录中。当我尝试保存我的托管上下文更改,我得到一个可可错误134040.这只发生在真正的iPhone在调试模式,而不是在模拟器上。


$ b

这是我最初在文档目录中创建数据存储的方式。

  NSString * documentDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES)objectAtIndex:0];

NSURL * userDataStoreURL = [NSURL fileURLWithPath:[documentDirPath
stringByAppendingPathComponent:@userdata.coredata]];
userDataStore = [persistentStoreCoordinator_ addPersistentStoreWithType:NSBinaryStoreType
configuration:@UserData
URL:userDataStoreURL
options:nil
error:&
NSFileManager * fileManager = [NSFileManager defaultManager];
#以下IF语句从不执行,因此我知道正在创建文件。
if(![fileManager fileExistsAtPath:[userDataStoreURL path]]){
NSLog(@用户数据文件不存在:%@,[userDataStoreURL path]);
}

这是我如何保存上下文:

  [managedObjectContext_ save:& error]; 

这是我在尝试保存托管对象上下文时出现的错误:



错误Domain = NSCocoaErrorDomain Code = 134040操作无法完成。(Cocoa错误134040.)UserInfo = 0x607b80 {NSAffectedStoresErrorKey = $ b< NSBinaryObjectStore:0x23f870>,



),NSUnderlyingException =部分失败:目录不存在不可写入/var/mobile/Applications/...[app捆绑包路径]}

更多澄清



我实际上有2个持久性商店。一个是假设是只读的数据,在升级之间不更改,它驻留在主包。另一个是用户保存的内容的数据存储,它位于文档文件夹中。两个永久存储属于同一个托管对象上下文,但我使用配置只保存每个实体(即只读实体进入一个,用户保存的实体进入另一个)。

  NSPersistentStore * readonlyStore = [persistentStoreCoordinator_ 
addPersistentStoreWithType:NSBinaryStoreType
configuration:@ReadOnlyData
URL:readonlyStoreURL
options:nil
错误:&错误];


解决方案

遇到错误时,这个是在CoreDataErrors.h(还有FoundationErrors.h和其他几个):

  NSPersistentStoreIncompleteSaveError = 134040,//一个或多个商店在保存期间返回错误(商店/失败的对象将在userInfo中)

所以,你不能保存,userInfo告诉你为什么。



确实如此。您的问题:

  NSUnderlyingException =保存部分失败:目录不存在或不可写/ var / mobile / Applications /。 .. [app bundle path]} 

您尝试保存到应用程序包中。这不会工作;您的应用程序包不可写(当然不在设备上)。



您已在文档目录中显示了持久存储的创建。据我所知,通过阅读,这应该是工作。你有任何其他永久存储,也许你打算默认的/从/迁移的内容?如果是,请修改您的问题以包含该代码。


I'm using Core Data to save a persistent store in the document directory of my application. When I attempt to save my managed context changes, I get a Cocoa Error 134040. This only occurs on the real iPhone in debug mode, not on the simulator. Any idea why this occurs?

This is how I initially create my data store in the documents directory.

    NSString *documentDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                                     NSUserDomainMask,
                                                                     YES) objectAtIndex:0];

    NSURL *userDataStoreURL = [NSURL fileURLWithPath:[documentDirPath
        stringByAppendingPathComponent:@"userdata.coredata"]];
    userDataStore= [persistentStoreCoordinator_ addPersistentStoreWithType:NSBinaryStoreType
        configuration:@"UserData"
        URL:userDataStoreURL
        options:nil
        error:&error];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    # The following IF statement never executes, so I know the file is being created.
    if(![fileManager fileExistsAtPath:[userDataStoreURL path]]) {
        NSLog(@"User data file does not exist: %@", [userDataStoreURL path]);
    }

This is how I save the context:

[managedObjectContext_ save:&error];

This is the error I get when trying to save the managed object context:

Error Domain=NSCocoaErrorDomain Code=134040 "The operation couldn’t be completed. (Cocoa error 134040.)" UserInfo=0x607b80 {NSAffectedStoresErrorKey=(
    "<NSBinaryObjectStore: 0x23f870>",
        (
        "<NSBinaryObjectStore: 0x23f870>"
    )
), NSUnderlyingException=Save partially failed: Directory does not exist or is not writable /var/mobile/Applications/...[app bundle path]}

MORE CLARIFICATION

I actually have 2 persistent stores. One is suppose to be read only data that doesn't change in between upgrades and it resides in the main bundle. The other is a data store for things saved by the user, and this resides inside the Document folder. Both persistent stores belong to the same managed object context, but I use configurations to only save certain entities to each (i.e. read only entities go in one, user saved entities goes in another).

NSPersistentStore *readonlyStore = [persistentStoreCoordinator_
  addPersistentStoreWithType:NSBinaryStoreType
  configuration:@"ReadOnlyData"
  URL:readonlyStoreURL
  options:nil
  error:&error];

解决方案

When you get an error, look it up. This one is in CoreDataErrors.h (there are also FoundationErrors.h and a few others):

    NSPersistentStoreIncompleteSaveError             = 134040, // one or more of the stores returned an error during save (stores/objects that failed will be in userInfo)

So, you couldn't save, and the userInfo tells you why.

Indeed it does. From your question:

NSUnderlyingException=Save partially failed: Directory does not exist or is not writable /var/mobile/Applications/...[app bundle path]}

You're trying to save into your app bundle. That won't work; your app bundle is not writable (certainly not on the device).

You showed the creation of a persistent store in the documents directory. As far as I can tell by reading it, that should be working. Do you have any other persistent stores, perhaps that you intended to default to the contents of/migrate from? If so, please edit your question to include that code.

这篇关于核心数据:NSCocoaErrorDomain = 134040只发生在真实电话,而不是模拟器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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