我创建了一个2个实体来管理应用上的聊天和用户和消息之间的一对多关系.所以用户可以有很多消息,但消息只是你的用户(创建者).
当添加新消息时,我尝试更新ChatUser实体关系,从而建立了ChatUser ID和ChatMessage之间的连接.我可以做到这一点,但是当我去添加一个新的消息到一个现有的userID时出现问题.目前正在实现的一切都是在ChatUser中添加一个额外的userID,而不是仅添加与现有UserID的关系.
NSManagedobjectContext *context = [self managedobjectContext];NSError *error = nil;// 4 . Get Timestamp for Rippllfloat timestamp = @([[NSDate date] timeIntervalSince1970]).floatValue; Nsstring * jayID = @"eu-west-1:be6457ce-bac1-412d-9307-e375e52e22ff"; Nsstring *message = @"ScIEnce string!";// Create a new managed objectChatUser *chatUserManagedobject = [NSEntityDescription insertNewObjectForEntityForname:@"ChatUser" inManagedobjectContext:context];Chat *chatManagedobject = [NSEntityDescription insertNewObjectForEntityForname:@"ChatMessage" inManagedobjectContext:context];Timeline *timelineManagedobject = [NSEntityDescription insertNewObjectForEntityForname:@"Timeline" inManagedobjectContext:context];// 3 . Save Timeline[timelineManagedobject setEvent:chatEvent];[timelineManagedobject setTimestamp:[NSNumber numberWithfloat:timestamp]];[timelineManagedobject setMeta:@""];[timelineManagedobject setVIEwed:@NO];[timelineManagedobject setEventID:jayID];//Save UserMessage[chatManagedobject setChatID:jayID];[chatManagedobject setTimestamp:[NSNumber numberWithfloat:timestamp]];[chatManagedobject setMessage:message];[chatManagedobject setMedia:@""];//Check if value existsNSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityname:@"ChatUser"];[request setPredicate:[nspredicate predicateWithFormat:@"userID = %@",jayID]];[request setFetchlimit:1];NSArray *entitIEs = [[context executeFetchRequest:request error:&error] mutablecopy];if (entitIEs.count == 0) { NSLog(@"GOOD TO ADD"); // no matching object [chatUserManagedobject setUserID:jayID]; //Create Relationship [chatUserManagedobject addChatObject:chatManagedobject];} else { NSLog(@"IT EXISTS!"); [chatManagedobject setChat:chatUserManagedobject];}// Save the object to persistent storeif (![context save:&error]) { NSLog(@"Can't Save! %@ %@",error,[error localizedDescription]);}解决方法 我瘦k,@PangHoMing在正确的轨道上,但使用了Magical Records.我们来完成CD:
首先重命名关系.可能在ChatMessage中应该有一个名为user(或chatUser)的to-1关系,而在ChatUser中应该有一个to-N关系消息(或chatMessages).他们应该是反向关系.
接下来,您应该在创建用户之前询问用户的存在. (否则你创建幻像用户.)你得到它的代码:
// Create message as you dIDChatMessage *message = …;…// The user will go hereChatUser *user; // Do not use types in names unless conversion is subject of your code// Look for an existing oneNSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityname:@"ChatUser"];[request setPredicate:[nspredicate predicateWithFormat:@"userID = %@",jayID]];[request setFetchlimit:1];NSArray *entitIEs = [[context executeFetchRequest:request error:&error] mutablecopy];if (entitIEs.count == 0) { // Only if there is none,create one user = [NSEntityDescription insertNewObjectForEntityForname:@"ChatUser" inManagedobjectContext:context]; // Set-up user's propertIEs …} else { // Use the existing one user = entitIEs[0];}[message setValue:user forKey:@"user"]; // message.user = user;
如其他人所提到的,CD的反向关系.但是,您可以使用反向关系来添加消息,如果这更可读:
[[user mutableSetValueForKey:@"messages"] addobject:message]; // [user addMessagesObject:message]
这也将维持“原”关系.
总结以上是内存溢出为你收集整理的ios – 将新属性添加到现有的核心数据实体关系全部内容,希望文章能够帮你解决ios – 将新属性添加到现有的核心数据实体关系所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)