所以我刚刚将我的Core Data模型转换为代码,因此它可以很容易地适应静态库。不幸的是它不是一个平稳的转换。
尝试将新播放器作为另一个名为gameToPlayer的类的关系插入时,我收到此错误。
Unacceptable type of value for to-one relationship: property = "player"; desired type = (null); given type = Player; value = <Player: 0x178ba9e0>
这是GameToPlayer的创作:
gameToPlayerEntity = [[NSEntityDescription alloc] init];
[gameToPlayerEntity setName:@"GameToPlayer"];
[gameToPlayerEntity setManagedObjectClassName:@"GameToPlayer"];
gameToPlayerPlayer = [[NSRelationshipDescription alloc] init];
[gameToPlayerPlayer setDestinationEntity:playerEntity];
[gameToPlayerPlayer setInverseRelationship:playerGames];
[gameToPlayerPlayer setDeleteRule:NSNullifyDeleteRule];
[gameToPlayerPlayer setOptional:YES];
[gameToPlayerPlayer setName:@"player"];
[gameToPlayerPlayer setMaxCount:1];
[gameToPlayerPlayer setMinCount:1];
玩家创作:
playerEntity = [[NSEntityDescription alloc] init];
[playerEntity setName:@"Player"];
[playerEntity setManagedObjectClassName:@"Player"];
playerGames = [[NSRelationshipDescription alloc] init];
[playerGames setDestinationEntity:gameToPlayerEntity];
[playerGames setInverseRelationship:gameToPlayerPlayer];
[playerGames setDeleteRule:NSCascadeDeleteRule];
[playerGames setName:@"games"];
[playerGames setOptional:YES];
[playerGames setMaxCount:0];
[playerGames setMinCount:0];
发生崩溃的地方(抱歉所有的NSLog都使用它们来追踪它)它停在我们设置玩家的地方:
NSLog(@"Create the GameToPlayer");
GameToPlayer *orderObj = [NSEntityDescription insertNewObjectForEntityForName:@"GameToPlayer" inManagedObjectContext:[SDDataManager dataManager].managedObjectContext];
NSLog(@"Set the Game");
[orderObj setGame:newGame];
NSLog(@"Set the Player");
[orderObj setPlayer:player];
NSLog(@"Set the Order");
[orderObj setOrder:[NSNumber numberWithInt:[selectedPlayers indexOfObject:player]]];
NSLog(@"Add to Array");
[orderObjs addObject:orderObj];
现在我实际上认为我知道这个问题。在我的ManagedObjectModel的自定义init中。 gameToPlayerEntity是在playerEntity之前创建的。所以当创建gameToPlayerEntity时,playerEntity实际上是NULL。但是,我不能简单地重新排序,使它们成为属性,或做任何事情来修复它,因为如果我更改了文件中的任何内容,它似乎不再能够映射现有的数据库......
我在文档中找不到任何地方,它显示了如何编写像xml模型那样的代码模型,我希望这是一个问题。任何帮助或提示赞赏。只是试着弄清楚如何使这个工作。
更新
值得一提的是,在进行此转换之前,我已经在使用轻量级迁移,并且是在数据库的第4次迭代中。
更新2
我已经完全重新开始工作了。而不是试图继续发布。所有内容都已转换为属性,以便在需要时创建每个实体,并且只创建一次。这可以作为从Visual Model到Code Model的转换。 然而 当这个模型不可避免地发生变化时,我肯定需要了解未来的版本控制。版本控制是这个问题的根源。因此,如果有人能够在不使用xcode界面的情况下阐明如何进行版本控制,我很乐意听到您的想法。
答案 0 :(得分:2)
我认为这不一定是你的问题,但我确实想为那些也得到这个错误的人发布另一种可能性。如果在将数据从一个持久性存储移动到另一个持久存储时看到此错误(例如,从后备存储移动到iCloud存储),则您用于创建新管理对象的NSEntityDescription可能来自您的旧副本对象(以及错误的托管对象上下文)。
当您执行以下代码时:
NSEntityDescription *entity = [oldObj entity];
YourClass *newObj = [[YourClass alloc] initWithEntity:entity
insertIntoManagedObjectContext:moc];
您需要采取额外步骤,从您插入的上下文中获取实体描述。
NSEntityDescription *entity = [oldObj entity];
NSEntityDescription *goodEntity = [NSEntityDescription entityForName:entity.name
inManagedObjectContext:moc];
YourClass *newObj = [[YourClass alloc] initWithEntity:goodEntity
insertIntoManagedObjectContext:moc];
感谢Pavel Kapinos关于Cocoa Dev电子邮件列表的说明。 http://www.mail-archive.com/cocoa-dev@lists.apple.com/msg24863.html
答案 1 :(得分:0)
在方法开头初始化实体和关系然后设置所有属性是否太难了?
playerEntity = [[NSEntityDescription alloc] init];
gameToPlayerEntity = [[NSEntityDescription alloc] init];
gameToPlayerPlayer = [[NSRelationshipDescription alloc] init];
然后在该方法的某个时间点:
[gameToPlayerPlayer setDestinationEntity:playerEntity];