我有一个非常烦人的问题,我似乎无法解决。
当我发送一条保存到核心数据的消息时,我有一个视图,完成后它会向数据库询问随机消息(句子)并将其保存到数据库中的另一行。
如果我对最后一部分进行硬编码,而不从数据库中获取数据,它工作得很好而且很花哨,但是一旦我从数据库中获取随机行,它就会发疯。
在我的 AppDelegate.m 中:
- (void)save {
NSAssert(self.context != nil, @"Not initialized");
NSError *error = nil;
BOOL failed = [self.context hasChanges] && ![self.context save:&error];
NSAssert1(!failed,@"Save failed %@",[error userInfo]);
}
- (NSString*)selectRandomSentence
{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Sentences" inManagedObjectContext:self.managedObjectContext];
[request setEntity:entity];
NSError *error = nil;
NSUInteger count = [self.context countForFetchRequest:request error:&error];
NSUInteger offset = count - (arc4random() % count);
[request setFetchOffset:offset];
[request setFetchLimit:1];
NSArray *sentenceArray = [self.context executeFetchRequest:request error:&error];
[request release];
return [[sentenceArray objectAtIndex:0] sentence];
}
- (NSManagedObjectContext *)context {
if (_managedObjectContext != nil)
return _managedObjectContext;
NSPersistentStoreCoordinator *coordinator = [self coordinator];
if (coordinator != nil) {
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return _managedObjectContext;
}
在我的 ChatController.m 中:
- (void)didRecieveMessage:(NSString *)message
{
[self addMessage:message fromMe:NO];
}
#pragma mark -
#pragma mark SendControllerDelegate
- (void)didSendMessage:(NSString*)text {
[self addMessage:text fromMe:YES];
}
#pragma mark -
#pragma mark Private methods
- (void)responseReceived:(NSString*)response {
[self addMessage:response fromMe:NO];
}
- (void)addMessage:(NSString*)text fromMe:(BOOL)fromMe {
NSAssert(self.repository != nil, @"Not initialized");
Message *msg = [self.repository messageForBuddy:self.buddy];
msg.text = text;
msg.fromMe = fromMe;
if (fromMe)
{
[self.bot talkWithBot:text];
}
[self.repository asyncSave];
[self.tableView reloadData];
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:[self.buddy.messages count] - 1] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
在我的 OfflineBot.m 中:
- (void)talkWithBot:(NSString *)textFromMe
{
AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[self didRecieveMessage:[delegate selectRandomSentence]];
}
- (void)didRecieveMessage:(NSString *)message
{
if ([self.delegate respondsToSelector:@selector(didRecieveMessage:)])
[self.delegate didRecieveMessage:message];
}
存储库.m
- (Message*)messageForBuddy:(Buddy*)buddy {
Message *msg = [self.delegate entityForName:@"Message"];
msg.source = buddy;
[self.delegate.managedObjectContext refreshObject:buddy mergeChanges:YES];
return msg;
}
- (void)asyncSave {
[self.delegate save];
}
错误:
2012-08-10 00:28:20.526 聊天 [13170:c07] * -[AppDelegate 保存] 中的断言失败,/Users/paulp/Desktop/TestTask/Classes/AppDelegate.m:28 2012-08-10 00:28 :20.527 Chat[13170:c07] *由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“保存失败 {type = immutable dict, count = 2, entries => 1 : {contents = "NSAffectedObjectsErrorKey"} = (" (实体:句子;id:0x6b8bf10;数据:)") 2:{contents = "NSUnderlyingException"} = CoreData 无法满足 '0x6b8bf10' 的错误}
我究竟做错了什么?
更新 我指出了这一行的错误:
NSArray *sentenceArray = [self.context executeFetchRequest:request error:&error];
当我执行该行时,我得到了错误......那是在获取数据时。但是,将新数据保存到 Messages 实体时似乎出现了错误。随机句子是从句子中获取的。
在我将 asyncSave 方法更改为直接保存(因此不使用新线程)后,它会保存第一次聊天,但之后什么也没有。它死了。
更新
这一切似乎都在我的didFinishLaunchingWithOptions
:
[self.context setRetainsRegisteredObjects:YES];
我了解 CodeData 对象模型上下文不会释放其对象,这似乎是添加和保存之间的问题。但为什么?