I'm reading from AppDelegate not small array. 开发者_如何学C
NSArray *mycountryspecific = [[NSArray alloc] initWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"nextTwoWeekEvents" withExtension:@"ary"]];
self.myCountrySpecificCodesList = mycountryspecific;
[mycountryspecific release],mycountryspecific = nil;
To keep it in memory, i declared property
@property (nonatomic, retain) NSArray *myCountrySpecificCodesList;
But from main view i have to go 2 steps while i will using it, under event controller i will have event detailed view controller where it will be necessary part of this array (based on predicate).
My current design is a read array not from AppDelegate, but from event detailed view controller when it necessary.
Can u share u experience? If better is load array from appdelegate and keep in memory, please advice a correct way to send access property between classes without memory leaks.
EventsTableViewController *events = [[EventsTableViewController alloc] initWithNibName:@"EventsTableViewController" bundle:nil];
self.eventListController = events;
PromoView *promo = [[PromoView alloc] initWithNibName:@"PromoView" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] init];
[navigationController pushViewController:events animated:NO];
UITabBarController *tabBar = [[UITabBarController alloc] init];
self.tabBarController = tabBar;
[tabBar setViewControllers:[NSArray arrayWithObjects:navigationController,promo,nil]];
eventListController.managedObjectContext = self.managedObjectContext;
[self.window addSubview:tabBarController.view];
SOLUTION After some playing with my controllers, i was find a solution. I still read array from file everytime when i showing a detailed page, but now this is a dictionary, where a key is a first world to find and array for this cay is a result which i need. Array was transer to 34k and a code, which make a case intensive search in keys is:
NSDictionary *mycountryspecific = [[NSDictionary alloc] initWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"countryspecific" withExtension:@"dict"]];
NSSet * mySet = [mycountryspecific keysOfEntriesPassingTest:^(id key, id obj, BOOL *stop) {
if([key compare:countryName options:NSCaseInsensitiveSearch] == NSOrderedSame) {
return YES;
}
else
return NO;
}];
NSArray *result = [mycountryspecific valueForKey:[mySet anyObject]];
This way is working well on iphone 3G :) and of course, much better on emulator ;-)
Rather than using one massive array that's held entirely in memory, I'd suggest storing your list of country-specific codes in a Core Data database.
By doing so, you could use batched fetches and an NSFetchedResultsController to only load whatever information you needed to present to the user at that given moment and nothing more. This will significantly reduce your application's overall memory usage, and will probably lead to faster startup and load times.
Each view controller could then have its own NSFetchedResultsController instance for accessing the database, providing different perspectives on the data.
I use singleton class to store shared data . It's very сonvenient. Take a look at this template http://blog.mugunthkumar.com/coding/xcode-tip-singleton-class-template/
If you retain and release your memory properly, it shouldn't matter where the object is initialized. Just ensure that all your properties are retain
, and you enforce a release
call in the dealloc
for each class that retains.
精彩评论