Saving data using local storage on an iPhone - which way to go?

Go To StackoverFlow.com

1

I'm building a web-app which is communicating with a database. I want the web-app to download all info from my database and then save it locally so it can be accessed and edited (even if the user is offline) locally and then synced back to my database (when the user is online).

Every row in my database consists of 2 strings and 3 integers and I've got around 1000-1500 rows in my database (that might give you and idea about the size of my db).

So which is the best way to go? Is it possible to cache this kind of data on the users device somehow? Like retrieving the database via JSON and than storing it locally in a javascript array which the user can access and work on (as mentioned: the later part should be able to work even if the user is offline). All input on this is greatly appreciated. Am I at least thinking in the right direction here?

EDIT: I just want to clarify that I'm working with a web app. An app devoloped with HTML, CSS and Javascript. I'm not doing a native objective C iOS app.

2012-04-03 21:03
by nalas
Look into SQLite. The framework is included with XCode. libsqlite3.0.dyli - PaulG 2012-04-03 21:07
Be sure to observe the new Data Storage Guidelines. You get slapped down pretty harshly if you don't - Hot Licks 2012-04-03 21:16
Guys, I just want to clarify that I'm working with a web app. An app devoloped with HTML, CSS and Javascript. I'm not doing a native objective C iOS app - nalas 2012-04-03 23:19


0

The best way to store the data specially if you are using web technologies to develop your mobile app is to use SQLite.You can store, retrieve your data in offline or online mode. Also another advantage is you can port your database to any mobile device or Desktop app if you decided to go native.

If you have more question or require help contact me and i can give you more info of how to use SQLite with web technologies?

If you want to save time on designing a Web GUI for your mobile app you can use Mobile web framework, Which speed up development time and allows you to access some native API's on the device. I recommend Sencha: http://www.sencha.com/products/touch/ (Really nice framework base on Html5, JS, CSS3)

2012-04-04 02:15
by darv


2

make a class of NSObject

@interface abc : NSObject<NSCoding>

@property..... // add properties that you want to save 

-(void)initWithDictionary:(NSMutableDictionary *)dictionary; // override init by passing dictionary with values of your properties

@end

in abc.m file 

implement these functions

-(void)initWithDictionary:(NSMutableDictionary *)dictionary
{

    yourProperty=[dictionary valueForKey:@"propertyKey"];

}

-(void) encodeWithCoder:(NSCoder *)aCoder{

    [aCoder encodeObject:yourProperty forKey:@"propertyKey"];

}

-(id) initWithCoder:(NSCoder *)aDecoder{

    self =[super init];

    if(self){

        [self setyourProperty:[aDecoder decodeObjectForKey:@"yourProperty"]];

    }
    return self;
}


now make a shared class that can be accessed from anywhere

@interface xyz : NSObject
{
    NSMutableArray *allItems;
}

+(xyz *) sharedStore;

-(NSMutableArray *)allItems;

-(abc *) createItem:(NSMutableDictionary *)dictionary;

-(NSString *) saveItemPath;

-(BOOL)saveChanges;


now in xyz.m implement these functions

-(id)init{

    self=[super init];

    if(self){

        NSString *path=[self saveItemPath];

        allItems=[NSKeyedUnarchiver unarchiveObjectWithFile:path];

        if(!allItems){

            allItems=[[NSMutableArray alloc]init] ;//]WithObjects:@"No more item in store", nil];

        }

    }

    return self;
}
+(xyz *)sharedStore{

    static xyz *sharedStore=nil;

    if(!sharedStore){

        sharedStore=[[super allocWithZone:nil]init];

    }

    return sharedStore;

}
+(id)allocWithZone:(NSZone *)zone{

    return [self sharedStore];

}
-(NSArray *)allItems{

    //[allItems insertObject:@"No more items are in store" atIndex:[allItems count]];
    return allItems;

}

-(abc *) createItem:(NSMutableDictionary *)dictionary
{
    abc *p=[[abc alloc] init];
    [p initWithDictionary:dictionary];
    [allItems insertObject:p atIndex:0];
    return p;
}

-(NSString *) saveItemPath
{
    NSArray *documentdirectories=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

    NSString * documentDirectory=[documentdirectories objectAtIndex:0];

    return [documentDirectory stringByAppendingPathComponent:@"items.archive"];

}

-(BOOL) saveChanges
{

    NSString *path=[self saveItemPath];

    return [NSKeyedArchiver archiveRootObject:allItems toFile:path];

}


now you can use the global variable like this

[xyz sharedStore]allItems]

now do one more thing, add this line to application did enter background

[xyz sharedStore]saveChanges];
2013-07-12 05:15
by user2534255


0

You should use Core Data (which uses SQLite).

2012-04-03 22:13
by Michael Frederick
That's not entirely true. Core Data uses SQLite as its backing store if you define it to do so. There are a number of other stores you can use with Core Data. It's not like by not defining a store, you get SQLite by default. This has to be set explicitly - jmstone617 2012-04-03 22:18


0

You need to look into persistence, and the best option for that is Core Data. Aside from persistence, you get a lot of other great benefits that I and others have outlined here and here, for instance. As mentioned, you can use SQLite as the backing store for the app, and then you can access the object representations of the entries however you wish. If you're dealing with syncing, you may also want to look into iCloud, which you can find information about here.

2012-04-03 22:23
by jmstone617
I just want to clarify that I'm working with a web app devoloped with HTML, CSS and Javascript. I'm not doing a native objective C iOS app. Isn't that what you are talking about - nalas 2012-04-03 22:37
Yes, this was assuming you were building an app that had native components - jmstone617 2012-04-03 22:38
Ads