我目前正在尝试找出访问不同实体的最佳方法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
,但是当我弹出视图控制器时,内存永远不会被释放。我进行了堆分析,它告诉我 DescriptionEntity 是罪魁祸首。为了在 dealloc 方法中确认这一点,我尝试self.descriptionEntity = nil
在[descriptionEntity dealloc]
. 当控制器弹出时,这会释放内存,但是当我尝试再次推送受控时,我得到了错误的访问权限,所以我认为这现在必须是对 DescriptionEntity 的唯一引用。
我也尝试过使用,NSFetchedResultsController
但是它使用了我当前方法的两倍以上的活动字节,并且在弹出视图时它也无法再次释放内存。
谁能解释在下一个视图中访问这些数据的最佳方法,有谁知道为什么在调用 dealloc 时我的内存不会再次被释放?
如果您需要任何进一步的信息,请随时询问。
编辑 1(其中 descriptionEntity 被声明):
我目前正在使用 NSFetchedResultsController 来获取我的 MapEntities 的一个子部分并将它们显示在 UITableView 中。When one of the rows is selected I perform the following to pass the DescriptionEntity into the 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;
}