데이터베이스에 삽입해야하는 행이 많습니다. 필요한 정보가 탭으로 구분 된 약 50 개의 .txt 파일을 다운로드합니다. 나는 데이터를 파싱하고 동일한 방법으로 데이터베이스에 삽입한다. 내가 별도의 방법으로 있었을 때,에 시뮬레이터 내 27 "은 메모리가 부족하기 때문에 아이맥이 충돌 할 방법은 다음과 같습니다.FMDB에 데이터를 삽입하면 장치에 충돌이 발생하여 대부분의 메모리 효율적인 방법으로 sqlite에 데이터를 삽입 할 수 있습니다.
- (BOOL)insertDataWithFileName:(NSString *)fileName {
NSLog(@"%s %@", __FUNCTION__, fileName);
if (_db == nil) {
return NO;
}
// Get the data from the file
NSData *data = [[NSData alloc] initWithContentsOfFile:[[self applicationDocumentsDirectory] stringByAppendingPathComponent:fileName]];
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// Parse each line by newlines first
NSArray *lines = [responseString componentsSeparatedByString:@"\n"];
NSString *ID = [[fileName componentsSeparatedByString:@"."] objectAtIndex:0];
// Set up the database insert statement
NSString *tableName = @"Hardcodedtablename"; // remove after prototype
BOOL oldshouldcachestatements = _db.shouldCacheStatements;
[_db setShouldCacheStatements:YES];
[_db beginTransaction];
NSString *insertQuery = [NSString stringWithFormat:@"INSERT INTO %@ values(null, ?, ?, ?, ?, ?, ?, ?);", tableName];
BOOL success;
// Parse each line by tabs
for (NSString *line in lines) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSArray *fields = [line componentsSeparatedByString:@"\t"];
// Need to check since some of the .txt files have an empty newline at the EOF.
if ([fields count] == NUM_VARIANT_FIELDS) {
NSNumber *loc = [NSNumber numberWithInteger:[[fields objectAtIndex:0] integerValue]];
NSString *carType = [fields objectAtIndex:1];
NSString *model = [fields objectAtIndex:2];
NSString *AA = [fields objectAtIndex:7];
NSString *BB = [fields objectAtIndex:8];
NSNumber *freq = [[fields objectAtIndex:11] isEqualToString:@""] ? [NSNumber numberWithDouble:-1.0] : [NSNumber numberWithDouble:[[fields objectAtIndex:11] doubleValue]];
NSArray *argArray = [[NSArray alloc] initWithObjects:ID, loc, carType, model, AA, BB, freq, nil];
success = [_db executeUpdate:insertQuery withArgumentsInArray:argArray];
[argArray release];
}
[pool drain];
}
[_patientAnnotatedDatabase commit];
[_patientAnnotatedDatabase setShouldCacheStatements:oldshouldcachestatements];
return success;
}
당신은 루프, 실제로 돈 내에서 볼 수 있듯이 ' 배열에있는 모든 파일이 \ t에 의해 분리 된 후에 필요합니다. for 루프에서 메모리 풋 프린트를 줄이기 위해 autorelease 풀을 설정합니다. 가장 큰 파일에는 27 만 개의 행이 있습니다. 약 50 개 정도의 파일이 있습니다. 따라서 장치에 2 개의 파일이있는 경우 오류가 발생합니다. 오류 메시지는 표시되지 않습니다 ..
아니면 서버 측에서 데이터베이스를 만들고 대신 장치에 데이터베이스를 다운로드해야합니까? 서버에서 장치로 데이터베이스를 다운로드 한 경우 FMDB는 이러한 테이블을 한 DB에서 다른 DB로 전송할 수있는 기능을 지원합니까? 내가 인터넷 검색을하는 동안 그와 같은 것을 찾을 수 없었습니다. 감사!
예 Allocations 템플릿 세트가있는 Instruments에서 실행했습니다. 나는 파일을 파싱 할 때 메모리가 커지는 것을보고, 파싱을 끝내고 for 루프가 끝나면 즉시 축소되는 것을 볼 수있다.NSBundle에 데이터를 포함 시키면 iPad에서 정보를 올바르게 처리 할 수 있습니다. 나는 네이티브 경험이 지금처럼 빨리 될 수 있도록 장치에 보관하는 것을 선호합니다. 당신의 분석에서, 아마 코드 일 것 같은데? iMac에서 여러 번 실행했는데 문제가 없습니다. 흠 ... 웹 서버를 통해 데이터베이스를 전송하는 것은 옵션이 아닙니다. – Crystal
그건 정말 이상한 ... 응용 프로그램이 NSBundle의 데이터와 잘 작동한다면 이는 메모리 문제가 아니라는 것을 의미합니다. 앱이 언제 충돌하나요? 특정 파일의로드가 중단되면 해당 파일에 문제가있을 수 있습니다. – pop850