IOS/swift 异步缓存文件

来源:互联网 发布:00后直播软件 编辑:程序博客网 时间:2024/06/11 15:33

#import "DownLoadMp3.h"





#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)




static DownLoadMp3 *sharedObj =nil;


  NSMutableArray *arrList;


@implementation DownLoadMp3


+ (DownLoadMp3 *) sharedInstance

{

    staticdispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        

        arrList=[[NSMutableArrayalloc] init];

        sharedObj=[[DownLoadMp3alloc] init];

        

        

    });

    

    returnsharedObj;

}



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

{

    staticdispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        

        sharedObj=[superallocWithZone:zone];

        

        

    });

    

    returnsharedObj;

}

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

{

    returnself;

}


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

    

    

    

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

        return;

    }

    

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

        return;

        

    }

    

    

    if (![arrListcontainsObject:fileUrl]) {

    

        [arrListaddObject:fileUrl];

        

    }


    

    [NSThreaddetachNewThreadSelector:@selector(downloadMp3)toTarget:selfwithObject:nil];

  

    

    

}


-(void)downloadMp3{

    

    

    

    

 

    NSString *urlString = [selfgetMp3Url];

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

        @try{

            

            

            

            

            

            

            

            dispatch_async(kBgQueue, ^{

                

                

                

                                   

                                   

                                   

                                   

                                 NSData *data = [NSDatadataWithContentsOfURL:[NSURLURLWithString:urlString]];

                                   

                

                

                

                if ([datalength]==0) {

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

                }else{

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

                }

                                   

                                   

                            


                               

                           });

            

            

           

            

           

            

            [NSThreadsleepForTimeInterval:0.1];

        }@catch (NSException * e) {

            break;

            

        }

        urlString = [selfgetMp3Url];

    }

    

 

    

}



-(NSString *)getMp3Url{

    

    

    NSMutableString  *str=[[NSMutableStringalloc] init];

    

    @synchronized(arrList){

        if([arrListcount]==0)returnnil;

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

        

        [str  setString:arrList[0]] ;

        [arrListremoveObjectAtIndex:0];

        return str;

    }

    

}


 


@end

0 0
原创粉丝点击