Iphone 在NSManagedObject中释放NSData
我使用datamodel存储两个对象:视频、图像。 视频只包含字符串属性,图像有2个“二进制数据”属性 开始时,视频对象中有2个二进制数据属性。 但所有视频都是在UITableView初始化期间加载的。 对于400个视频,二进制数据代表20个月,因此想象一下4000个视频 现在,对于2个对象,UITableView加载工作正常。 我在方法tableView:cellforrowatinexpath中加载二进制数据 但现在我越是滚动到列表中,内存就越大:( 看看我的方法:Iphone 在NSManagedObject中释放NSData,iphone,database,memory-management,core-data,nsdata,Iphone,Database,Memory Management,Core Data,Nsdata,我使用datamodel存储两个对象:视频、图像。 视频只包含字符串属性,图像有2个“二进制数据”属性 开始时,视频对象中有2个二进制数据属性。 但所有视频都是在UITableView初始化期间加载的。 对于400个视频,二进制数据代表20个月,因此想象一下4000个视频 现在,对于2个对象,UITableView加载工作正常。 我在方法tableView:cellforrowatinexpath中加载二进制数据 但现在我越是滚动到列表中,内存就越大:( 看看我的方法: - (UITableVi
- (UITableViewCell *)tableView:(UITableView *)myTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"videoCell";
Video *theVideo = (Video *)[[self fetchedResultsController] objectAtIndexPath:indexPath];
VideoCellViewController *cell = (VideoCellViewController *)[myTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"VideoCellView" owner:self options:nil];
cell = editingTableViewCell;
self.editingTableViewCell = nil;
}
cell.video = theVideo;
return cell;
}
以及在VideoCellViewController中设置视频的方法
- (void)setVideo:(Video *)newVideo {
if (newVideo != video) {
[video release];
video = [newVideo retain];
}
NSData *imageData = [video.allImages valueForKey:@"thumbnailImage"];
UIImage *uiImage = [[UIImage alloc] initWithData:imageData];
smallImage.image = uiImage;
nameLabel.text = video.displayName;
[uiImage release];
}
即使没有设置smallImage,我的记忆也有问题。
如果我加载图像对象,它永远不会释放
我尝试了很多方法来释放内存,但没有成功…(didTurnIntoFault,release,CFRelease…)
在performance tool中,我可以将二进制数据视为CFData
我使用了很多iPhoneCoreDataRecipes和PhotoLocations示例
我需要帮助来清理我的记忆;)
谢谢
Samuel很明显,您的表格单元格创建逻辑有问题。让我们来看一个典型的<代码> CyfFROW 委托处理程序…./P>
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
}
// do stuff with cell
return cell;
我们看到了
- 试图得到一个可重复使用的细胞
- 如果失败(nil),则创建一个新的id并将可重用id传递给ctor
- 然后对单元格进行填充(新的或现有的),并将其返回
smallImage
?你确定你不想只在视频是新的时候做所有事情吗
- (void)setVideo:(Video *)newVideo {
if (newVideo != video) {
[video release];
video = [newVideo retain];
NSData *imageData = [video.allImages valueForKey:@"thumbnailImage"];
UIImage *uiImage = [[UIImage alloc] initWithData:imageData];
smallImage.image = uiImage;
nameLabel.text = video.displayName;
[uiImage release];
}
}
editingTableViewCell
从何而来?在我的项目中,单元格重用工作得很好(我在nib文件中定义了cellIdentifier)。使用log,我只看到了8个单元格的创建。我真的认为这是二进制数据发布的一个问题。谢谢你的帮助;)smallImage是nib中定义的UIImageView。但即使没有设置smallImage,我也有内存问题:(只是valueForKey制造了麻烦。它在…之后永远不会发布。也许我必须告诉NSManagedObjectContext暂时禁止内存中的对象。您是否在dealloc
块中发布视频?我想我必须查看更多您的代码才能在这方面帮助您也许解决方案是将图像存储到文件中,而不是数据库中。).例如: