使用Core Data发送消息更新实体到解除分配的实例

时间:2011-03-24 17:20:05

标签: iphone objective-c core-data

我在尝试更新实体上的值时遇到了一个非常奇怪的错误。我是一个新的核心数据,所以可能是一些愚蠢的......

这是我的更新代码:

-(BOOL)trataTarefa:(TarefaMap*) mapa{

NSFetchRequest *request =[[NSFetchRequest alloc] init];
NSManagedObjectContext *context = [[DataSource sharedManager]managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Tarefa" inManagedObjectContext:context];
[request setEntity:entity];
NSPredicate *predicate;
predicate = [NSPredicate predicateWithFormat:@"cd_tarefa == %@ AND cd_instalacao contains[cd] %@ ", mapa.cd_tarefa, mapa.cd_instalacao];
[request setPredicate:predicate];

NSError *error=nil;
NSArray *result = [context executeFetchRequest:request error:&error];
[request release];
NSLog(@"result=%d",[result count]);

if([result count]==1){
    if (mapa.tp_operacao ==3){

        Tarefa* tarefa = [result objectAtIndex:0];
        [tarefa setTp_operacao:[NSNumber numberWithInt:3]]; 


        NSLog(@"Tarefa=%@", tarefa);
        NSError* saveError=nil;
        if (![context save:&saveError]) {
            NSLog(@"Saving changes to book book two failed: %@", saveError);
        } else {
            NSLog(@"Teoricamente gravou...") ;   

        }            
    }
}    
return YES;

}

代码有效,它保存条目,但它返回 * - [CFNumber release]:发送到解除分配的实例的消息##### 用shell malloc_history跟踪它,我得到:

Date/Time:       2011-03-24 14:12:01.988 -0300
OS Version:      Mac OS X 10.6.6 (10J567)
Report Version:  7

ALLOC 0x6603f60-0x6603f6f [size=16]: thread_a052f540 |start | main | UIApplicationMain |
-[UIApplication _run] | CFRunLoopRunInMode | CFRunLoopRunSpecific | __CFRunLoopRun |  
 __CFRunLoopDoSource1 | __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ | 
 PurpleEventCallback | _UIApplicationHandleEvent | -[UIApplication sendEvent:] 
| -  [UIApplication handleEvent:withNewEvent:] | -[UIApplication
 _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] | -
[UIApplication _createStatusBarWithRequestedStyle:orientation:hidden:] | -[UIStatusBar
requestStyle:animated:] | -[UIStatusBar requestStyle:animationParameters:] | -  
[UIStatusBarWindow setCornerStyle:] | -[UIImageView initWithImage:] | -[UIImageView 
setImage:] | -[UIImageView(UIImageViewInternal) _updateState] | -
[UIImageView(UIImageViewInternal) _canDrawContent] | -[UIView(Rendering) contentMode] |
+[NSNumber numberWithInt:] | -[NSPlaceholderNumber initWithInt:] | CFNumberCreate |
_CFRuntimeCreateInstance | malloc_zone_malloc 

Binary Images:
0x1000 -    0x5cff3 +Nonononono (??? - ???) <C4B33680-F374-3E37-9C91-6DEDE06C4701> 
 /Users/marsson/Library/Application Support/iPhone Simulator/4.1/Applications/B13321A3-
 1E73-4DC0-8E37-E2E56116ECEA/Nonononoo.app/Nnonononononono

我可以从该日志中获取的是该对象未真正取消分配... 我已经尝试使用setValue更新对象:forKey具有相同的结果...如果我在更新上注释该行,则代码运行,但当然对象不会更新。 任何人都有类似的问题??

提前致谢!

2 个答案:

答案 0 :(得分:1)

实际上,这是一个愚蠢的线程问题......我试图从后台线程更新ManagedContext,它有时会崩溃应用程序。一旦我使用了不同的托管上下文,并使用通知中心来同步上下文,一切都顺利进行。谢谢大家的建议......

答案 1 :(得分:0)

[tarefa setTp_operacao:]看起来有问题。你可以将代码发布到Tarefa类吗?您创建的NSNumber是自动释放的,因此如果Tarefa在运行setTP_operacao时没有保留它,您将在自动释放池发布后访问一个解除分配的对象(可能在当前UI事件结束时)。