iOS检查MD5导致内存警告

时间:2013-10-28 04:52:51

标签: ios hash md5 didreceivememorywarning

我在下载文件后使用了一些方法来检查MD5。但是,似乎检查过多的文件md5总是导致内存警告或低内存崩溃。有人有一个很好的解决方案吗?

我有使用方法从Joel's Writings检查MD5,以及其他帖子中的其他方法,但都无法解决我的问题。

NSInteger index = 0;

    while (downloadQueue.count > 0) {

        NSLog(@"%i files need to download.", downloadQueue.count);
        NSLog(@"00: %u", [MenChecker get_free_memory]);

        @autoreleasepool {

            NSString *src = [downloadQueue objectAtIndex:index];
            NSString *path = [self.storageRoot stringByAppendingPathComponent:[storageList objectAtIndex:index]];
            NSString *md5 = [md5CheckList objectAtIndex:index];

            BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:path];
            if (isExist) {

                NSString *md5OfData = nil;

                CFStringRef fileMD5Hash = FileMD5HashCreateWithPath((__bridge CFStringRef)path, FileHashDefaultChunkSizeForReadingData);
                if (fileMD5Hash) {

                    md5OfData = (__bridge NSString *)fileMD5Hash;
                    CFRelease(fileMD5Hash);
                }

                if ([md5OfData isEqualToString:md5]) {

                    [downloadQueue removeObjectAtIndex:index];
                    [storageList removeObjectAtIndex:index];
                    [md5CheckList removeObjectAtIndex:index];
                }
                else {

                    [[NSFileManager defaultManager] removeItemAtPath:path error:nil];

                    ASIHTTPRequest *request_cover = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:src]];
                    [request_cover setDownloadDestinationPath:path];
                    [request_cover startSynchronous];

                    NSString *md5OfDataFile = nil;
                    CFStringRef dataFileMD5Hash = FileMD5HashCreateWithPath((__bridge CFStringRef)path, FileHashDefaultChunkSizeForReadingData);
                    if (dataFileMD5Hash) {

                        md5OfDataFile = (__bridge NSString *)dataFileMD5Hash;
                        CFRelease(dataFileMD5Hash);
                    }

                    if ([[NSFileManager defaultManager] fileExistsAtPath:path] && [md5OfDataFile isEqualToString:md5]) {

                        [downloadQueue removeObjectAtIndex:index];
                        [storageList removeObjectAtIndex:index];
                        [md5CheckList removeObjectAtIndex:index];
                    }
                    else {

                        index ++;
                    }
                    md5OfDataFile = nil;
                }
                md5OfData = nil;
            }
            else {

                NSString *directory = [path stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"/%@", [path lastPathComponent]] withString:@""];
                [[NSFileManager defaultManager] createDirectoryAtPath:directory withIntermediateDirectories:YES attributes:nil error:nil];

                ASIHTTPRequest *request_cover = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:src]];
                [request_cover setDownloadDestinationPath:path];
                [request_cover startSynchronous];
                request_cover = nil;

                NSString *md5OfData = nil;
                CFStringRef fileMD5Hash = FileMD5HashCreateWithPath((__bridge CFStringRef)path, FileHashDefaultChunkSizeForReadingData);
                if (fileMD5Hash) {

                    md5OfData = (__bridge NSString *)fileMD5Hash;
                    CFRelease(fileMD5Hash);
                }

                if ([[NSFileManager defaultManager] fileExistsAtPath:path] && [md5OfData isEqualToString:md5]) {

                    [downloadQueue removeObjectAtIndex:index];
                    [storageList removeObjectAtIndex:index];
                    [md5CheckList removeObjectAtIndex:index];
                }
                else {

                    index ++;
                }
                md5OfData = nil;
            }

            if (index >= downloadQueue.count) {

                index = 0;
            }
        }

0 个答案:

没有答案