清除缓存机制

作者: 轻斟浅醉17 | 来源:发表于2016-07-20 21:10 被阅读196次

app中基本上都要有清除缓存功能,下面是自己写的一个清除缓存的事例,有需要的可以看下。大神勿喷,谢谢!
效果图如下

屏幕快照 2016-07-20 21.08.51.png
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableCell"];
if (cell == nil)
{
    cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"TableCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.detailTextLabel.font=[UIFont systemFontOfSize:14];  
}
if (indexPath.row==0) {
    cell.textLabel.text = @"清理缓存";
    cell.detailTextLabel.text=[NSString stringWithFormat:@"%.2fM",[self filePath]];
}else{
    cell.imageView.image = [UIImage imageNamed:@"206040274942060663"];
}
return cell;

}

屏幕快照 2016-07-20 21.09.27.png
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
if (indexPath.row==0) {
    //清除缓存
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil
                                                        message:@"是否清理缓存?"
                                                       delegate:self
                                              cancelButtonTitle:@"取消"
                                              otherButtonTitles:@"确定", nil];
    [alertView show];     
   }  
}

// 显示缓存大小
-( float )filePath
{
NSString * cachPath = [ NSSearchPathForDirectoriesInDomains ( NSCachesDirectory , NSUserDomainMask , YES ) firstObject ];
return [ self folderSizeAtPath :cachPath]; 
   }
   //1:首先我们计算一下 单个文件的大小 
- ( long long ) fileSizeAtPath:( NSString *) filePath{
    NSFileManager * manager = [ NSFileManager defaultManager ]; 
    if ([manager fileExistsAtPath :filePath]){
    
      return [[manager attributesOfItemAtPath :filePath error : nil ] fileSize ];
   }
 
  return 0 ;

}
//2:遍历文件夹获得文件夹大小,返回多少 M

- ( float ) folderSizeAtPath:( NSString *) folderPath{

NSFileManager * manager = [ NSFileManager defaultManager ];

if (![manager fileExistsAtPath :folderPath]) return 0 ;

NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath :folderPath] objectEnumerator ];

NSString * fileName;

long long folderSize = 0 ;

    while ((fileName = [childFilesEnumerator nextObject ]) != nil ){
    
       NSString * fileAbsolutePath = [folderPath stringByAppendingPathComponent :fileName];
    
      folderSize += [ self fileSizeAtPath :fileAbsolutePath];
    
   }

   return folderSize/( 1024.0 * 1024.0 );

}



-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
   if (buttonIndex == 1 ) {
    [self clearFile];
    
    }
}

// 清理缓存

- (void)clearFile
{
 dispatch_async(                     dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
               , ^{//地址
                   NSString * cachPath = [   NSSearchPathForDirectoriesInDomains ( NSCachesDirectory , NSUserDomainMask , YES ) firstObject ];
                   
                   NSArray * files = [[ NSFileManager defaultManager ] subpathsAtPath :cachPath];
                   
                   NSLog ( @"cachpath = %@" , cachPath);
                   
                   for ( NSString * p in files) {
                       
                       NSError * error = nil ;
                       
                       NSString * path = [cachPath stringByAppendingPathComponent :p];
                       
                       if ([[ NSFileManager defaultManager ] fileExistsAtPath :path]) {
                           
                           [[ NSFileManager defaultManager ] removeItemAtPath :path error :&error];
                           
                       }
                       
                   }
                   
                   [ self performSelectorOnMainThread : @selector (clearCachSuccess) withObject : nil waitUntilDone : YES ];});   
}
  
  -(void)clearCachSuccess
{
    NSLog ( @" 清理成功 " );

    NSIndexPath *index=[NSIndexPath indexPathForRow:0 inSection:0];//刷新某一行数据
[_tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:index,nil] withRowAnimation:UITableViewRowAnimationNone];
}

代码通道:https://github.com/Gang679/GZBase 记得Star一下,谢谢!

相关文章

  • 清除require的缓存

    清除require的缓存机制,require之前清除缓存

  • 清除缓存机制

    app中基本上都要有清除缓存功能,下面是自己写的一个清除缓存的事例,有需要的可以看下。大神勿喷,谢谢!效果图如下 ...

  • 浏览器缓存,状态码200与304

    缓存机制 浏览器缓存控制机制有两种: 1、meta标签 清除浏览器中的缓存,必须从服务端获取最新内容,但不是所有浏...

  • 清理缓存

    //清除缓存 - (void)removeCache { //===============清除缓存=======...

  • ios SDWebImage 清除缓存 机制

    SDWebImage默认清除磁盘缓存的时长是7天。那么SDWebImage是如何进行操作的?进入SDImageCa...

  • [笔记] Swift清除缓存

    1.获取缓存大小 2.清除缓存 另附SDWebImage清除缓存

  • laravel 后台模板 View [admin.login]

    1、清除应用程序缓存 php artisan cache:clear2、清除清除路由缓存缓存 php artisa...

  • Elasticsearch的状态管理

    1、清除缓存 清除缓存接口可以清除所有缓存或者关联一个或更多索引的特定缓存。 请求: POST http://12...

  • iOS清除所有缓存

    计算缓存 清除缓存

  • 内存管理

    1. 内存池(缓存机制) 小整数对象 字符串、列表等元素的缓存 2. 垃圾收集 引用计数 标记清除 分代收集`

网友评论

    本文标题:清除缓存机制

    本文链接:https://www.haomeiwen.com/subject/qcnvjttx.html