iPhone故障安全多个文件下载 [英] iPhone fail-safe multiple files download

查看:138
本文介绍了iPhone故障安全多个文件下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程式正在从伺服器下载图片包。它是一个来自XML的直接链接数组(20-50个文件)。




  • 如何确保整套图像


  • 如果使用iPhone按钮关闭应用程序,如何添加条件以取消整个下载(并删除所有已下载的文件)? (这种方法在AppDelegate,而我的整个下载代码在一些downloadviewcontroller.m中)


  • 还有什么我不得不担心, (共有5-10 MB)




我目前使用的代码在下载中断时并不十分安全或应用关闭。在后台线程中,我为每个文件调用此方法:

 (BOOL)loadImageFromURL:(NSString *)url withName: NSString *)filename toFolder:(NSString *)文件夹{

NSURL * link = [NSURL URLWithString:url];
NSFileManager * manager = [NSFileManager defaultManager];
NSString * filepath = [folder stringByAppendingPathComponent:filename];

if([manager fileExistsAtPath:filepath]){
return YES;
}
else
{
UIImage * image = [[UIImage imageWithData:[NSData dataWithContentsOfURL:link]] retain];
NSData * data = [NSData dataWithData:UIImageJPEGRepresentation(image,1.0f)];
if([data length]< = 0)
[image release];
return NO; // no data
else
{
[data writeToFile:filepath atomically:YES];
[image release];
return YES;
}
}
}


解决方案>

使用nsoperation这样做



检查blow link ..



http://www.cimgf.com/2008/02/16/cocoa-tutorial-nsoperation-and-nsoperationqueue /



现在2012年8月试图搜索wwdc 2012 video no 211来学习操作。
u可以使用块来这样做。

  [aNsque addExecutionBlock:^ {... code ...}]; 

其中aNsque是nsblockoperation。


My app is downloading packages of images from a server. It's an array of direct links (20-50 files) from XML.

  • How do I make sure the whole set of images is fully downloaded?

  • How do I add a condition to cancel whole download (and remove all already downloaded files) if app was closed with iPhone button? (such methods are in AppDelegate while my whole download code is in some downloadviewcontroller.m)

  • Anything else I have to worry about while downloading several files? (5-10 MB in total)

The code I currently use isn't very safe in case of download interrupting or app closing. In background thread I'm calling this method for each file:

    (BOOL) loadImageFromURL:(NSString *)url withName:(NSString *)filename toFolder:(NSString *)folder {

        NSURL *link = [NSURL URLWithString:url];
        NSFileManager *manager = [NSFileManager defaultManager];    
        NSString *filepath = [folder stringByAppendingPathComponent:filename];

        if ([manager fileExistsAtPath:filepath]) {
            return YES;
        }
        else
        { 
            UIImage *image = [[UIImage imageWithData:[NSData dataWithContentsOfURL:link]] retain];
            NSData *data = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0f)];
            if ([data length] <= 0) 
                [image release];
                return NO; // no data
            else 
            {
                [data writeToFile:filepath atomically:YES];
                [image release];            
                return YES;
            }
        }
    }

解决方案

Use nsoperation to do so

check out blow link..

http://www.cimgf.com/2008/02/16/cocoa-tutorial-nsoperation-and-nsoperationqueue/

now on august 2012 try to search wwdc 2012 video no 211 to learn nsoperation. u can use block to do so.

[aNsque addExecutionBlock:^{ ...code... }];

where aNsque is nsblockoperation.

这篇关于iPhone故障安全多个文件下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆