Iphone核心数据崩溃保存 [英] Iphone Core Data crashing on Save

查看:208
本文介绍了Iphone核心数据崩溃保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Core Data编写Iphone应用程序,并且在[managedObjectContext save:&& error]代码行中出现 EXC_BAD_ACCESS 错误。此崩溃只有在我修改了某些字段后才会发生。更具体地说,我的实体有两个字符串字段(大约10个字段),从一个模态视图控制器(如文本编辑器)的返回获得它们的值。崩溃也只发生在这些字段编辑后,第一次我把一个值在它工作正常。

I'm currently writing an Iphone application using Core Data and I get a EXC_BAD_ACCESS error during the [managedObjectContext save:&&error] code line. This crash only happens after I modify certain fields. More specifically my entity has two string fields (out of about 10 fields), that get their values from a the return of a modal view controller (like a text editor). The crash also only happens after these fields are edited, the first time I put a value in it works fine.

我有字符串格式构造函数与字符串的原因是因为我试图复制构造...不知道如果这种情况自动发生?思想可能保留/释放那些字符串的消息(这两个来自模态视图控制器),被解散的模态视图控制器或东西被释放。

The reason I have string with format constructors with just strings is because I was trying to copy construct... not sure if that happens automatically? Thought maybe retain/release messages from those strings (those two are from the modal view controller), were getting released on dismissal of the modal view controller or something. Guess not though because it still doesn't work.

这是代码段崩溃:

EDITED]

        - (void)actionSheet:(UIActionSheet *)modalView clickedButtonAtIndex:    (NSInteger)buttonIndex
      switch(buttonIndex) {
              case 0: {
                if(message == nil) {
                  message = [NSEntityDescription insertNewObjectForEntityForName:@"MailMessage" inManagedObjectContext:self.managedObjectContext];
                }
                message.toString = txtTo.text;
                message.fromString = txtFrom.text;
                message.subjectString = txtSubject.text;
                message.backgroundColor = [NSNumber numberWithInt:[bgColor intValue]];
                message.textArray = [NSString stringWithFormat:@"%@", stringTextArray];
                message.htmlString = [NSString stringWithFormat:@"%@", stringHTML];
                message.timeStamp = [NSDate date];
                message.statusCode = [NSNumber numberWithInt:0];
                NSError *error = nil;
                if (![message.managedObjectContext save:&error]) {
                    abort();
                }   
                break;
               }
              case 1: {
             break;
              }
      }
      if(buttonIndex != modalView.cancelButtonIndex) {
      [webViewBody loadHTMLString:@"<html><head></head><body></body></html>" baseURL:[NSURL URLWithString:@""]];
      [self.navigationController popToRootViewControllerAnimated:YES];
}

}

这是崩溃日志:


Exception Type:  EXC_BAD_ACCESS (SIGBUS)
Exception Codes: KERN_PROTECTION_FAILURE at 0x00000015
Crashed Thread:  0

Thread 0 Crashed:
0   libobjc.A.dylib                 0x30011940 objc_msgSend + 20
1   CoreData                        0x367f7d3e -[NSKnownKeysDictionary1 dealloc] + 82
2   CoreData                        0x367f7cda -[NSKnownKeysDictionary1 release] + 34
3   CoreData                        0x3687eec4 -[NSManagedObject(_NSInternalMethods) _setOriginalSnapshot__:] + 40
4   CoreData                        0x36821030 -[NSManagedObjectContext(_NSInternalAdditions) _clearOriginalSnapshotAndInitializeRec:] + 16
5   CoreData                        0x368205f2 -[NSManagedObjectContext(_NSInternalAdditions) _didSaveChanges] + 958
6   CoreData                        0x368133bc -[NSManagedObjectContext save:] + 412
7   Decome                          0x0001fdd6 -[CreateMessageViewController actionSheet:clickedButtonAtIndex:] (CreateMessageViewController.m:163)
8   UIKit                           0x30a6cbd8 -[UIActionSheet(Private) _buttonClicked:] + 256
9   CoreFoundation                  0x30256dd4 -[NSObject performSelector:withObject:withObject:] + 20
10  UIKit                           0x3096e0d0 -[UIApplication sendAction:to:from:forEvent:] + 128
11  UIKit                           0x3096e038 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 32
12  UIKit                           0x3096e000 -[UIControl sendAction:to:forEvent:] + 44
13  UIKit                           0x3096dc58 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 528
14  UIKit                           0x309a6e9c -[UIControl touchesEnded:withEvent:] + 452
15  UIKit                           0x309a60d4 -[UIWindow _sendTouchesForEvent:] + 520
16  UIKit                           0x309a5464 -[UIWindow sendEvent:] + 108
17  UIKit                           0x30936e3c -[UIApplication sendEvent:] + 400

任何帮助,谢谢。

更新:此外,即使程序崩溃,当我打开它备份它的数据已正确保存。所以,EXC_BAD_ACCESS必须发生后,保存已经得到足够的存储在持久存储我认为。

UPDATE: Also, even though the program crashes, when I open it back up it the data has saved correctly. So the EXC_BAD_ACCESS must happen after the save has gotten at least far enough to store in the persistent store i think.

如果我注释掉保存行,代码运行良好现在。但它不保存后,我关闭和退出。如果我在我的根视图控制器willAppear函数中运行保存行,它会抛出相同的EXC_BAD_ACCESS错误。除非EXC_BAD_ACCESS
控制台没有说什么,如果我做一个回溯我得到:

If I comment out the save line, the code runs fine now. But it doesn't save after i close and exit. If I run the save line in my Root View Controllers willAppear function, it throws the same EXC_BAD_ACCESS error. The console doesn't say anything other than EXC_BAD_ACCESS if I do a backtrace I get :


#0  0x30011940 in objc_msgSend ()
#1  0x367f7d44 in -[NSKnownKeysDictionary1 dealloc] ()
#2  0x367f7ce0 in -[NSKnownKeysDictionary1 release] ()
#3  0x3687eeca in -[NSManagedObject(_NSInternalMethods) _setOriginalSnapshot__:] ()
#4  0x36821036 in -[NSManagedObjectContext(_NSInternalAdditions) _clearOriginalSnapshotAndInitializeRec:] ()
#5  0x368205f8 in -[NSManagedObjectContext(_NSInternalAdditions) _didSaveChanges] ()
#6  0x368133c2 in -[NSManagedObjectContext save:] ()
#7  0x0000314e in -[RootViewController viewWillAppear:] (self=0x11b560, _cmd=0x3014ecac, animated=1 '\001') at /Users/inckbmj/Desktop/iphone/Decome/Classes/RootViewController.m:85

抱歉,代码格式不正确。当这个视图控制器被创建,如果它不是一个新的消息,它被传递一个消息对象从一个fetchedResultsController获得像这样:

Sorry the code wasn't properly formatted before. When this view controller gets created if it is not a new "message" it is passed a message object obtained from a fetchedResultsController like so:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    MailMessage *aMessage = (MailMessage *)[fetchedResultsController objectAtIndexPath:indexPath];
    [messageView loadMessage:aMessage viewOnly:NO usingTemplate:NO];
    messageView.managedObjectContext = self.managedObjectContext;
    [self.navigationController pushViewController:messageView animated:YES]; 
}

(第一组代码来自MessageViewController.m文件类messsageView是)

(the first set of code is from the MessageViewController.m file which is the class that messsageView is)

它只会崩溃,如果我提供我的EditorViewController作为模态视图,然后返回。即使我将textArray和htmlString行(这是模态视图影响的唯一的东西)更改为:

It only crashes if I present my EditorViewController as a modal view and then return. Even if I change the textArray and htmlString lines (which are the only things the modal view affects) to:

message.textArray = @"HELLO";
message.htmlString = @"HELLO";

它仍然崩溃。如果我注释两行,但它不会崩溃。

it still crashes. If I comment both lines out however it doesn't crash.

所以它似乎崩溃,如果我提出一个模态视图,然后尝试编辑textArray或htmlString我的NSOManagedObject的字段...

So it seems like it crashes if I present a modal view and then try to edit either the textArray or htmlString fields of my NSOManagedObject...

这里是我呈现视图的地方:

Here is where i present the view:

- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event {
    if(!viewOnly) {
        UITouch *touch = [touches anyObject];
        CGPoint location = [touch locationInView: txtTo];
    location = [touch locationInView: webViewBody];
        if(CGRectContainsPoint(webViewBody.bounds, location)) {
            [editor loadTextArrayString:stringTextArray];
            [self presentModalViewController:editor animated:YES];
        }
    }
}

-(void)returnWithTextArray:(NSString *)arrayString HTML:(NSString *)html bgColor:(NSNumber *)numColor {
    [self dismissModalViewControllerAnimated:YES];
    self.stringTextArray = [NSString stringWithFormat:@"%@", arrayString];
    self.stringHTML = [NSString stringWithFormat:@"%@", html];
    self.bgColor = [NSNumber numberWithInt:[numColor intValue]];
    [webViewBody loadHTMLString:self.stringHTML baseURL:[NSURL URLWithString:@""]];
}


推荐答案

不确定我在解决实际的根本原因。当我添加此行时,错误消除:

Solved the problem though I'm not sure I'm addressing the actual root cause. The error was eliminated when I added this line:

[managedObjectContext setRetainsRegisteredObjects:YES];

到我创建managedObjectContext的位置。所以我想这与保留计数有关。我猜,或许实例变量部分或暂时释放或什么时模式视图呈现?我不知道。在任何情况下,这个错误被消除的程序工作正常现在。

To where I create the managedObjectContext. So I guess it had to do with retain counts. I'm guessing that maybe instance variables get released partially or temporarily or something when modal views are presented? I don't know. In any case, this error was eliminated the the program works fine now.

这篇关于Iphone核心数据崩溃保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆