Well, I'm trying to decide if the way I chose to persist web-based data is the right way in my iPhone app.
I haven't yet dove into the intricacies of Core Data, so am still using Sqlite with the FMDB wrapper.
Here is how I'm doing it now:
I have a class that uses a singleton similar to theElements sample. A static shared instance is created, and is shared among two or more views. The data is downloaded and loaded into an array in this class. I store the downloads in Sqlite. I load the Sqlite stored data on initialization, and get any updates from the web per a timestamp.
Am I doing this the "correct" way? Does this look proper?
Here is some pseudocode heavily cut for brevity.
@implementation theClass
static theClass *sharedInstance = nil;
...
- (void)requestDone:(Request *)request
{
NSDictionary *results=[[request responseString] JSONValue];
[self._array removeAllObjects];
[self._array addObjectsFromArray:results];
[self updateDatabase]; //stores any new results
[[NSUserDefaults standardUserDefaults] setObject:self.lastUpdated forKey:@"LastAccessed"];
[[NSUserDefaults standardUserDefaults] synchronize];
[self didFinish];
}
-(void)setupArray {
self._array=[NSMutableArray array];
...
}
...
-(void)getUpdates {
NSURL *jsonURL = [NSURL URLWithString:@"URL TO THE WEBDATA"];
request = NSURLRequst....
}
- init {
if (self = [super init]) {
[self setupArray];
}
return self;
}
+ (theClass *)shared {
@synchronized(self) {
if (sharedInstance == nil) {
[[self alloc] init]; // assignment not done here
}
}
return sharedInstance;
}
+ (id)allocWithZone:(NSZone *)zone {
@synchronized(self) {
if (sharedInstance == nil) {
sharedInstance = [super allocWithZone:zone];
return sharedInstance; // assignment and return on first allocation
}
}
return nil; //on subsequent allocation attempts return nil
}
- (id)copyWithZone:(NSZone *)zone {
return self;
}
- (id)retain {
return self;
}
- (unsigned)retainCount {
return UINT_MAX;}
- (void)release {
//do nothing
}
- (id)autorelease {
return self;
}