IOS/swift 异步缓存文件

本文介绍了一个使用Objective-C实现的下载MP3文件的多线程解决方案,包括单例模式、线程分离和错误处理机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#import "DownLoadMp3.h"





#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)




static DownLoadMp3 *sharedObj = nil;


  NSMutableArray *arrList;


@implementation DownLoadMp3


+ (DownLoadMp3 *) sharedInstance

{

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        

        arrList=[[NSMutableArray alloc] init];

        sharedObj=[[DownLoadMp3 alloc] init];

        

        

    });

    

    return sharedObj;

}



+ (id) allocWithZone:(NSZone *)zone //第三步:重写allocWithZone方法

{

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        

        sharedObj=[super allocWithZone:zone];

        

        

    });

    

    return sharedObj;

}

- (id) copyWithZone:(NSZone *)zone //第四步

{

    return self;

}


-(void)downLoadUrl:(NSString *)fileUrl{

    

    

    

    if (fileUrl==nil||[fileUrl length]==0) {

        return;

    }

    

    if (!([fileUrl hasPrefix:@"http://"]||[fileUrl hasPrefix:@"https://"])) {

        return;

        

    }

    

    

    if (![arrList containsObject:fileUrl]) {

    

        [arrList addObject:fileUrl];

        

    }


    

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

  

    

    

}


-(void)downloadMp3{

    

    

    

    

 

    NSString *urlString = [self getMp3Url];

    while (urlString!=nil && [urlString length]>0) {

        @try{

            

            

            

            

            

            

            

            dispatch_async(kBgQueue, ^{

                

                

                

                                   

                                   

                                   

                                   

                                 NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];

                                   

                

                

                

                if ([data length]==0) {

                       NSLog(@"%@    ++++++++++++++++++++++++++++++++++++++",urlString);

                }else{

                     NSLog(@"%@    ++++++++++++++++++++++++++++++++++++++ %lu",urlString, [data length]);

                }

                                   

                                   

                            


                               

                           });

            

            

           

            

           

            

            [NSThread sleepForTimeInterval:0.1];

        }@catch (NSException * e) {

            break;

            

        }

        urlString = [self getMp3Url];

    }

    

 

    

}



-(NSString *)getMp3Url{

    

    

    NSMutableString  *str=[[NSMutableString alloc] init];

    

    @synchronized(arrList){

        if([arrList count]==0)return nil;

        while(arrList.count>5)[arrList removeObjectAtIndex:0];

        

        [str  setString:arrList[0]] ;

        [arrList removeObjectAtIndex:0];

        return str;

    }

    

}


 


@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值