這是我的問題。我創建了一個coreData NSMAnagedObject,保存它,並且它始終使用相同的對象ID創建一個新的對象...這對我沒有任何敏感,因爲我認爲objectID將是唯一的。我必須失去一樣大,在房間裏的大象的東西,但我不能看到它... ...保存後CoreData 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對象ID實際上是用戶對象ID。所以每次我創建一個對話時,都有用戶的ObjectID。
很抱歉,如果我是鈍的,但我看不到的問題。 'newConversation'打印輸出中顯示它的'objectID'實際上是用戶'objectID'? – pbasdf
沒錯!我正在讀取ObjectID的錯誤方式。 – GrandSteph