xcode CoreData 1570 错误代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7862191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CoreData 1570 Error Code
提问by xizor
Currently all of my saves are going to memory but are not being written out to disk (iOS). My app is set up with a UITableView
with an Add Modal View presented over this to create content, when the user is done creating the content and the save button is clicked the new Item (NSManagedObject class created by my CoreData Model) I print it out and it is fully filled in. Immediately after this I try to save it to disk and an error message is produced with the same object ID except the fields are nil. In between however my UITableViews - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
method is getting called which is logging @"CanEdit"
.
目前我所有的保存都将进入内存,但没有写入磁盘(iOS)。我的应用程序设置了UITableView
一个添加模态视图来创建内容,当用户完成创建内容并单击保存按钮时,新项目(由我的 CoreData 模型创建的 NSManagedObject 类)我打印出来并它被完全填满。在此之后,我立即尝试将其保存到磁盘,并使用相同的对象 ID 生成一条错误消息,但字段为 nil。然而,在两者之间,我的 UITableViews(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
方法被调用,它正在记录@"CanEdit"
。
Can anyone see what I am doing wrong?
谁能看到我做错了什么?
Here is the code
这是代码
NSLog(@"newItem %@", newItem);
NSError *error;
if (![newItem.managedObjectContext save:&error]) {
// Handle the error.
NSLog(@"%@", error);
}
if (editItem) {
[self.navigationController popViewControllerAnimated:YES];
} else {
[self dismissModalViewControllerAnimated:YES];
}
And Here is my error
这是我的错误
2011-10-22 15:24:46.322 App[42115:fb03] newItem <Item: 0x81a4a30> (entity: Item; id: 0x81a0ab0 <x-coredata:///Item/t7F2B54D2-0DCC-4530-88D5-900BE25C7DC23> ; data: {
containedIn = "0x6e89010 <x-coredata:///Item/t7F2B54D2-0DCC-4530-88D5-900BE25C7DC22>";
contains = (
);
content = a;
dateLastUsed = nil;
depth = 0;
encrypted = 0;
favorite = 0;
favoritePosition = nil;
folder = 0;
fullPath = "^Templates^Add Title";
name = a;
sortPosition = 0;
})
2011-10-22 15:24:46.323 App[42115:fb03] CanEdit
2011-10-22 15:24:46.326 App[42115:fb03] Error Domain=NSCocoaErrorDomain Code=1570 "The operation couldn't be completed. (Cocoa error 1570.)" UserInfo=0x6ecc490
{NSValidationErrorObject=<Item: 0x6e88fb0> (entity: Item; id: 0x6e89010 <x-coredata:///Item/t7F2B54D2-0DCC-4530-88D5-900BE25C7DC22> ; data: {
containedIn = nil;
contains = (
"0x81a0ab0 <x-coredata:///Item/t7F2B54D2-0DCC-4530-88D5-900BE25C7DC23>"
);
content = nil;
dateLastUsed = nil;
depth = 0;
encrypted = 0;
favorite = 0;
favoritePosition = nil;
folder = 1;
fullPath = "^Templates^";
name = Templates;
sortPosition = 0;
}), NSValidationErrorKey=content, NSLocalizedDescription=The operation couldn't be completed. (Cocoa error 1570.)}
回答by logancautrell
The problem is that you have a MO in your context which has required fields set to nil. Specifically this is saying NSValidationErrorKey=content
which in the preceding NSValidationErrorObject
is printing out as nil.
问题是您的上下文中有一个 MO,它的必填字段设置为 nil。具体来说,这是说NSValidationErrorKey=content
前面的哪个NSValidationErrorObject
打印出来为 nil。
Either you have a logic error where you values are not getting set in the MO correctly, or you should change your model to make that field optional.
要么您有一个逻辑错误,即您的值未在 MO 中正确设置,要么您应该更改模型以使该字段成为可选字段。
回答by Mundi
From your error output above, you can see that there are two different objects, one with the address 0x6e89010
containing your data, another with the address 0x6e88fb0
where required fields are nil
.
从上面的错误输出中,您可以看到有两个不同的对象,一个的地址0x6e89010
包含您的数据,另一个的地址为0x6e88fb0
必填字段nil
。
The source of this error must be contained in code that you did not post.
此错误的来源必须包含在您未发布的代码中。
My recommendation in order to avoid these kind of problems is to follow the following design pattern which is also used in Apple's demos:
为了避免此类问题,我的建议是遵循以下设计模式,该模式也在 Apple 的演示中使用:
- Pass the managed object context as a property to the modal view controller. It is advisable to only have one managed object context.
- Create a new managed object when the input controller starts with
[NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:self.managedObjectContext];
- As the user inputs the data, assign the properties / attributes to your new object right away.
- When the user hits "Save", save the changes with
[self.managedObjectContext save:&error];
- If the user cancels, delete the object from the context with
[self.managedObjectContext deleteObject:insertedObject];
- 将托管对象上下文作为属性传递给模态视图控制器。建议只有一个托管对象上下文。
- 当输入控制器开始时创建一个新的托管对象
[NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:self.managedObjectContext];
- 当用户输入数据时,立即将属性/属性分配给您的新对象。
- 当用户点击“保存”时,保存更改
[self.managedObjectContext save:&error];
- 如果用户取消,从上下文中删除对象
[self.managedObjectContext deleteObject:insertedObject];
This is very efficient and tends to avoid stray object errors.
这是非常有效的,并且可以避免杂散对象错误。