gpt4 book ai didi

ios - 无法更新从未插入的对象

转载 作者:技术小花猫 更新时间:2023-10-29 10:51:11 26 4
gpt4 key购买 nike

我创建一个类别对象并保存它:

    NSManagedObjectContext *managedObjectContext = [[FTAppDelegate sharedAppDelegate] managedObjectContext];

_category = (Category *)[NSEntityDescription
insertNewObjectForEntityForName:@"Category"
inManagedObjectContext:managedObjectContext];

NSError *error = nil;
[managedObjectContext save:&error];
if (error) {
NSLog(@"error saving: %@",error);
}

然后编辑类别对象的名称并再次保存。

    _category.name = _nameTextField.text;

NSManagedObjectContext *managedObjectContext = [[FTAppDelegate sharedAppDelegate] managedObjectContext];

NSError *error = nil;
[managedObjectContext save:&error];
if (error) {
NSLog(@"error saving: %@",error);
}

并得到这个错误:

 2013-01-12 17:53:11.862 instacat[7000:907] Unresolved error Error Domain=NSCocoaErrorDomain Code=134030 "The operation couldn’t be completed. (Cocoa error 134030.)" UserInfo=0x2027b300 {NSAffectedObjectsErrorKey=(
"<Category: 0x1ed43cf0> (entity: Category; id: 0x1ed52970 <x-coredata://68E5D7B6-D461-4962-BC07-855349DB3263-7000-00000141BAB4C399/Category/tE8AB2F2E-C14C-4E93-8343-CC245B7726622> ; data: {\n categoryId = nil;\n isPrivate = 0;\n name = techies;\n users = (\n );\n})"
), NSUnderlyingException=Cannot update object that was never inserted.}, {
NSAffectedObjectsErrorKey = (
"<Category: 0x1ed43cf0> (entity: Category; id: 0x1ed52970 <x-coredata://68E5D7B6-D461-4962-BC07-855349DB3263-7000-00000141BAB4C399/Category/tE8AB2F2E-C14C-4E93-8343-CC245B7726622> ; data: {\n categoryId = nil;\n isPrivate = 0;\n name = techies;\n users = (\n );\n})"
);
NSUnderlyingException = "Cannot update object that was never inserted.";
}

感谢您的时间和考虑。

我正在使用 AFIncrementalStore。

最佳答案

这样的事情怎么样:

self.category.name = self.nameTextField.text;

NSManagedObjectContext *managedObjectContext = [[FTAppDelegate sharedAppDelegate] managedObjectContext];

if(![self.category isInserted])
{
[managedObjectContext insertObject:self.category];
}

NSError *error = nil;
[managedObjectContext save:&error];
if (error) {
NSLog(@"error saving: %@",error);
}

基本上检查对象是否已经被插入,如果没有,插入它然后保存上下文。

关于ios - 无法更新从未插入的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14300909/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com