현재 내 앱의 didFinishLaunchingWithOptions
에있는 파일을 검색하여 응용 프로그램 디렉토리에 저장하는 기능을 사용하고 있습니다.원격 파일을 검색 할 때 연결 속도가 느려 앱 충돌이 발생하지 않도록하십시오.
약한 연결이있을 때 앱이 중단 될 때 앱이 중단되는 것으로 나타났습니다. 나는 앱이 깨지기 전에 애플이 허락하는 20 초의 시간 제한이 있음을 읽었다. 이 올바른지? 그렇다면 매우 약한 연결을 제외하고는 앱이 완벽하게 작동하므로 이것이 내 문제의 원인이라고 생각합니다.
어떻게하면 내 논리를 수정하여이를 보완 할 수 있습니까?
- (void)writeJsonToFile
{
//applications Documents dirctory path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//live json data url
NSString *stringURL = @"http://link-to-my-data.json";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
//attempt to download live data
if (urlData)
{
NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"data.json"];
[urlData writeToFile:filePath atomically:YES];
}
//copy data from initial package into the applications Documents folder
else
{
//file to write to
NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"data.json"];
//file to copy from
NSString *json = [ [NSBundle mainBundle] pathForResource:@"data" ofType:@"json" inDirectory:@"html/data" ];
NSData *jsonData = [NSData dataWithContentsOfFile:json options:kNilOptions error:nil];
//write file to device
[jsonData writeToFile:filePath atomically:YES];
}
은}
내 스레드에서 결함을 지적 해 주셔서 감사합니다. 이것은 첫 번째 응용 프로그램이므로 스레딩 및 메모리 관리의 개념을 여전히 조정하고 있습니다. 이 메서드의 현재 구현을 다시 생각한 후에는 앱이 시작된 후에 백그라운드에서 데이터를 업데이트하려고합니다. 귀하의 대답은 @SVGreg와 유사하지만 제안이 먼저 제시되었으므로 여기에 답이 주어집니다. –