Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MACloop

macrumors 6502
Original poster
May 18, 2009
393
0
Germany
Hello,
I have an array created through dataaccess and parsing when my app starts to run. This array is then available in my appDelegate. Each viewcontroller who needs access to this data saved in the array are calling it using:
Code:
theList = [(AppDelegate*)[[UIApplication sharedApplication] delegate] theList];

The object theList is where my confusion starts. I have tried some different approaches here in order to both get a proper memory management and also to let the object live as long as needed in the app.

So for instance, if I have three table views and the user may navigate between them using a navigationController. Those tables are filled with data as described above. theList is defined as follows:

.h file
NSArray *theList;
Code:
@property(nonatomic, copy) NSArray *theList;

.m file
Code:
//in viewDidLoad
theList = [(AppDelegate*)[[UIApplication sharedApplication] delegate]theList];
mutableList = [[NSMutableArray alloc]init];
for(Object *o in theList){
[mutableList addObject:o];
}

Some things I do not understand here:
If i release theList after the code above, the app is crashing. As I defined it with the property copy I thought I have to release it when I do not want to use it anymore? theList is a pointer to an NSArray object - ie an object that will not change as long as the app lives. I defined it as copy, as I wrote above, because after analyzing the memory use, this seemed to be the most effective solution. I might be wrong...?

Anyway, after copying the objects from theList into an local mutable array, I thought it would be correct to kill theList...? Why is this wrong? The problem I noticed was when navigating between the different tableViews. The app crashed when I revisited a table, and that problem does not appear with the solution above. The question is only - is this the right way to go? Is it effective? Is it robust? The more I read about memory management on iphone development, the lmore confused I feel.

Thanks in advance for any ideas or comments!
MACloop
 
Nowhere in your code are you actually using your property.

Doing this:

Code:
theList = ...

just assigns the object to the 'theList' instance variable. Your property accessors are not being used. You should be doing this:

Code:
self.theList = ...

If you don't understand this, you need to go back re-read the basics of Objective-C syntax and properties.

I really don't understand what you are trying to do with the mutableList variable. Please post complete code; not snippets. It just confuses the issue.
 
... If you don't understand this, you need to go back re-read the basics of Objective-C syntax and properties.

I really don't understand what you are trying to do with the mutableList variable. Please post complete code; not snippets. It just confuses the issue.

Hello and thanks for the comment!
I have read some more about this in the documentation and this is how I have done it now:

Code:
NSArray *theList;
@property(nonatomic, retain) NSArray *theList;

@synthesize theList;
self.theList = [(AppDelegate*)[[UIApplication sharedApplication] delegate] theList];[COLOR="Green"]//assigning the local NSArray object[/COLOR]

mutableList = [[NSMutableArray alloc]init];[COLOR="Green"]//creating a mutable object to use in the current view[/COLOR]
for(Object o in theList){
	[mutableList addObject:o];[COLOR="Green"]//taking every object in theList and puts it into the mutable list[/COLOR]
}
[theList release];[COLOR="Green"]//releasing the initial NSArray object[/COLOR]

[COLOR="Green"]//Using the mutableList through the code...[/COLOR]

- (void)dealloc {
	[mutableList release];//releasing the mutableList object
	[super dealloc];
}

The app seems to run allright - is this the way to do it?
MACloop
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.