我目前正在尝试找出访问不同UIViewControllers
中实体的最佳方式。我目前正在使用以下似乎具有最少的活字节:
View Controller 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中显示它们。当选择其中一行时,我执行以下操作将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;
}