请问一下,我这个demo设置了从缓存获取数据,为什么每次运行都是从网络获取数据呢?

Demo 百度云地址: http://pan.baidu.com/s/1hqghijq

Demo 微云地址:     http://url.cn/KVOwQy

请帮忙看看,谢谢!

我调用的是别人封装的NetWebServiceRequest,在NetWebServiceRequest.m里面调用ASIHTTPRequest ,缓存已经都设置好了。

代码如下:

1.NetWebServiceRequest.m:

 _runningRequest = [ASIHTTPRequest requestWithURL:url];
        NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]];
        AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
        
        [_runningRequest setCacheStoragePolicy: ASICachePermanentlyCacheStoragePolicy];
        [_runningRequest setSecondsToCache:60 * 10];
        [_runningRequest setDownloadCache: appDelegate.myCache];
        [_runningRequest setDelegate: self];
        
        //SOAP
        [_runningRequest addRequestHeader:@"Host" value:[url host]];
        [_runningRequest addRequestHeader:@"Content-Type" value:@"text/xml; charset=utf-8"];
        [_runningRequest addRequestHeader:@"Content-Length" value:msgLength];
        [_runningRequest addRequestHeader:@"SOAPAction" value:[NSString stringWithFormat:@"%@",soapActionURL]];
        [_runningRequest setRequestMethod:@"POST"];

        [_runningRequest appendPostData:[soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
        [_runningRequest setValidatesSecureCertificate:NO];
        [_runningRequest setTimeOutSeconds:60.0];
        [_runningRequest setDefaultResponseEncoding:NSUTF8StringEncoding];


2.AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
    
    //set Cache
    _myCache = [[ASIDownloadCache alloc] init];
    NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    [_myCache setStoragePath:[documentDirectory stringByAppendingPathComponent:@"myCache"]];
    [_myCache setDefaultCachePolicy: ASIAskServerIfModifiedWhenStaleCachePolicy | ASIFallbackToCacheIfLoadFailsCachePolicy];
    
    //--
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.mainViewController = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
    self.window.rootViewController = self.mainViewController;
    [self.window makeKeyAndVisible];
    return YES;
}


3.ASIDownloadCache.m

- (void)setStoragePath:(NSString *)path
{
[[self accessLock] lock];
// [self clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy];
[storagePath release];
storagePath = [path retain];

NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease];

BOOL isDirectory = NO;
NSArray *directories = [NSArray arrayWithObjects:path,[path stringByAppendingPathComponent:sessionCacheFolder],[path stringByAppendingPathComponent:permanentCacheFolder],nil];
for (NSString *directory in directories) {
BOOL exists = [fileManager fileExistsAtPath:directory isDirectory:&isDirectory];
if (exists && !isDirectory) {
[[self accessLock] unlock];
[NSException raise:@"FileExistsAtCachePath" format:@"Cannot create a directory for the cache at '%@', because a file already exists",directory];
} else if (!exists) {
[fileManager createDirectoryAtPath:directory withIntermediateDirectories:NO attributes:nil error:nil];
if (![fileManager fileExistsAtPath:directory]) {
[[self accessLock] unlock];
[NSException raise:@"FailedToCreateCacheDirectory" format:@"Failed to create a directory for the cache at '%@'",directory];
}
}
}
// [self clearCachedResponsesForStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy];
[[self accessLock] unlock];
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值