简体   繁体   English

CoreData DetailTableView BAD_ACCESS 错误

[英]CoreData DetailTableView BAD_ACCESS Error

Maybe I'm not going about showing a detail for a selected row using CoreData, but I can't figure out why I'm getting a "BAD_ACCESS" error.也许我不打算使用 CoreData 显示所选行的详细信息,但我无法弄清楚为什么会出现“BAD_ACCESS”错误。 I've googled around and can't find what I'm looking for.我用谷歌搜索,找不到我要找的东西。

Basically I use CoreData to populate the data for a Table View.基本上我使用 CoreData 来填充表格视图的数据。 It retrieves all of the title attributes for all of the entities.它检索所有实体的所有标题属性。 When the user clicks on a row, I have a Detail View that needs to show the description for that entity.当用户单击一行时,我有一个详细视图,需要显示该实体的描述。 I think I need to make a new NSManagedObjectContext and a new NSEntityDescription for a new NSFetchRequest in my DetailViewController and then use a NSPredicate to say "where title = [user selected title]".我想我需要在我的 DetailViewController 中为新的 NSFetchRequest 创建一个新的 NSManagedObjectContext 和一个新的 NSEntityDescription,然后使用 NSPredicate 说“其中 title = [user selected title]”。 I get an error when I select a row.当我连续 select 时出现错误。 See code:见代码:

- (void)viewDidLoad
{
    // Do any additional setup after loading the view from its nib.

    // Get the objects from Core Data database
    Caregiver_Activity_GuideAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    NSEntityDescription *entityDescription = [NSEntityDescription
                                              entityForName:@"Definition"
                                              inManagedObjectContext:context];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entityDescription];

    NSPredicate *pred = [NSPredicate predicateWithFormat:@"(title = %@)", self.title];
    [request setPredicate:pred];

    NSError *error;
    NSArray *objects = [context executeFetchRequest:request error:&error];
    if (objects == nil) {
        NSLog(@"There was an error!");
        // Do whatever error handling is appropriate
    }

    for (NSManagedObject *oneObject in objects) {
        [definitionDescriptionTextView setText:[oneObject valueForKey:@"desc"]];
    }

    [objects release];
    [request release];

    [super viewDidLoad];
}

I comment out that code and everything works.我注释掉该代码,一切正常。 But when I try to debug with breakpoints, nothing catches.但是当我尝试使用断点进行调试时,什么都没有。 So I'm more confused.所以我更加困惑。

I know CoreData is probably overkill for what I'm doing but this is a learning app for me.我知道 CoreData 对于我正在做的事情可能有点矫枉过正,但这对我来说是一个学习应用程序。

EDIT: I didn't include that I'm using a sqlite database that is pre-populated with the entities.编辑:我没有包括我使用的是预先填充了实体的 sqlite 数据库。

You can also download my project on my github page .您也可以在我的 github 页面上下载我的项目。

Normally, with a Core Data backed Master-Detail interface, you don't fetch for the Detail view.通常,使用 Core Data 支持的 Master-Detail 接口,您不会获取 Detail 视图。

When you select a row in the Master tableview, you are selecting a particular managed object instance.当您在主表视图中 select 一行时,您正在选择一个特定的托管 object 实例。 You then pass that managed object instance to the detail view.然后将该托管 object 实例传递到详细信息视图。 There is no need to refetch the object that you selected in the tableview.无需重新获取您在表格视图中选择的 object。

A good example of this would be the Contacts app.一个很好的例子是联系人应用程序。 The Master tableview would be a list of Contact objects (displaying the name.) When you select a row, the Master tableview controller takes the specific Contact object associated with the selected row and then passes it to the Detail view controller which then populates the Detail view using data taking from the properties of the passed Contact object. The Master tableview would be a list of Contact objects (displaying the name.) When you select a row, the Master tableview controller takes the specific Contact object associated with the selected row and then passes it to the Detail view controller which then populates the Detail使用从传递的联系人 object 的属性中获取的数据进行查看。

So, that entire code block where the error occurs is unnecessary.因此,发生错误的整个代码块是不必要的。

However, the immediate error in this code is that you are releasing an object you didn't create.但是,此代码中的直接错误是您正在发布一个不是您创建的 object。 In this line:在这一行:

 NSArray *objects = [context executeFetchRequest:request error:&error];

... you are not creating a NSArray instance with a init , new or create method. ...您没有使用initnewcreate方法创建 NSArray 实例。 Instead, you are merely receiving an autoreleased NSArray instance created and returned by the context NSManagedObjectContext instance.相反,您只是接收由context NSManagedObjectContext 实例创建和返回的自动释放的 NSArray 实例。 When you release an object you did not create here:当您发布 object 时,您没有在此处创建:

 [objects release];

... you cause the crash. ...你导致崩溃。

Conversely, you do create a NSFetchRequest here:相反,您确实在这里创建了一个 NSFetchRequest:

 NSFetchRequest *request = [[NSFetchRequest alloc] init];

... because you used init so you do have to balance that with: ...因为你使用了init所以你必须平衡它:

[request relwase];

BTW, this type of code should not be put in viewDidLoad as the method is only called when the view is read in the first time from the nib file on disk.顺便说一句,这种类型的代码不应该放在viewDidLoad中,因为该方法仅在第一次从磁盘上的 nib 文件中读取视图时调用。 That is only guaranteed to happen once as the view may remain in memory when the user switches to another view.这只能保证发生一次,因为当用户切换到另一个视图时,视图可能会保留在 memory 中。 Instead, put code that needs to run each time the view appears in viewWillAppear .相反,将每次视图出现时需要运行的代码放在viewWillAppear中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM