gpt4 book ai didi

Iphone 核心数据在保存时崩溃

转载 作者:行者123 更新时间:2023-12-03 18:23:06 26 4
gpt4 key购买 nike

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

我使用带有字符串的格式构造函数的字符串的原因是因为我试图复制构造...不确定这是否会自动发生?我想也许来自这些字符串的保留/释放消息(这两个来自模态视图 Controller ),在模态视图 Controller 解散或其他东西时被释放。但不要猜测,因为它仍然不起作用。

这是崩溃的代码部分:

[已编辑]

        - (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 0x00000015Crashed Thread:  0Thread 0 Crashed:0   libobjc.A.dylib                 0x30011940 objc_msgSend + 201   CoreData                        0x367f7d3e -[NSKnownKeysDictionary1 dealloc] + 822   CoreData                        0x367f7cda -[NSKnownKeysDictionary1 release] + 343   CoreData                        0x3687eec4 -[NSManagedObject(_NSInternalMethods) _setOriginalSnapshot__:] + 404   CoreData                        0x36821030 -[NSManagedObjectContext(_NSInternalAdditions) _clearOriginalSnapshotAndInitializeRec:] + 165   CoreData                        0x368205f2 -[NSManagedObjectContext(_NSInternalAdditions) _didSaveChanges] + 9586   CoreData                        0x368133bc -[NSManagedObjectContext save:] + 4127   Decome                          0x0001fdd6 -[CreateMessageViewController actionSheet:clickedButtonAtIndex:] (CreateMessageViewController.m:163)8   UIKit                           0x30a6cbd8 -[UIActionSheet(Private) _buttonClicked:] + 2569   CoreFoundation                  0x30256dd4 -[NSObject performSelector:withObject:withObject:] + 2010  UIKit                           0x3096e0d0 -[UIApplication sendAction:to:from:forEvent:] + 12811  UIKit                           0x3096e038 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 3212  UIKit                           0x3096e000 -[UIControl sendAction:to:forEvent:] + 4413  UIKit                           0x3096dc58 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 52814  UIKit                           0x309a6e9c -[UIControl touchesEnded:withEvent:] + 45215  UIKit                           0x309a60d4 -[UIWindow _sendTouchesForEvent:] + 52016  UIKit                           0x309a5464 -[UIWindow sendEvent:] + 10817  UIKit                           0x30936e3c -[UIApplication sendEvent:] + 400

Any help is appreciated, Thanks.

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.

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_ACCESSif 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

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文件,它是messageView所属的类)

只有当我将 EditorViewController 作为模态视图显示然后返回时,它才会崩溃。即使我将 textArray 和 htmlString 行(这是模态视图影响的唯一内容)更改为:

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

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

因此,如果我呈现模态视图,然后尝试编辑 NSOManagedObject 的 textArray 或 htmlString 字段,它似乎会崩溃...

这是我展示观点的地方:

- (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:@""]];
}

最佳答案

只要对该对象进行更改(插入、更新、删除),您就只能保证在上下文中保留对托管对象的访问权限。一旦调用 save:,您就会丢失对托管对象的引用。

虽然在设置 setRetainsRegisteredObjects:YES 时不再出现错误,但您可能引入了内存管理问题,因为您将托管对象的生存期设置为依赖于托管对象上下文的生存期。如果您在整个应用程序中传递上下文,并且对象层次结构很大,那么这可能会变得相当大。

您可以在 Apple 文档中阅读更多内容:https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CoreData/MO_Lifecycle.html

关于Iphone 核心数据在保存时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1230858/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com