确定代码中的泄漏 - iPhone

发布于 2024-11-07 19:31:09 字数 2501 浏览 2 评论 0原文

我在我的应用程序中使用核心数据,并复制了 xcode 为获取的结果控制器生成的相同代码(除了我将缓存名称更改为 nil)。尽管如此,仪器工具显示我的代码中存在 fetchedResultsController 的泄漏,特别是下面代码中的变量 aFetchedResultsController。知道为什么会发生这种情况吗?感谢您的投入。谢谢。

这是 myModel 类

- (NSFetchedResultsController *)fetchedResultsController {

    if (fetchedResultsController_ != nil) {
        return fetchedResultsController_;
    }

    /*
     Set up the fetched results controller.
     */
    // Create the fetch request for the entity.
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:self.entityName inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    // Set the batch size to a suitable number.
    [fetchRequest setFetchBatchSize:20];

    // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:self.sortFieldName ascending:YES];
    NSMutableArray *sortDescriptors = [[NSMutableArray alloc] initWithObjects:sortDescriptor, nil];

    if(self.additionalSortField !=nil){
        NSSortDescriptor *additionalSortDescr = [[NSSortDescriptor alloc] initWithKey:self.additionalSortField ascending:YES];
        [sortDescriptors addObject:additionalSortDescr];
        [additionalSortDescr release];
    }




    [fetchRequest setSortDescriptors:sortDescriptors];

    if(predicate != nil)
        [fetchRequest setPredicate:predicate];

    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:sectionKeyName cacheName:nil];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    [aFetchedResultsController release];
    aFetchedResultsController = nil;
    [fetchRequest release];
    fetchRequest = nil;
    [sortDescriptor release];
    [sortDescriptors release];

    NSError *error = nil;
    if (![fetchedResultsController_ performFetch:&error]) {

        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);

    }

    return fetchedResultsController_;
} 

这是我调用它的地方

NSFetchedResultsController* fetchResults = myModel.fetchedResultsController;

I am using core data in my app, and have copied the same code generated by xcode for the fetched results controller(except that I am changing the cache name to nil). Nevertheless, the instruments tool shows a leak in my code for the fetchedResultsController, specifically for the variable aFetchedResultsController in the code below. Any idea why this is happening? Appreciate your inputs.thanks.

This is myModel class

- (NSFetchedResultsController *)fetchedResultsController {

    if (fetchedResultsController_ != nil) {
        return fetchedResultsController_;
    }

    /*
     Set up the fetched results controller.
     */
    // Create the fetch request for the entity.
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:self.entityName inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    // Set the batch size to a suitable number.
    [fetchRequest setFetchBatchSize:20];

    // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:self.sortFieldName ascending:YES];
    NSMutableArray *sortDescriptors = [[NSMutableArray alloc] initWithObjects:sortDescriptor, nil];

    if(self.additionalSortField !=nil){
        NSSortDescriptor *additionalSortDescr = [[NSSortDescriptor alloc] initWithKey:self.additionalSortField ascending:YES];
        [sortDescriptors addObject:additionalSortDescr];
        [additionalSortDescr release];
    }




    [fetchRequest setSortDescriptors:sortDescriptors];

    if(predicate != nil)
        [fetchRequest setPredicate:predicate];

    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:sectionKeyName cacheName:nil];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    [aFetchedResultsController release];
    aFetchedResultsController = nil;
    [fetchRequest release];
    fetchRequest = nil;
    [sortDescriptor release];
    [sortDescriptors release];

    NSError *error = nil;
    if (![fetchedResultsController_ performFetch:&error]) {

        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);

    }

    return fetchedResultsController_;
} 

This is where I am calling it

NSFetchedResultsController* fetchResults = myModel.fetchedResultsController;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无需解释 2024-11-14 19:31:09

NSFetchedResultsController* fetchResults 在 .h 文件中声明

NSFetchedResultsController* fetchResults declare in .h file

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文