2012-04-11 3 views
0

일부 값을 저장하기 위해 plist를 만들었지 만 테스트 중에 응용 프로그램이 닫힌 후에 새로 작성한 plist를 유지한다는 사실을 알고 있습니다. &이 멀티 태스킹에서 제거되었습니다. 그러나 그 응용 프로그램이 멀티 태스킹에서 제거되면 그 plist 내 값을 느슨하게하지만 애플 리케이션이 닫혀 있다면 ...plist를 유지하지만 plist 값이 손실 됨

이것은 내 plist 컨트롤러 클래스에서 모든 읽기/쓰기를 관리하는 데이터를 저장하는 방법입니다/등 저장

- (void) saveData:(NSString *)methodName signature:(NSString *)pSignature Version:(NSNumber *)pVersion request:(NSNumber *)rNumber dataVersion:(NSNumber *)dvReturned cacheValue:(NSNumber *)cValue; 
    { 
     // get paths from root direcory 
     NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); 
     // get documents path 
     NSString *documentsPath = [paths objectAtIndex:0]; 
     // get the path to our Data/plist file 
     NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"EngineProperties.plist"]; 

     // set the variables to the values in the text fields 
     self.signature = pSignature; 
     self.version = pVersion; 
     self.request = rNumber; 
     self.dataVersion = dvReturned; 

     //do some if statment stuff here to put the cache in the right place or what have you. 
     if (methodName == @"manu") 
     { 
      self.man = cValue; 
     } 
     else if (methodName == @"models") 
     { 
      self.mod = cValue; 
     } 
     else if (methodName == @"subMod") 
     { 
      self.sub = cValue; 
     } 

     self.cacheValue = [NSDictionary dictionaryWithObjectsAndKeys: 
          man, @"Manu", 
          mod, @"Models", 
          sub, @"SubModels", nil]; 


     NSDictionary *plistDict = [NSDictionary dictionaryWithObjectsAndKeys: 
            signature, @"Signature", 
            version, @"Version", 
            request, @"Request", 
            dataVersion, @"Data Version", 
            cacheValue, @"Cache Value", nil]; 



     NSString *error = nil; 
     // create NSData from dictionary 
     NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error]; 

     // check is plistData exists 
     if(plistData) 
     { 
      // write plistData to our Data.plist file 
      [plistData writeToFile:plistPath atomically:YES]; 

      NSString *myString = [[NSString alloc] initWithData:plistData encoding:NSUTF8StringEncoding]; 
      //  NSLog(@"%@", myString); 
     } 
     else 
     { 
      NSLog(@"Error in saveData: %@", error); 
      //  [error release]; 
     } 
    } 


    @end 

내 질문에 두 부분으로 .. 그들은 응용 프로그램이 multitaksing 표시 줄에서 제거에도 PLIST에 유지되도록 내가 값을 저장할 수 있습니다. 작동하는 경우 변경하려면 무엇을 변경해야합니까?

+0

아마도 문제는 아니지만 사전에 writeToFile :을 호출하면 plist로 출력됩니다. –

+0

파일 이름'EngineProperties.plist'에 저장하고 있지만, 여러분의 의견은'Data.plist'를 저장하고 있다고 생각합니다. 어떤 파일 이름을로드하고 있습니까? 또한 파일을 저장할 때'plistPath'를 기록하십시오. 시뮬레이터에서 앱을 실행하는 경우 파인더의 경로로 이동하여 생성 된 파일을보고 XCode에서 열어 내용을 볼 수 있습니다. – Dondragmer

+0

opps 의견이 잘못되었습니다. 좀 더 기술적 인 이름으로 업데이트했습니다. 그러나 한 가지 질문은 파인더에서 경로가 어디에 있습니까? 전에 들어 본 적이 없습니까? –

답변

0

아마도 saveData 메소드가 호출되는 위치와 관련이 있습니다. 앱 위임을 확인하십시오. 기본적으로 스텁이 몇 개 있습니다. 아마도 applicationWillResignActive, applicationDidEnterBackground 또는 applicationWillTerminate을 원할 것입니다. 각각의 문서를 확인하십시오. 데이터를 기록하고자 할 때 원하는 문서가 무엇인지에 따라 다릅니다.

+0

맞아요. 그래서 당신이 그 appdelegate 메소드 중 하나가 호출 될 때이 메소드를 호출해야한다고 생각합니다.이 메소드를 호출 할 때까지는 (즉, 다른 인스턴스를 생성하지 않아도되는) 특별한 호출 방법이 필요하지 않습니다. –

+0

그 savedata 메소드는 plist에 새로운 값을 추가 할 때마다 호출됩니다. 그리고 테스트 할 때 모든 값이 저장되었는지 확인하고 앱이 멀티 태스킹에서 삭제되었을 때 여전히 제거되었습니다 ..... . –

관련 문제