Mknetworkkit Custom Cache validity time

Source: Internet
Author: User

Mknetworkkit is a very useful, lightweight network request framework. can support automatic caching.
However, in some cases, we require data to be updated in real time, such as after placing orders, order status Refresh, product status refresh, and so on.
The Mknetworkkit has a variable that controls the cache's effective time, called mknetworkkitdefaultcacheduration. This variable is pre-defined in the MKNetworkKit.h

//  MKNetworkKit.h#define kMKNetworkKitDefaultCacheDuration 60 // 1 minute

We can find this variable to be used in mknetworkoperation.m after the global search.

expiresOnDate = [[NSDate date] dateByAddingTimeInterval:kMKNetworkKitDefaultCacheDuration];

This expiresondate is the variable used to control the cache lifecycle.
If you need to customize the cache time, you can customize a mknetworkoperation subclass. Here for a simple demonstration, it is directly in the Mknetworkoperation class to modify.

First, write in the location where the MKNetworkOperation.h file defines the property.

@propertystrong) NSString *customExpireTime;//ifisnotdefaulttimewith this

and find out where this line of code is.

expiresOnDate = [[NSDate date] dateByAddingTimeInterval:kMKNetworkKitDefaultCacheDuration];

and change it to

if (self.customExpireTime) {    int expireSeconds = [self.customExpireTime intValue];    expiresOnDate = [[NSDate date] dateByAddingTimeInterval:expireSeconds];}else{    expiresOnDate = [[NSDate date] dateByAddingTimeInterval:kMKNetworkKitDefaultCacheDuration];}

When using, if you need to customize the cache validity time, you just need to add such a line of code

netOp = [appDelegate.netEngine operationWithPath:path];//加上下面这行代码,自定义缓存有效时间.netOp.customExpireTime = [NSString stringWithFormat:@"%d",2];

Of course, in order to ensure the integrity of the source code, and to facilitate future updates of mknetworkkit, it is recommended to customize a mknetworkoperation subclass to accomplish the same function.

Mknetworkkit Custom Cache validity time

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.