0

現在、さまざまなエンティティにアクセスする最良の方法を見つけようとしていますUIViewControllers。私は現在、ライブバイトが最も少ないと思われる以下を使用しています:

ビュー コントローラー 1 は、次のビューを表示するときにこれを呼び出します。

fdvc = [[FormDescriptionViewController alloc] initWithNibName:@"FormDescriptionViewController" bundle:nil andDescriptionEntity:mapEntity.desc];

View Controller 2 ( FormDescriptionViewController) は、以下を使用してセットアップされます。

.h

@property (nonatomic, retain) DescriptionEntity *descriptionEntity;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil andDescriptionEntity: (DescriptionEntity *)descE;

.m

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil andDescriptionEntity: (DescriptionEntity *)descE
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.descriptionEntity = descE;
    }
    return self;
}

このメソッドを使用する場合、viewDidLoad で次のことを実行します。

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

    [self setTitle:@"Form Description"];

    [self.textViewTitle setText:[NSString stringWithString:self.descriptionEntity.map.sName]];

    [self.textViewTitle addObserver:self forKeyPath:@"contentSize" options:(NSKeyValueObservingOptionNew) context:NULL];

    [self.textViewDescription setText:[NSString stringWithString:self.descriptionEntity.sDescription]];

    if (self.descriptionEntity.map.dImage != nil)
    {
        [self.imageView setImage:[UIImage imageWithData:[NSData dataWithData:self.descriptionEntity.map.dImage]]];
    }
    else
        [self.imageView setImage:[UIImage imageNamed:@"wildform.gif"]];

    if ([self.descriptionEntity.map.bDownloaded boolValue] == YES)
        [self.buttonUseForm setTitle:@"Use Map"];
}

これらの呼び出しは、に関連する唯一の呼び出しですがDescriptionEntity、View Controller をポップすると、メモリが解放されることはありません。ヒープ分析を実行したところ、DescriptionEntity が原因であることがわかりました。dealloc メソッド内でこれを確認するためself.descriptionEntity = nilに、[descriptionEntity dealloc]. これにより、コントローラーがポップしたときにメモリが解放されますが、もう一度コントローラーをプッシュしようとするとアクセスが悪くなるため、これが DescriptionEntity への唯一の参照であると推測されます。

私も使用してみましたNSFetchedResultsControllerが、これは現在のメソッドの 2 倍以上のライブ バイトを使用し、ビューをポップするときにメモリを再度解放することもできません。

次のビューでこのデータにアクセスする最良の方法を誰かが説明できますか?また、dealloc が呼び出されたときにメモリが再び解放されない理由を知っている人はいますか?

さらに情報が必要な場合は、お気軽にお問い合わせください。

EDIT 1(descriptionEntityが宣言されている場所):

現在、NSFetchedResultsController を使用して、MapEntities のサブセクションを取得し、UITableView 内に表示しています。行の 1 つが選択されたら、次の手順を実行して、DescriptionEntity を DescriptionViewController に渡します。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.viewHasToolbar = YES;

    MapEntity *mapEntity = [_fetchedResultsController objectAtIndexPath:indexPath];

    FormDescriptionViewController *fdvc;

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        fdvc = [[FormDescriptionViewController alloc] initWithNibName:@"FormDescriptionViewController" bundle:nil andDescriptionEntity:mapEntity.desc];
    } else {
        fdvc = [[FormDescriptionViewController alloc] initWithNibName:@"FormDescriptionViewController_iPad" bundle:nil andDescriptionEntity:mapEntity.desc];
    }

    [self.navigationController pushViewController:fdvc animated:YES];
    [fdvc release];

    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

_fetchedResultsController は次のように設定されます。

@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;

.m

- (NSFetchedResultsController *)fetchedResultsController {

    if (_fetchedResultsController != nil)
        return _fetchedResultsController;

    Singleton *singleton = [Singleton sharedSingleton];

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"MapEntity" inManagedObjectContext:[singleton managedObjectContext]];
    [fetchRequest setEntity:entity];

    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"sName" ascending:NO];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];

    [fetchRequest setFetchBatchSize:8];

    // Finally check the results
    NSError *error;
    NSArray *fetchedObjects = [[singleton managedObjectContext] executeFetchRequest:fetchRequest error:&error];
    for (MapEntity *map in fetchedObjects)
    {
        NSLog(@"Maps present in database: %@", map.sName);
    }


    NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[singleton managedObjectContext] sectionNameKeyPath:nil cacheName:@"Root11"];
    self.fetchedResultsController = theFetchedResultsController;
    self.fetchedResultsController.delegate = self;
    [self.fetchedResultsController performFetch:NULL];
    [fetchRequest release];

    return _fetchedResultsController;
}
4

0 に答える 0