Alan
Alan

Reputation: 4325

iOS Memory Management/Persistence Issue

I am using a helper class in my app, to access a database and return an array of 5 objects, which I then assign to a property in my view controller.

In my view controller I call it like so:

- (void)viewDidLoad
{
[super viewDidLoad];

DatabaseHelper *helper = [[DatabaseHelper alloc] init];
self.trailersArray = [helper retrieveTrailers];

// Set trailer for first round
self.trailer = [self.trailersArray objectAtIndex:0];

// Prepare audio player
[self prepareToPlay];

// Swoop film screen in
[self swoopFilmScreenInAndAddPlayButton];

// Fade title in
[self fadeInTitleScreen];

// Initialise button array and set buttons to hidden
self.buttonOutlets = [NSArray arrayWithObjects:self.button1, self.button2, self.button3, self.button4, nil];
[self hideButtons];

// Initialise rounds
self.round = [NSNumber numberWithInt:-1];

// Initialise score which will also update graphics
[self initialiseScore:self.round];
self.scoreInteger = 0;

// Start first round
self.round = [NSNumber numberWithInt:0];

NSLog([self.trailersArray description]);
// User will trigger playing with Play IBAction

// User will trigger stop playing with Stop Playing IBaction

}

My problem is that once viewDidLoad is finished, the helper object seemingly disappears, as do its objects, and my self.trailersArray ends up pointing at nothing.

How do I fix this? Have tried deep copying, and using a retain attribute on the property but not working.

I can't use a class method because it ruins my helper object database methods but I am intrigued as to how class methods get around this memory problem?

Thanks for any help.

Alan

EDIT: As requested, code for retrieveTrailers below:

-(NSArray *)retrieveTrailers
{
// Get list of mp3 files in bundle and put in array
NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *dirContents = [fm contentsOfDirectoryAtPath:bundleRoot error:nil];
NSPredicate *fltr = [NSPredicate predicateWithFormat:@"self ENDSWITH '.mp3'"];
NSArray *onlyMP3s = [dirContents filteredArrayUsingPredicate:fltr];
NSMutableArray *arrayOfTrailersWithMP3s = [[NSMutableArray alloc] init];


// Query database for objects where (unique id) = (mp3 file title)
for(NSString *string in onlyMP3s)
{
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Trailer"];
    NSString *stringWithNoFileExtension = [string stringByReplacingOccurrencesOfString:@".mp3" withString:@""];
    request.predicate = [NSPredicate predicateWithFormat:@"unique = %@", stringWithNoFileExtension];
    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES];
    request.sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
    NSArray *array = [[self managedObjectContext] executeFetchRequest:request error:nil];

    Trailer *trailer = [array lastObject];
    NSLog([NSString stringWithFormat:@"Trailer Available:%@", trailer.title]);

    if(trailer)
    {
        [arrayOfTrailersWithMP3s addObject:trailer];
    }

}

// Choose five of the trailers at random and return

NSMutableArray *fiveRandomSelectedTrailers = [[NSMutableArray alloc] init];

int numberOfAvailableTrailers = [arrayOfTrailersWithMP3s count];

for(int i=0;i<5;i++)
{
    int rand = (arc4random() % (numberOfAvailableTrailers));

    Trailer *trailer = [arrayOfTrailersWithMP3s objectAtIndex:rand];
    NSLog([NSString stringWithFormat:@"Trailer Chosen:%@", trailer.title]);

    [fiveRandomSelectedTrailers addObject:trailer];

    [arrayOfTrailersWithMP3s removeObject:trailer];

    numberOfAvailableTrailers --;
}

return fiveRandomSelectedTrailers;

}

Upvotes: 0

Views: 152

Answers (1)

Abizern
Abizern

Reputation: 150605

If that really is you viewDidLoad code what you are doing is creating a local object of your helper class which is then out of scope once the function has completed.

If you have a retained property of the helper class, you don't need the declaration: try doing it this way

In your .h file have a line like this:

@property(retain, nonatomic) DatabaseHelper *helper;

In your .m file make sure you have:

@synthesize helper;

In your viewDidLoad:

self.helper = [[DatabaseHelper alloc] init];
self.trailersArray = [self.helper retrieveTrailers];

This way you are creating an object of your helper class and assigning it to property, instead of creating a local variable. And, as you can see, you can use the property object of your helper class when you want to send it messages.

I'm assuming you are using MRC instead of ARC.

Upvotes: 1

Related Questions