웹 기반 데이터를 유지하기로 선택한 방식이 내 iPhone 앱에서 올바른 방법인지 결정하려고합니다.iphone data persistence sanity check
아직 코어 데이터의 복잡함에 익숙하지 않아서 Sqlite를 FMDB 래퍼와 함께 사용하고 있습니다.
나는 theElements 샘플 유사한 싱글을 사용하는 클래스가 : 여기
내가 지금하고 있어요 방법이다. 정적 공유 인스턴스가 만들어지고 두 개 이상의보기간에 공유됩니다. 데이터가 다운로드되어이 클래스의 배열에로드됩니다. 나는 Sqlite에 다운로드를 저장한다. 초기화시 저장된 Sqlite 데이터를로드하고 타임 스탬프마다 웹에서 업데이트를 가져옵니다.
"올바른"방법입니까? 이게 제대로 보이나요?
간결하게하기 위해 의사 코드를 많이 잘라 냈습니다.
@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;
}
http : //cocoawithlove.googlepages.com/SynthesizeSingleton.h.zip 매크로를 사용하면 클래스를 직접 코딩하는 대신 싱글 톤으로 만들 수 있습니다. 그냥 + (ClassName *) sharedClassName을 추가하십시오; (ClassName 대체) 구현에서 synthesizesingleton.h 파일을 가져오고 SYNTHESIZE_SINGLETON_FOR_CLASS (ClassName);을 수행하십시오. 이점은 여러 개의 싱글 톤 클래스가있는 경우 코드가 중복되지 않는다는 것입니다. – mk12
멋진 팁. 감사! 위의 내용이 데이터 및 영구 저장 장치에 적합한 방법이라고 생각하십니까? –