桌子
RoomOccupant < occupantid,relations(room,contact)>
因此,房间和联系人是 coreData 中的实体。从房间和联系人到 RoomOccupant 有一对多的关系。我已经定义了 addvalue、removevalue、addobjects 等,以正确添加 roomoccupant 关系。
因此,在创建 roomoccupant 时,我正在设置房间和居住者的联系方式。
occupant.room = room1, occupant.contact = contact1,因此 room1 和 contact1 是在 coreData 中创建的。
但它在“occupant.contact = contact1”声明中崩溃。如果解决了将不胜感激。提前致谢。
代码:
-(BOOL) setChatRoomOccupant:(RoomOccupant*)occupant
{
CDChatRoomOccupants* occupantExisted = [self getChatRoomOccupant:occupant.occupantid];
CDObjectFactory* factory = [[MobileClient getMobileClient] getFactory];
if (!occupantExisted)
{
CDChatRoom* room = [self setChatRoom:occupant.room];
CDContact* contact = [factory setContact:occupant.contact];
CDChatRoomOccupants* newOccupant = [NSEntityDescription insertNewObjectForEntityForName:@"CDChatRoomOccupants" inManagedObjectContext:localContext];
newOccupant.occupantid = occupant.occupantid;
if (contact) {
NSLog(@"contact details:%@,%@",contact.phoneNumber,contact.firstName);
newOccupant.contact = contact;
}
if (room) {
newOccupant.room = room;
}
NSError* error = nil;
if (![localContext save:&error]) {
return true;
}
}
else{
occupantExisted.occupantid = occupant.occupantid;
occupantExisted.room = [self setChatRoom:occupant.room];
occupantExisted.contact = [factory setContact:occupant.contact];
NSError* error = nil;
if (![localContext save:&error]) {
return true;
}
}
return false;
}