gpt4 book ai didi

iphone - CoreData 无法保存上下文,表示关系为零 - 但它肯定已设置

转载 作者:行者123 更新时间:2023-12-03 18:36:07 26 4
gpt4 key购买 nike

当我尝试保存 CoreData 模型的上下文时,收到错误


Unresolved error Error Domain=NSCocoaErrorDomain Code=1560 "The operation couldn’t be completed. (Cocoa error 1560.)" UserInfo=0x76924b0 {NSDetailedErrors=(
"Error Domain=NSCocoaErrorDomain Code=1570 "
The operation couldn\U2019t be completed. (Cocoa error 1570.)"
UserInfo=0x768c410 {NSValidationErrorObject=<Posts: 0x7177990> (entity: Posts; id: 0x7177010 <x-coredata:///Posts/tEF7138E6-A968-45B7-95CF-116C2AA93D605> ; data: {\n "
business_entity"
= nil;\n "<br/>
created_at"
= "
2012-01-21 03:31:30 +0000"
;\n "
own_description"
= "
Roasted this morning- Panama Duncan Estate: A rose fragrance debuts flavors of pear, coconut and marshmallow with caramel throughout the cup."
;\n postsID = 4;\n "
updated_at"
= "
2012-01-21 03:31:30 +0000"
;\n}), NSValidationErrorKey=business_entity, NSLocalizedDescription=The operation couldn\U2019t be completed. (Cocoa error 1570.)}",

关键部分是:

business_entity = nil;<br/>
NSValidationErrorKey=business_entity

但是,当我创建“Posts”对象时,我将它们设置为:

    // First try to find a business for the post
NSNumber* businessID = [NSNumber numberWithInteger: [(NSString*)[jsonInfo objectForKey:@"business_id"] integerValue]];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity: [NSEntityDescription entityForName:kCoreDataModelNameBusinesses inManagedObjectContext: self.managedObjectContext]];
[fetchRequest setPredicate: [NSPredicate predicateWithFormat:@"(businessesID == %i)", [businessID intValue]]];

NSError *error = nil;
NSArray *matchingManagedObjects = [__managedObjectContext executeFetchRequest:fetchRequest error:&error];

if(error) {
NSLog(@"Error-createNewPostsWithJsonInfo: Unresolved error %@, %@", error, [error userInfo] );
return;
}

// Check any results
if( [matchingManagedObjects count] == 0) {
NSLog(@"Error-createNewPostsWithJsonInfo: Could not locate a matching businessId: %@ for postId:%@", businessID, (NSString*)[jsonInfo objectForKey:@"id"]);
}
// Check too many results
if( [matchingManagedObjects count] > 1) {
NSLog(@"Error-createNewPostsWithJsonInfo: Too many bussinesses match businessId: %@ for postId:%@", businessID, (NSString*)[jsonInfo objectForKey:@"id"]);
}

// Final sanity check outputs fine!!
NSLog(@"Business entity %@", (Businesses*)[matchingManagedObjects objectAtIndex:0] );

// Finally create the post
Posts* newPosts = [NSEntityDescription
insertNewObjectForEntityForName:entityName inManagedObjectContext:self.managedObjectContext];

newPosts.created_at = [_dateFormater dateFromString: [jsonInfo objectForKey:@"created_at"] ];
newPosts.updated_at = [_dateFormater dateFromString: [jsonInfo objectForKey:@"updated_at"] ];
newPosts.own_description = [jsonInfo objectForKey:@"description"];
newPosts.postsID = [NSNumber numberWithInteger: [(NSString*)[jsonInfo objectForKey:@"id"] integerValue]];
[newPosts setBusiness_entity: (Businesses*)[matchingManagedObjects objectAtIndex:0] ];

所以我知道 Business_entity 正在设置,并且是正确的。在这种情况下,什么会导致它在保存上下文时出错?

如果有帮助的话,这是模型:

enter image description here enter image description here

最佳答案

万一有人遇到这个问题,错误在于我在 coredatamodel 中设置关系的方式。

这些图像将比我更好地解释,但请确保您在所有者实体中将关系设置为多对多,否则它不会为您动态创建可变集

enter image description here enter image description here

关于iphone - CoreData 无法保存上下文,表示关系为零 - 但它肯定已设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8993258/

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