2012-03-28 151 views
0

我在我的應用程序中使用助手類來訪問數據庫並返回5個對象的數組,然後將其分配給我的視圖控制器中的屬性。iOS內存管理/持久性問題

在我的視圖控制器我叫它像這樣:

- (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 

}

我的問題是,一旦viewDidLoad中結束時,輔助對象似乎消失了,因爲這樣做的目的,和我自己。拖車陣營最終指向什麼都沒有。

我該如何解決這個問題?已嘗試深層複製,並在屬性上使用retain屬性但不工作。

我不能使用類方法,因爲它毀掉了我的幫助器對象數據庫方法,但我對如何使用類方法解決這個內存問題很感興趣?

感謝您的任何幫助。

艾倫

編輯:按照要求,下面retrieveTrailers代碼:

-(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; 

}

+0

提供驗證碼的代碼 – jacekmigacz 2012-03-28 09:07:15

+1

提供**完整的**代碼viewDidLoad – jacekmigacz 2012-03-28 09:07:45

+0

補充說,希望它不會讓事物過分混淆...... – Alan 2012-03-28 09:23:06

回答

1

如果這真的是你viewDidLoad代碼,你在做什麼是創造助手的本地對象一旦函數完成,它就會超出範圍。

如果你有輔助類的保留的財產,你不需要聲明:嘗試做這種方式

在您的.h文件中有這樣一行:

@property(retain, nonatomic) DatabaseHelper *helper; 

在您的.m文件請確保您有:

@synthesize helper; 

在你viewDidLoad

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

這樣您就可以創建助手類的對象並將其分配給屬性,而不是創建局部變量。正如你所看到的,當你想發送消息時,你可以使用你的助手類的屬性對象。

我假設你正在使用MRC而不是ARC。

+0

同意Abizem,這個工程,我實際上已經嘗試過類似的東西,但搞砸了,認爲我忘了alloc-init,謝謝! – Alan 2012-03-28 09:29:59

+0

儘管我不得不說,現在我試圖將數組傳遞給下一個視圖控制器,並且還必須將helper對象傳遞給新VC中的屬性,但似乎很奇怪,製作數組的副本並保留其對象。 – Alan 2012-03-28 09:40:38

+0

是的,你可以。數組已經保留了它的內容。你可以發送一個'copy'或'mutableCopy'消息來得到一個副本。 – Abizern 2012-03-28 09:54:02