URL에서 데이터를 다운로드해야합니다 (JSON 형식으로 데이터를 인쇄 함). 앱의 AppDelegate.m 파일에 앱의 '구성'파일에 저장해야합니다. 앱을 실행할 때 어떤 이유로 든 dispatch_async 코드를 건너 뜁니다. 왜 이런 일이 일어나고 어떻게 해결할 수 있습니까?iOS 코드가 dispatch_async로 건너 뜁니다.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//Download the config.json file
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
NSString *configFileUrl = @"http://webserviceurl";
//NSString *downloadToFile = @"Configuration.json";
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:configFileUrl]];
[self performSelectorOnMainThread:@selector(writeDataToConfigurationJsonFile:) withObject:data waitUntilDone:YES];
});
//More code below
내가 응용 프로그램의 문서 디렉토리에있는 파일에 데이터를 쓰고있는 곳은 다음과 같습니다
-(void)writeDataToConfigurationJsonFile:(NSData*)jsonData{
NSString *content = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
//get the documents directory:
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//make a file name to write the data to using the documents directory:
NSString *fileName = [NSString stringWithFormat:@"%@/Configuration.json", documentsDirectory];
//save content to the documents directory
[content writeToFile:fileName
atomically:YES
encoding:NSUTF8StringEncoding
error:nil];
}
이 dispatch_async'가 대신 할 예정이다 정확하게'인 당신이 그 위에 건너 뛰는 아니에요 –
은 즉시 반환 것 작동한다있는 NSURLConnection 로딩 데이터를 비동기 방법을 살펴 - 여기에 코드의 마지막 덩어리입니다 완료 될 때까지 차단. – iwasrobbed