ios – 将新属性添加到现有的核心数据实体关系

前端之家收集整理的这篇文章主要介绍了ios – 将新属性添加到现有的核心数据实体关系前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我不知道我对Core Data关系的理解是否有缺陷,因为我似乎无法实现我想做的事情.

我创建了一个2个实体来管理应用上的聊天和用户和消息之间的一对多关系.所以用户可以有很多消息,但消息只是你的用户(创建者).

添加新消息时,我尝试更新ChatUser实体关系,从而建立了ChatUser ID和ChatMessage之间的连接.我可以做到这一点,但是当我去添加一个新的消息到一个现有的userId时出现问题.目前正在实现的一切都是在ChatUser中添加一个额外的userId,而不是仅添加与现有UserId的关系.

NSManagedObjectContext *context = [self managedObjectContext];
NSError *error = nil;

// 4 . Get Timestamp for Rippll
float timestamp = @([[NSDate date] timeIntervalSince1970]).floatValue;
 NSString * jayID = @"eu-west-1:be6457ce-bac1-412d-9307-e375e52e22ff";

  NSString *message = @"Science string!";

// Create a new managed object
ChatUser *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 exists
NSFetchRequest *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 store
if (![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 did
ChatMessage *message = …;
…

// The user will go here
ChatUser *user; // Do not use types in names unless conversion is subject of your code

// Look for an existing one
NSFetchRequest *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]

这也将维持“原”关系.

原文链接:https://www.f2er.com/iOS/337337.html

猜你在找的iOS相关文章