iOS 内存管理/持久性问题

标签 ios memory

我在应用程序中使用辅助类来访问数据库并返回 5 个对象的数组,然后将其分配给 View Controller 中的属性。

在我的 View Controller 中,我这样调用它:

- (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 完成,辅助对象似乎就消失了,它的对象也是如此,而我的 self.trailersArray 最终什么也没有指向。

我该如何解决这个问题?已尝试深度复制,并在属性上使用保留属性,但不起作用。

我无法使用类方法,因为它会破坏我的辅助对象数据库方法,但我很好奇类方法如何解决此内存问题?

感谢您的帮助。

艾伦

编辑:根据要求,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;

}

最佳答案

如果这确实是您的viewDidLoad代码,那么您正在做的就是创建辅助类的本地对象,一旦函数完成,该对象就会超出范围。

如果您有辅助类的保留属性,则不需要声明:尝试这样做

在你的 .h 文件中有这样一行:

@property(retain, nonatomic) DatabaseHelper *helper;

在您的 .m 文件中,确保:

@synthesize helper;

在你的viewDidLoad中:

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

通过这种方式,您可以创建辅助类的对象并将其分配给属性,而不是创建局部变量。而且,正如您所看到的,当您想要向辅助类发送消息时,您可以使用辅助类的属性对象。

我假设您使用的是 MRC 而不是 ARC。

关于iOS 内存管理/持久性问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9904212/

相关文章:

iOS 11.2.6 DateFormatter.date 对于遵守巴西利亚夏令时的城市返回 nil

ios - 在 iOS 的 UITableViewCell 中更新 UIButton 图像时发生冲突

ios - 从 Realm 中分离对象?

delphi - delphi 将zip中的图像提取到内存中

任何人都可以解释这两个分配之间的区别以及为什么好坏?

c - 为 C 中的动态二维数组赋值时出现段错误

ios - 如何在 Swift 中检查字典中的 NSObject 项?

c# - 将 Sphero 2.0 连接到 Mac OSX 10.9

c++ - 如何确定已分配缓冲区的大小

c++ - 获取错误:在抛出 'std::bad::alloc' what(): std::bad_alloc 的实例后调用终止