保存后CoreData objectID始终相同

时间:2015-07-04 20:14:40

标签: ios core-data nsmanagedobject

这是我的问题。我创建了一个coreData NSMAnagedObject,保存它,它总是创建一个具有相同objectID的新对象...这对我没有任何意义,因为我认为objectID将是唯一的。我必须在房间里遗失像大象一样大的东西,但我看不到它......

这是我的部分代码:

- (Conversation *) saveImagesToCore:(NSData *)userImage andOtherImage:(NSData *)otherImage {

NSManagedObjectContext *moc = [(AppDelegate*)[[UIApplication sharedApplication] delegate] managedObjectContext];

Message *topMessage = [self saveImageToCore:otherImage
                                 andCloudID:self.otherCloudIDsmall
                                    andUser:_friendShaker];
Message *bottomMessage = [self saveImageToCore:userImage
                                    andCloudID:_userCloudID
                                       andUser:_currentUser];

NSSet *messages = [NSSet setWithArray:@[topMessage, bottomMessage]];


NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Conversation"
                                                     inManagedObjectContext:moc];

Conversation *newConversation = (Conversation *)[[NSManagedObject alloc] initWithEntity:entityDescription
                                                         insertIntoManagedObjectContext:moc];

newConversation.dateStarted = [NSDate date];
newConversation.isHost = [NSNumber numberWithBool:_currentUserIsCalling];
newConversation.user = (User *) _friendShaker.userCoreData;
[newConversation addMessage:messages];

NSError *error = nil;

if (![newConversation.managedObjectContext save:&error]) {
    NSLog(@"Unable to save managed object context.");
    NSLog(@"%@, %@", error, error.localizedDescription);
} else {
    NSLog(@"Conversation saved *******");
    NSLog(@"Conversation ID %@",[newConversation objectID]);

    NSDictionary *userInfo = [NSDictionary dictionaryWithObject:newConversation forKey:@"conversation"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ConversationSaved" object:self userInfo:userInfo];
}

return (Conversation *) newConversation;}

感谢您的帮助。我疯了。

这是newConversation对象的打印输出:

<Conversation: 0x16758cd0> (entity: Conversation; id: 0x165073b0 <x-coredata://E31BE071-9ED6-4624-8440-2DC619535A00/Conversation/p7> ; data: {
allowShareOther = 0;
allowShareSelf = 0;
dateLastModified = nil;
dateStarted = "2015-07-05 15:03:24 +0000";
isHost = 1;
message =     (
    "0x165b1780 <x-coredata://E31BE071-9ED6-4624-8440-2DC619535A00/Message/p14>",
    "0x165caa00 <x-coredata://E31BE071-9ED6-4624-8440-2DC619535A00/Message/p13>"
);
preview = nil;
type = nil;
user = "0x16687110 <x-coredata://E31BE071-9ED6-4624-8440-2DC619535A00/User/p3>";})

您会注意到我的问题:newConversation ObjectID实际上是用户ObjectID。所以每次我创建一个对话时,它都有用户的ObjectID。

1 个答案:

答案 0 :(得分:0)

尝试“刷新”objectID:

NSPersistentStoreCoordinator *coordinator =  [(AppDelegate*)[[UIApplication sharedApplication] delegate] persistentStoreCoordinator];
NSURL *uriRep = [newConversation.objectID URIRepresentation];
NSManagedObjectID *realID = [coordinator managedObjectIDForURIRepresentation:uriRep];

NSLog(@"Conversation ID %@",realID);