为什么我不必在第二个TableViewController中发布managedObjectContext [英] Why don't I have to release managedObjectContext in the 2nd TableViewController

查看:93
本文介绍了为什么我不必在第二个TableViewController中发布managedObjectContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个tableview控制器显示CoreData对象。一个是细节视图(带有句子)一个是概述(带有故事)。
选择一个故事 - >查看句子。

I have two tableview controllers showing CoreData objects. One is a detail view (with sentences) one is an overview (with stories). Pick a Story -> See the Sentences.

看起来我超载我的managedObjectContext;我最初在dealloc的TableViewControllers中发布了它,并且每次我在两个控制器之间(Story - > Sentence - > Story - > Sentence - > Story - > Crash)发生崩溃。
一些调试显示我在TableViewControllers的ViewDidLoad中的代码后崩溃了我的App Delegate:

It looks like I was over-releasing my managedObjectContext; I originally released it in both TableViewControllers in dealloc and got a crash every third time I went between the two controllers (Story -> Sentence -> Story -> Sentence -> Story -> Crash). Some debugging showed I was crashing in my App Delegate after this code in ViewDidLoad of both TableViewControllers:

 if (managedObjectContext == nil) 
{ 
    managedObjectContext = [(StoryBotAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
    NSLog(@"After managedObjectContext: %@",  managedObjectContext);
}

更多研究发现这个讨论,这让我相信这是一个过度发布的ManagedObjectContext的情况:

Some more research found this discussion which led me to believe it was a case of an over released ManagedObjectContext:


第二个更平凡的问题是过度发布的
NSManagedObject。 Instruments ObjectAlloc工具应该能够帮助

The second more prosaic issue is simply an overreleased NSManagedObject. Instruments ObjectAlloc tool should be able to help you.

所以我删除了[managedObjectContext release]。从我的dealloc在TableViewController和现在我没有泄漏(根据仪器),没有崩溃。

So I removed [managedObjectContext release]; from my dealloc in TableViewController and now I have no leaks (according to Instruments) and no crash.

看来问题已修复,但这里有个问题:

It looks like the problem is fixed, but here's the question:


  • 我可能完全错过了这一点,只是隐藏了另一个问题。如何找到过度发布或真正的问题?

  • I might be missing the point altogether, and just hiding another issue. How can I find the over-release, or the real problem?

如果我已解决问题,我想知道为什么它是固定的,为什么我不需要在第二个TableViewController中释放MOC

If I have fixed the problem, I'd like to know why it's fixed and why I don't need to release the MOC in the second TableViewController

MakeSentenceTableViewController.m

MakeSentenceTableViewController.m

@implementation MakeSentenceTableViewController
@synthesize story, managedObjectContext;
- (void)viewDidLoad {
[super viewDidLoad];

self.title = @"My Story";
NSLog(@"Passed Story Object: %@", story);
if (managedObjectContext == nil) 
{ 
    NSLog(@"managedObjectContext == nil");
    managedObjectContext = [(StoryBotAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
    NSLog(@"After managedObjectContext: %@",  managedObjectContext);
}else{
    NSLog(@"managedObjectContext != nil");
}
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Sentence" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];

//sorting stuff:
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"order" ascending: YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects: sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
//[request setFetchBatchSize:FETCH_BATCH_SIZE];
[sortDescriptors release];
[sortDescriptor release];

 fetchedResultsController = [[NSFetchedResultsController alloc] 
                            initWithFetchRequest:request managedObjectContext:managedObjectContext 
                            sectionNameKeyPath:nil cacheName:nil];
[request release];

NSError *error;
[fetchedResultsController performFetch:&error];

NSLog(@"FetchedResultsController: %@", fetchedResultsController);
NSLog(@"fetchedResultsController RetainCount at viewDidLoad: %d",     [fetchedResultsController retainCount]);
 }
//snip...table view bits
- (void)dealloc {
[fetchedResultsController release];
//Why don't I have to release this?
//[managedObjectContext release];
    [super dealloc];
}


推荐答案

。即使你的视图控制器中的MOC属性是(retain),你不是调用setter,而是直接设置引用。如果你想保留和释放它,你必须调用 self.managedObjectContext = ... (注意点),这相当于 [self setManagedObjectContext:...] / em>,然后你可以安全地释放它在dealloc。实际上,由于MOC由应用程序委托拥有和管理,我甚至不会保留它。

Because you are not retaining it. Even if the "MOC" property in you view controller is (retain), you are not calling the setter but simply setting the reference directly. If you want to retain and release it, you have to call self.managedObjectContext = ... instead (notice the dot), that is equivalent to [self setManagedObjectContext:...] and only then you can safely release it in the dealloc. Actually, since the "MOC" is owned by and managed in the app delegate, I wouldn't even bother to retain it.

这篇关于为什么我不必在第二个TableViewController中发布managedObjectContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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