2011-03-09 7 views
0

나는 내 웹 서버에있는 plist에서 데이터를 가져 오는 응용 프로그램을 개발 중입니다. 따라서 응용 프로그램은 네트워크 연결에 의존합니다. 사용자가 오프라인에서 내 응용 프로그램을 사용할 수 있기를 원하므로 네트워크 연결로 응용 프로그램이로드 될 때마다 사용자 장치에 plist 복사본을 저장합니다. 거기에서 장치에있는 plist에서 데이터를 읽습니다.저장되지 않은 plist에서 읽기

그래도 문제가 있습니다. 나는 AppDelegate의 didFinishLaunchingWithOptions 메소드에서 데이터 다운로드를 시작하고있다. 이것은 다음과 같이 수행됩니다

if(hasConnection) { // returns TRUE or FALSE based on Apple's example on checking network reachability 
     NSLog(@"Starting download of data."); 

     // loading using NSURLConnection 
     NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:FETCH_URL] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; 

     // create the connection with the request 
     // and start loading the data 
     NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 
     if (theConnection) { 
      // Create the NSMutableData to hold the received data. 
      // receivedData is an instance variable declared elsewhere. 
      receivedData = [[NSMutableData data] retain]; 
     } 
    } 

은 그 때 나는 connectionDidFinishLoading

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    // throw data into a property list (plist) 
    NSMutableArray *tmpArray = [NSPropertyListSerialization propertyListFromData:receivedData mutabilityOption:NSPropertyListMutableContainers format:nil errorDescription:nil]; 

    NSMutableArray *plistArray = [[NSMutableArray alloc] initWithArray:tmpArray]; 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 

    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Bands.plist"]; 

    [plistArray writeToFile:path atomically:YES]; 
    [plistArray release]; 

    // release the connection, and the data object 
    [connection release]; 
    [receivedData release]; 
} 

내 PLIST Bands.plist에 데이터를 추가하지만 처음으로 응용 프로그램을로드 할 때, 그것은 충돌합니다. 나는 이것이 아직 저장되지 않았음에도 불구하고이 데이터에 접근하려는 응용 프로그램 때문이라고 생각합니다. 로컬로 저장된 데이터에 도달하려고하는 부분을 제거하면 아무런 문제가 없습니다. 다시 추가하고 응용 프로그램을 다시 시작하면 문제가 발생하지 않습니다 (두 번째로드).

누구든지이 문제를 해결하는 방법을 알고 있습니까?

마치 응용 프로그램이 아직 존재하지 않는 데이터를로드하고 처리하는 것처럼 보입니다.

+0

- 당신은 선이 충돌하는 찾을 수있을 것입니다. – deanWombourne

+0

@deanWombourne 'NSInvalidArgumentException'이 발생합니다 : '- [__ NSCFDictionary setObject : forKey :] :없는 값을 삽입하려고 시도합니다.'라는 정보가없는 정보를 처리하려는 방법 때문입니다. plist가 준비되기를 기다릴 수있는 방법이 있습니까? – simonbs

+0

앱이 시작될 수있는 기본 plist가 함께 제공되지 않는 이유는 무엇입니까? 그런 다음 기다리거나 기다리는 것처럼 보이는 동안 백그라운드에서 더 많은 데이터를 얻을 수 있습니다. (또는 당신은 @ Mundi의 아이디어를 시도 할 수있다!) – deanWombourne

답변

0

간단히 먼저 PLIST의 존재를 확인해보십시오 :

if ([[NSFileManager defaultManager] fileExistsAtPath:path]) { 
    ... // read the data, etc. 
} 
else { 
    ... // don't try to read it 
} 

건배,
샤샤 콘솔 및 디버거가 당신에게 무엇을

+0

대단히 고마워. 더미 파일을 아직 복사하지 않으면 문제를 해결하는 것 같습니다. – simonbs

관련 문제