satishiOS25
satishiOS25

Reputation: 531

how to achieve caching the data of web services locally retrieve back in iPhone Application?

I'm new for iOS, i'm using AF-Networking framework for fetching the web services and successfully getting the data and loading it to the UI elements now what's my issue is Application performance is slow and it's loading form the web service every time i want to cache the images and data locally and increase the performance of the application can anyone out there can help me with the proper solution.

Thanks in Advance

Upvotes: 0

Views: 490

Answers (2)

M David
M David

Reputation: 260

I think you have to use NSUserDefaults, follow the following

1: create NSArray of the data u need

2: Save into NSUserDefaults with a key

3: Now you are able to use that NSArray in every class of the application

4: if you want update data simply again save into NSUserDefaults with old key

So you don't need to download data every time, just download once and save into NSUserDefaults and use. if there is problem in saving data in NSUserDefaults then look at code bellow

To store the information:

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:arrayOfImage forKey:@"tableViewDataImage"];
[userDefaults setObject:arrayOfText forKey:@"tableViewDataText"];

To retrieve the information:

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSArray *arrayOfImages = [userDefaults objectForKey:@"tableViewDataImage"];
NSArray *arrayOfText = [userDefaults objectForKey:@"tableViewDataText"];
// Use 'yourArray' to repopulate your UI

Upvotes: 0

dopcn
dopcn

Reputation: 4218

I think what you are talking about has nothing to do with NSURLCache but to save the previous network request data locally. Then next time before you send a network request you can read from local file first

There are many different ways of saving data locally like Core Data NSKeyedArchiver plist FMDB. Here is my way using NSKeyedArchiver.
(put the interface here you can read the implementation at this link https://github.com/dopcn/HotDaily/blob/master/HotDaily/HDCacheStore.m)

@interface HDCacheStore : NSObject

+ (HDCacheStore *)sharedStore;
@property (copy, nonatomic) NSArray *mainListCache;
@property (copy, nonatomic) NSArray *funinfoListCache;
- (BOOL)save;

@end

in XXXAppDelegate.m

- (void)applicationWillResignActive:(UIApplication *)application //Or some other place
{
    if ([[HDCacheStore sharedStore] save]) {
        NSLog(@"save success");
    } else {
        NSLog(@"save fail");
    }
}

Upvotes: 1

Related Questions