开发者

How NSData writetoFile in multi-thread?

开发者 https://www.devze.com 2023-03-16 07:26 出处:网络
Hi I have a problem with saving nsdata to file in multi-thread. In the main thread I used: [NSThread detachNewThreadSelector:@selector(DownloadImage) toTarget:self withObject:nil];

Hi I have a problem with saving nsdata to file in multi-thread.

In the main thread I used:

[NSThread detachNewThreadSelector:@selector(DownloadImage) toTarget:self withObject:nil];

An开发者_如何学Pythond in DownloadImage Method:

- (void)ThreadDownloadThumbImage
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    [loadThumbLock lock];
    .....//I get the NSData for my image here
    [data writeToFile:path atomically:YES];
    [loadThumbLock unlock];
    [pool release];
}

Then I always get a EXC_BAD_ACCESS exception while use data writeToFile. The data is already downloaded because I can use imageWithData and show it in a UIImageView.So I think it's the problem with writeToFile in this thread. And that path has no problem.

Can some one help me?Thanks!


OK I know it's the problem with documentDirectory.

I get the documentDirectory and save the path in an extern var. And the problem is when I get that path from NSFileManager, I didn't use retain. And the I added retain to solve this problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消