There are five ways to cache data locally in iOS:
1. Direct Write file: The object can be stored nsstring, Nsarray, Nsdictionary, NSData, NSNumber, the data are all stored in a property list file (*.plist file).
2.NSUserDefaults (preferences), used to store application settings information, the file is placed in the Perference directory.
3. Archive operation (Nskeyedarchiver), different from the previous two, it can put the custom object in the file.
4.coredata:coredata is an integrated database launched by Apple's official iOS5, which uses an ORM (Object Relational Mapping) object-relational mapping technique to convert objects into data and store them in a local database. CoreData to improve efficiency, even storing data in different databases, and putting local data into memory when used, makes access faster. We can choose CoreData data storage methods, including SQLite, XML and other formats. But it is also the CoreData is completely object-oriented, and its performance is less efficient than the original database. In addition to this, CoreData has data validation, undo and other functions that are functionally the most persistent solutions.
5.fmdb:fmdb is an iOS platform SQLite database framework, FMDB in OC to encapsulate the SQLite C language API, use more object-oriented, save a lot of cumbersome, redundant C language code, compared to Apple's own core data framework, More lightweight and flexible, providing multi-threaded secure database operation method, effectively prevent data confusion.
Next I'll cover the third caching Mechanism archive operation:
As a durable solution for lightweight storage, data archiving is encrypted, and data is converted to binary data as it is archived, so security is much higher than the list of attributes. In addition to using archiving, we can write complex objects to a file, and the same way that objects are written to disk, regardless of how many objects are added.
Use Nskeyedarchiver to serialize the custom data and save it in the sandbox directory. The premise of using this archive is to have the stored data model comply with the Nscoding protocol and implement its two protocol methods. (Of course, if the nssecurecoding protocol can be adhered to for more secure storage, this is the new feature after IOS6)
The main benefit of storing data using archive operations is that nskeyedarchiver can store custom objects, unlike the previous two methods, which store only a few commonly used data types.
/** code Example **/
First, create a class that inherits NSObject, which adheres to the Nscoding protocol
#import <Foundation/Foundation.h>
@interface jxitem:nsobject<nscoding>
@property (nonatomic, assign) Nsinteger collect;
@property (nonatomic, assign) Nsinteger collect_num;
@property (nonatomic, assign) Nsinteger CourseID;
@property (nonatomic, copy) NSString *coverpic;
@property (nonatomic, assign) long long date_end;
@property (nonatomic, assign) long long Date_start;
@property (nonatomic, assign) Nsinteger GroupID;
@property (nonatomic, assign) Nsinteger Invitation_code;
@property (nonatomic, assign) Nsinteger is_live;
@property (nonatomic, assign) Nsinteger paid;
@property (nonatomic, assign) Nsinteger play_num;
@property (nonatomic, copy) NSString *price;
@property (nonatomic, assign) Nsinteger product_id;
@property (nonatomic, copy) NSString *product_info;
@property (nonatomic, copy) NSString *productname;
@property (nonatomic, copy) NSString *realname;
@property (nonatomic,assign) Nsinteger recomment;
@property (nonatomic, copy) NSString *stream_url;
@property (nonatomic, copy) NSString *time_duan;
@property (nonatomic,assign) Nsinteger user_id;
@property (nonatomic, assign) Nsinteger ViewType;
@property (nonatomic, assign) Nsinteger live_in;
+ (Instancetype) randomdictionary: (Nsdictionary *) dictionary;
@end
Second, in the model of the. m file for read parsing, archiving and archiving
#import "JXItem.h"
@implementation Jxitem
+ (Instancetype) randomdictionary: (Nsdictionary *) Dictionary {
Jxitem *item = [[Jxitem alloc] init];
[Item setvaluesforkeyswithdictionary:dictionary];
return item;
}
-(void) Encodewithcoder: (Nscoder *) Acoder {
[Acoder encodeInteger:self.collect forkey:@ "collect"];
[Acoder encodeInteger:self.collect_num forkey:@ "Collect_num"];
[Acoder encodeInteger:self.courseid forkey:@ "CourseID"];
[Acoder encodeObject:self.coverpic forkey:@ "Coverpic"];
[Acoder encodeInteger:self.date_end forkey:@ "Date_end"];
[Acoder encodeInteger:self.date_start forkey:@ "Date_start"];
[Acoder encodeInteger:self.groupid forkey:@ "GroupID"];
[Acoder encodeInteger:self.invitation_code forkey:@ "Invitation_code"];
[Acoder encodeInteger:self.paid forkey:@ "paid"];
[Acoder encodeInteger:self.play_num forkey:@ "Play_num"];
[Acoder encodeObject:self.price forkey:@ "Price"];
[Acoder encodeInteger:self.product_id forkey:@ "product_id"];
[Acoder encodeObject:self.product_info forkey:@ "Product_info"];
[Acoder encodeObject:self.productname forkey:@ "ProductName"];
[Acoder encodeObject:self.realname forkey:@ "Realname"];
[Acoder encodeInteger:self.recomment forkey:@ "Recomment"];
[Acoder encodeObject:self.stream_url forkey:@ "Stream_url"];
[Acoder EncodeObject:self.time_duan forkey:@ "Time_duan"];
[Acoder encodeInteger:self.user_id forkey:@ "user_id"];
[Acoder encodeInteger:self.viewtype forkey:@ "ViewType"];
[Acoder encodeInteger:self.live_in forkey:@ "live_in"];
}
-(Instancetype) Initwithcoder: (Nscoder *) Adecoder {
self = [super init];
if (self) {
_collect = [Adecoder decodeintegerforkey:@ "collect"];
_collect_num = [Adecoder decodeintegerforkey:@ "Collect_num"];
_coverpic = [Adecoder decodeobjectforkey:@ "Coverpic"];
_date_end = [Adecoder decodeintegerforkey:@ "Date_end"];
_date_start = [Adecoder decodeintegerforkey:@ "Date_start"];
_groupid = [Adecoder decodeintegerforkey:@ "GroupID"];
_invitation_code = [Adecoder decodeintegerforkey:@ "Invitation_code"];
_paid = [Adecoder decodeintegerforkey:@ "paid"];
_price = [Adecoder decodeobjectforkey:@ "Price"];
_product_id = [Adecoder decodeintegerforkey:@ "product_id"];
_product_info = [Adecoder decodeobjectforkey:@ "Product_info"];
_productname = [Adecoder decodeobjectforkey:@ "ProductName"];
_realname = [Adecoder decodeobjectforkey:@ "Realname"];
_recomment = [Adecoder decodeintegerforkey:@ "recomment"];
_stream_url = [Adecoder decodeobjectforkey:@ "Stream_url"];
_time_duan = [Adecoder decodeobjectforkey:@ "Time_duan"];
_viewtype = [Adecoder decodeintegerforkey:@ "ViewType"];
}
return self;
}
@end
In addition, establish a data management class Jxitemstore
#import <Foundation/Foundation.h>
@class Jxitem;
@interface Jxitemstore:nsobject
@property (nonatomic, copy) NSString *archivename;
@property (nonatomic, strong) Nsarray *allitem;
+ (instancetype) Sharedstore;
-(Jxitem *) CreateItem: (Jxitem *) item;
/**
* Delete Objects
*
* @param item needs to be deleted
*/
-(void) RemoveItem: (Jxitem *) item;
/**
* Remove Objects
*
* @param fromIndex The starting position of the removed object
* @param the location after Toindex removal
*/
-(void) Moveitematindex: (Nsinteger) FromIndex Toindex: (Nsinteger) Toindex;
-(BOOL) savechanages;
@end
//
#import "JXItemStore.h"
#import "JXItem.h"
@interface Jxitemstore ()
@property (nonatomic, strong) Nsmutablearray *privateitems;
@end
@implementation Jxitemstore
+ (Instancetype) Sharedstore {
static Jxitemstore *sharedstore = nil;
Static dispatch_once_t Oncetoken;
Dispatch_once (&oncetoken, ^{
Sharedstore = [[Self alloc] init];
});
return sharedstore;
}
-(BOOL) Savechanages {
return [Nskeyedarchiver ArchiveRootObject:self.privateItems
Tofile:[self Itemarchivepath]];
}
-(Nsarray *) GetAll {
[Self.privateitems setarray:[nskeyedunarchiver unarchiveobjectwithfile: [self itemarchivepath]];
return self.privateitems;
}
-(void) RemoveItem: (Jxitem *) Item {
[Self.privateitems Removeobjectidenticalto:item];
//
}
-(void) Moveitematindex: (Nsinteger) FromIndex Toindex: (Nsinteger) Toindex {
if (FromIndex = = Toindex) {
Return
}
Jxitem *item = Self.privateitems[fromindex];
[Self.privateitems Removeobjectatindex:fromindex];
[Self.privateitems Insertobject:item Atindex:toindex];
}
-(Nsmutablearray *) Privateitems {
if (!_privateitems) {
_privateitems = [[Nsmutablearray alloc] init];
[Self getAll];
}
return _privateitems;
}
-(Nsarray *) Allitem {
return [self.privateitems copy];
}
-(Jxitem *) CreateItem: (Jxitem *) Item {
[Self.privateitems Addobject:item];
return item;
}
-(NSString *) Itemarchivepath {
Nsarray *documentdirectories = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentdirectory = [Documentdirectories firstobject];
if (!_archivename) {
return [documentdirectory stringbyappendingpathcomponent:[nsstring stringwithformat:@ "putcharTems.archive"];
}
return [documentdirectory stringbyappendingpathcomponent:[nsstring stringwithformat:@ "% @items. Archive", _ Archivename]];
}
This way, we can request the data. The Zxhttprequestmanager class is just a network request management class that encapsulates the Afnetworking request library
#define KDOMAIN_NAME @ "www.niulaile.tv"//test.91kanjian.com "
#import "ViewController.h"
#import "ZXHttpRequestManager.h"
#import "JXItemStore.h"
#import "JXItem.h"
@interface Viewcontroller ()
@end
@implementation Viewcontroller
-(void) Viewdidload {
[Super Viewdidload];
Additional setup after loading the view, typically from a nib.
NSString *url = [NSString stringwithformat:@ "Http://%@//kj.php?m=course&a=dotv_newindex", KDomain_name];
Jxitemstore *manager = [Jxitemstore Sharedstore];
Manager.archivename = @ "Jxmodel";
__weak typeof (Manager) Weakmanager = Manager;
[[Zxhttprequestmanager Manager] Post:url parameters:nil success:^ (zxhttprequestcode code, ID response) {
if (code = = zxhttprequestsuccess) {
if ([response[@ "status"] integervalue] = = 1) {
For (Nsdictionary *dict in response[@ "Data"]) {
Jxitem *item = [Jxitem randomdictionary:dict];
[Weakmanager Createitem:item];
}
Nsarray *array = Weakmanager.allitem;
NSLog (@ "%@", array);
}
}
} failure:^ (Nsinteger errorCode, Nserror *error) {
Nsarray *array = Weakmanager.allitem;
NSLog (@ "%@", array);
}];
}
-(void) didreceivememorywarning {
[Super didreceivememorywarning];
Dispose of any resources the can be recreated.
}
@end
IOS nskeyedarchiver (Lightweight cache)