코어 데이터 또는 SQLite와 같은 데이터를 유지하기 위해 일종의 데이터베이스를 사용하는 것이 좋습니다. SQLite (내 강점)를 사용하면 데이터베이스 파일을 열어두고 배열의 변경 내용을 트랜잭션 방식으로 저장할 수 있습니다. SQLite의 journalling mechanism 덕분에 무엇이든 잘못 될 가능성은 거의없고 그 엄청나게 철저한 testing regime 덕분입니다. 핵심 데이터는 SQLite를 기반으로하므로 동일한 보증이 적용됩니다.
EDIT (RE : 코멘트)
과잉 문제가 해결되기 위해 용액의 비용이 과다 상황에서만 문제가된다. SQLite는 사용을 요구하는 것 주위에 앉아 있습니다. 첫째로, 당신은 더 많은 의미있는 데이터를 표현하는 데이터 구조를 만들 수 있습니다
@interface Memo {
NSString *title;
NSString *textBody;
};
@property (nonatomic, retain) title;
@property (nonatomic, retain) textBody;
@end
@implementation Memo
@synthesize title, textBody;
@end
SQLite는의 원시 인터페이스는 약간 투박, 그래서 저장하고 이러한 간단한 일을 유지하기 위해 C++ wrapper library를 사용하여로드 할 것 :
- (NSString *)databasePath {
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
// Choose a filename for your database.
return [documentsDir stringByAppendingPathComponent:@"data.db"];
}
- (void)saveMemos:(NSArray *)memos {
try {
sqlite3_connection con([[self databasePath] cStringUsingEncoding:NSUTF8StringEncoding]);
con.executenonquery("CREATE TABLE IF NOT EXISTS memo (i, title, textBody)");
sqlite3_transaction trans(con);
{
sqlite3_command cmd(con,
"INSERT INTO memo (i, title, textBody) VALUES (?, ?, ?)");
for (int i = 0; i < memos.count; i++) {
Memo *memo = (Memo *)[memos objectAtIndex:i];
cmd.bind(1, i);
cmd.bind(2, [memo.title cStringUsingEncoding:NSUTF8StringEncoding]);
cmd.bind(3, [memo.textBody cStringUsingEncoding:NSUTF8StringEncoding]);
cmd.executenonquery();
}
}
trans.commit();
} catch(exception &ex) {
NSLog(@"SQLite3 error: %s", ex.what());
}
}
- (NSMutableArray *)loadMemos {
sqlite3_connection con([[self databasePath] cStringUsingEncoding:NSUTF8StringEncoding]);
NSMutableArray *result = [NSMutableArray arrayWithCapacity:10];
if (con.executeint("SELECT COUNT(*) FROM sqlite_master WHERE name = 'memo'")) {
sqlite3_command cmd("SELECT title, textBody FROM memo ORDER BY i");
sqlite3_reader rd = cmd.executereader();
while (rd.read()) {
Memo *memo = [[Memo alloc] init];
memo.text = [NSString stringWithFormat:@"%s", rd.getstring(0).c_str()];
memo.titleBody = [NSString stringWithFormat:@"%s", rd.getstring(1).c_str()];
[result addObject:memo];
}
}
return result;
}
(경고 :.이 코드는 완전히 안된)
집에서 재배 파일 형식이보다 적은 코드를 필요로하지 않을이며, 거의 확실히 덜 강력한 될 것입니다.
또한 코어 데이터를 고려해야하지만 직접 사용하지 않았으므로 그 부분에 대한 지원은 제공 할 수 없습니다.
에서이 질문은 매우 읽기 어렵 호출하는 응용 프로그램 대리인의
applicationWillTerminate,
및로드 방법에서 부르는 "저장"방법을 만들 수 있습니다. 예를 들어, "2 개 또는 2 개 이상의 배열을 갖고 싶습니다 ..."는 의미는 무엇입니까? –이 (가) 편집되었습니다. :) thanks – jarryd