- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章iOS系统缓存方面开发的相关基础由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
1、关于同一个url的多次请求 有时候,对同一个url请求多次,返回的数据可能都是一样的,比如服务器上的某张图片,无论下载多少次,返回的数据都是一样的。 上面的情况会造成以下问题 (1)用户流量的浪费 (2)程序响应速度不够快 解决上面的问题,一般考虑对数据进行缓存。 2、缓存 为了提高程序的响应速度,可以考虑使用缓存(内存缓存\硬盘缓存) 第一次请求数据时,内存缓存中没有数据,硬盘缓存中没有数据。 缓存数据的过程 当服务器返回数据时,需要做以下步骤 (1)使用服务器的数据(比如解析、显示) (2)将服务器的数据缓存到硬盘(沙盒) 此时缓存的情况是:内存缓存中有数据,硬盘缓存中有数据。 再次请求数据分为两种情况: (1)如果程序并没有被关闭,一直在运行 那么此时内存缓存中有数据,硬盘缓存中有数据。如果此时再次请求数据,直接使用内存缓存中的数据即可 (2)如果程序重新启动 那么此时内存缓存已经消失,没有数据,硬盘缓存依旧存在,还有数据。如果此时再次请求数据,需要读取内存中缓存的数据。 提示:从硬盘缓存中读取数据后,内存缓存中又有数据了 3、缓存的实现 1.说明: 由于get请求一般用来查询数据,post请求一般是发大量数据给服务器处理(变动性比较大) 因此一般只对get请求进行缓存,而不对post请求进行缓存 在ios中,可以使用nsurlcache类缓存数据 ios 5之前:只支持内存缓存。从ios 5开始:同时支持内存缓存和硬盘缓存 2.nsurlcache ios中得缓存技术用到了nsurlcache类。 缓存原理:一个nsurlrequest对应一个nscachedurlresponse 缓存技术:把缓存的数据都保存到数据库中。 3.nsurlcache的常见用法 (1)获得全局缓存对象(没必要手动创建)nsurlcache *cache = [nsurlcache sharedurlcache]; (2)设置内存缓存的最大容量(字节为单位,默认为512kb)- (void)setmemorycapacity:(nsuinteger)memorycapacity; (3)设置硬盘缓存的最大容量(字节为单位,默认为10m)- (void)setdiskcapacity:(nsuinteger)diskcapacity; (4)硬盘缓存的位置:沙盒/library/caches (5)取得某个请求的缓存- (nscachedurlresponse *)cachedresponseforrequest:(nsurlrequest *)request; (6)清除某个请求的缓存- (void)removecachedresponseforrequest:(nsurlrequest *)request; (7)清除所有的缓存- (void)removeallcachedresponses; 4.缓存get请求 要想对某个get请求进行数据缓存,非常简单 。
只要设置了缓存策略,系统会自动利用nsurlcache进行数据缓存 5.ios对nsurlrequest提供了7种缓存策略:(实际上能用的只有4种) 。
nsurlrequestuseprotocolcachepolicy // 默认的缓存策略(取决于协议) nsurlrequestreloadignoringlocalcachedata // 忽略缓存,重新请求 nsurlrequestreloadignoringlocalandremotecachedata // 未实现 nsurlrequestreloadignoringcachedata = nsurlrequestreloadignoringlocalcachedata // 忽略缓存,重新请求 nsurlrequestreturncachedataelseload// 有缓存就用缓存,没有缓存就重新请求 nsurlrequestreturncachedatadontload// 有缓存就用缓存,没有缓存就不发请求,当做请求出错处理(用于离线模式) nsurlrequestreloadrevalidatingcachedata // 未实现 。
。
。
6.缓存的注意事项 缓存的设置需要根据具体的情况考虑,如果请求某个url的返回数据: (1)经常更新:不能用缓存!比如股票、彩票数据 (2)一成不变:果断用缓存 (3)偶尔更新:可以定期更改缓存策略 或者 清除缓存 提示:如果大量使用缓存,会越积越大,建议定期清除缓存 。
。
4、本地缓存开发相关 为了节约流量,同时也是为了更好的用户体验,目前很多应用都使用本地缓存机制,其中以网易新闻的缓存功能最为出色。我自己的应用也想加入本地缓存的功能,于是我从网上查阅了相关的资料,发现总体上说有两种方法。一种是自己写缓存的处理,一种是采用asihttprequest中的asidownloadcache.
方法一:一般将服务器第一次返回的数据保存在沙盒里面。这样在手机断网的情况下可以从本地读取数据了。 1.保存到沙盒的代码: 。
。
+ (void)savecache:(int)type andid:(int)_id andstring:(nsstring *)str; { nsuserdefaults * setting = [nsuserdefaults standarduserdefaults]; nsstring * key = [nsstring stringwithformat:@"detail-%d-%d",type, _id]; [setting setobject:str forkey:key]; [setting synchronize]; } 。
2.读取本地沙盒的代码 读取之前首先根据type和id判断本地是否有 。
。
+ (nsstring *)getcache:(int)type andid:(int)_id { nsuserdefaults * settings = [nsuserdefaults standarduserdefaults]; nsstring *key = [nsstring stringwithformat:@"detail-%d-%d",type, _id]; nsstring *value = [settings objectforkey:key]; return value; } 。
如果沙盒里面有数据 。
。
nsstring *value = [tool getcache:5 andid:self.qiutime]; if (value) { nsdictionary *backdict = [value jsonvalue]; if ([backdict objectforkey:@"items"]) { nsarray *array=[nsarray arraywitharray:[backdict objectforkey:@"items"]]; for (nsdictionary *qiushi in array) { qiushi *qs=[[[qiushi alloc]initwithdictionary:qiushi] autorelease]; [self.list addobject:qs]; } } [self.tableview reloaddata]; } [self.tableview tableviewdidfinishedloadingwithmessage:@"数据全部加载完了.."]; self.tableview.reachedtheend = yes; 。
方法二:使用asihttprequest和asidownloadcache实现本地缓存 1、设置全局的cache 在appdelegate.h中添加一个全局变量 。
。
@interface appdelegate : uiresponder { asidownloadcache *mycache; } @property (strong, nonatomic) uiwindow *window; @property (nonatomic,retain) asidownloadcache *mycache; 。
在appdelegate.m中的- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions方法中添加如下代码 。
。
。
在appdelegate.m中的dealloc方法中添加如下语句 。
。
[mycache release]; 。
。
。
到这里为止,就完成了全局变量的声明。 2、设置缓存策略 在实现asihttprequest请求的地方设置request的存储方式,代码如下 。
。
。
nsstring *str = @"http://....../getpicturenews.aspx"; nsurl *url = [nsurl urlwithstring:str]; asihttprequest *request = [asihttprequest requestwithurl:url]; //获取全局变量 appdelegate *appdelegate = [[uiapplication sharedapplication] delegate]; //设置缓存方式 [request setdownloadcache:appdelegate.mycache]; //设置缓存数据存储策略,这里采取的是如果无更新或无法联网就读取缓存数据 [request setcachestoragepolicy:asicachepermanentlycachestoragepolicy]; request.delegate = self; [request startasynchronous]; 。
。
。
3、清理缓存数据 我在这里采用的是手动清理数据的方式,在适当的地方添加如下代码,我将清理缓存放在了应用的设置模块:
。
。
appdelegate *appdelegate = [[uiapplication sharedapplication] delegate]; [appdelegate.mycache clearcachedresponsesforstoragepolicy:asicachepermanentlycachestoragepolicy]; 。
。
这里清理的是asicachepermanentlycachestoragepolicy这种存储策略的缓存数据,如果更换其他的参数的话,即可清理对应存储策略的缓存数据.
最后此篇关于iOS系统缓存方面开发的相关基础的文章就讲到这里了,如果你想了解更多关于iOS系统缓存方面开发的相关基础的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
IO 设备如何知道属于它的内存中的值在memory mapped IO 中发生了变化? ? 例如,假设内存地址 0 专用于保存 VGA 设备的背景颜色。当我们更改 memory[0] 中的值时,VGA
我目前正在开发一个使用Facebook sdk登录(通过FBLoginView)的iOS应用。 一切正常,除了那些拥有较旧版本的facebook的人。 当他们按下“使用Facebook登录”按钮时,他
假设我有: this - is an - example - with some - dashesNSRange将使用`rangeOfString:@“-”拾取“-”的第一个实例,但是如果我只想要最后
Card.io SDK提供以下详细信息: 卡号,有效期,月份,年份,CVV和邮政编码。 如何从此SDK获取国家名称。 - (void)userDidProvideCreditCardInfo:(Car
iOS 应用程序如何从网络服务下载图片并在安装过程中将它们安装到用户的 iOS 设备上?可能吗? 最佳答案 您无法控制应用在用户设备上的安装,因此无法在安装过程中下载其他数据。 只需在安装后首次启动应
我曾经开发过一款企业版 iOS 产品,我们公司曾将其出售给大型企业,供他们的员工使用。 该应用程序通过 AppStore 提供,企业用户获得了公司特定的配置文件(包含应用程序配置文件)以启用他们有权使
我正在尝试将 Card.io SDK 集成到我的 iOS 应用程序中。我想为 CardIO ui 做一个简单的本地化,如更改取消按钮标题或“在此保留信用卡”提示文本。 我在 github 上找到了这个
我正在使用 CardIOView 和 CardIOViewDelegate 类,没有可以设置为 YES 的 BOOL 来扫描 collectCardholderName。我可以看到它在 CardIOP
我有一个集成了通话工具包的 voip 应用程序。每次我从我的 voip 应用程序调用时,都会在 native 电话应用程序中创建一个新的最近通话记录。我在 voip 应用程序中也有自定义联系人(电话应
iOS 应用程序如何知道应用程序打开时屏幕上是否已经有键盘?应用程序运行后,它可以接收键盘显示/隐藏通知。但是,如果应用程序在分屏模式下作为辅助应用程序打开,而主应用程序已经显示键盘,则辅助应用程序不
我在模拟器中收到以下错误: ImageIO: CGImageReadSessionGetCachedImageBlockData *** CGImageReadSessionGetCachedIm
如 Apple 文档所示,可以通过 EAAccessory Framework 与经过认证的配件(由 Apple 认证)进行通信。但是我有点困惑,因为一些帖子告诉我它也可以通过 CoreBluetoo
尽管现在的调试器已经很不错了,但有时找出应用程序中正在发生的事情的最好方法仍然是古老的 NSLog。当您连接到计算机时,这样做很容易; Xcode 会帮助弹出日志查看器面板,然后就可以了。当您不在办公
在我的 iOS 应用程序中,我定义了一些兴趣点。其中一些有一个 Kontakt.io 信标的名称,它绑定(bind)到一个特定的 PoI(我的意思是通常贴在信标标签上的名称)。现在我想在附近发现信标,
我正在为警报提示创建一个 trigger.io 插件。尝试从警报提示返回数据。这是我的代码: // Prompt + (void)show_prompt:(ForgeTask*)task{
您好,我是 Apple iOS 的新手。我阅读并搜索了很多关于推送通知的文章,但我没有发现任何关于 APNS 从 io4 到 ios 6 的新更新的信息。任何人都可以向我提供 APNS 如何在 ios
UITabBar 的高度似乎在 iOS 7 和 8/9/10/11 之间发生了变化。我发布这个问题是为了让其他人轻松找到答案。 那么:在 iPhone 和 iPad 上的 iOS 8/9/10/11
我想我可以针对不同的 iOS 版本使用不同的 Storyboard。 由于 UI 的差异,我将创建下一个 Storyboard: Main_iPhone.storyboard Main_iPad.st
我正在写一些东西,我将使用设备的 iTunes 库中的一部分音轨来覆盖 2 个视频的组合,例如: AVMutableComposition* mixComposition = [[AVMutableC
我创建了一个简单的 iOS 程序,可以顺利编译并在 iPad 模拟器上运行良好。当我告诉 XCode 4 使用我连接的 iPad 设备时,无法编译相同的程序。问题似乎是当我尝试使用附加的 iPad 时
我是一名优秀的程序员,十分优秀!