2012-04-10 3 views
0

私はあなたが読み書きのためのルートディレクトリに置く新しいplistオブジェクトに私のバンドルから読んだplistを持っています。私の質問は、これで何をやっているのか、アプリケーションを終了するときにどのような線量を行うのですか?そして、アプリケーションがiosの「マルチタスク」メニューから殺されたときにどうなるかが問題です。あなたの値を失うことはありませんのでplistを維持する方法

また、このplistをメモリ/バンドルに保存して、アプリケーションを再度使用するときに使用するための方法です。

私のコードは次のとおりです。ここで

が私の.h

#import <Foundation/Foundation.h> 

    @interface EngineProperties : NSObject { 

    NSString *signature; 
    NSNumber *version; 
    NSNumber *request; 
    NSNumber *dataVersion; 
    NSMutableDictionary *cacheValue; 
    //cachevalue Items 
    NSNumber *man; 
    NSNumber *mod; 
    NSNumber *sub; 

    } 

    @property (copy, nonatomic) NSString *signature; 
    @property (copy, nonatomic) NSNumber *version; 
    @property (copy, nonatomic) NSNumber *request; 
    @property (copy, nonatomic) NSNumber *dataVersion; 
    @property (copy, nonatomic) NSMutableDictionary *cacheValue; 
    //cachevalue Items 
    @property (copy, nonatomic) NSNumber *man; 
    @property (copy, nonatomic) NSNumber *mod; 
    @property (copy, nonatomic) NSNumber *sub; 



    //Singletton 
+ (id)sharedManager; 
    - (void) saveData:(NSString *)methodName signature:(NSString *)pSignature Version:(NSNumber *)pVersion request:(NSNumber *)rNumber dataVersion:(NSNumber *)dvReturned cacheValue:(NSNumber *)cValue; 

    @end 

。ここでは私の.m

#import "EngineProperties.h" 

static EnginePropertiesController *sharedMyManager = nil; 

    @implementation EngineProperties 

    @synthesize signature; 
    @synthesize version; 
    @synthesize request; 
    @synthesize dataVersion; 
    @synthesize cacheValue; 

    @synthesize man; 
    @synthesize mod; 
    @synthesize sub; 



    #pragma mark Singleton Methods 
+ (id)sharedManager { 
    @synchronized(self) { 
     if (sharedMyManager == nil) 
      sharedMyManager = [[self alloc] init]; 
    } 
    return sharedMyManager; 
} 
- (id)init { 
    if (self = [super init]) { 

     // Data.plist code 
     // 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"]; 

     // check to see if Data.plist exists in documents 
     if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) 
     { 
      // if not in documents, get property list from main bundle 
      plistPath = [[NSBundle mainBundle] pathForResource:@"EngineProperties" ofType:@"plist"]; 
     } 

     // read property list into memory as an NSData object 
     NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath]; 
     NSString *errorDesc = nil; 
     NSPropertyListFormat format; 
     // convert static property liost into dictionary object 
     NSDictionary *tempRoot = (NSMutableDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc]; 
     if (!tempRoot) 
     { 
      NSLog(@"Error reading plist: %@, format: %d", errorDesc, format); 
     } 
     // assign values 
     self.signature = [tempRoot objectForKey:@"Signature"]; 
     self.version = [tempRoot objectForKey:@"Version"]; 
     self.request = [tempRoot objectForKey:@"Request"]; 
     self.dataVersion = [tempRoot objectForKey:@"Data Version"]; 

     man = [cacheValue objectForKey:@"Man"]; 
     mod = [cacheValue objectForKey:@"Mod"]; 
     sub = [cacheValue objectForKey:@"SubMod"]; 

     cacheValue = [tempRoot objectForKey:@"Cache Value"]; 
    } 


    - (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 == @"manufacturers") 
     { 
      self.man = cValue; 
     } 
     else if (methodName == @"models") 
     { 
      self.mod = cValue; 
     } 
     else if (methodName == @"subMod") 
     { 
      self.sub = cValue; 
     } 

     self.cacheValue = [NSDictionary dictionaryWithObjectsAndKeys: 
          man, @"Manufacturers", 
          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 

答えて

0

であることは、正直、あなたが求めてとのplistの値を変更することがあります頻度によって異なります。例えば、私のアプリは一度だけそれを取得し、数回(それほど多くはない)書き込む必要があったので、私の節約コードはすべて、この特定のメソッドの最後にあった。

ただし、plistがライブ(一定値の変更)の場合、AppDelegateからアクセス可能な保存するデータへの参照を保持することをお勧めします。そうすれば、beginBackgroundTaskWithExpirationHandler: on-applicationDidResignActiveと呼んでplistデータを保存することができます。

(ただし、ユーザーが完全に保存する前にアプリを強制終了するほど高速であれば(大きなif)、plistの完全性についての保証はありません)。

+0

権利を。私はあなたの言っていることとそれを助けてくれます。しかし、私が持っているもう一つの懸念は、データを実際に保存していることです。SDKでアプリケーションを閉じてバンドルをチェックしたときに、私のplistは空でした。私はどのくらいの量のplistが最後になるのかwounderingですか? (いつ削除されますか?)つまり、ユーザーがそれを閉じると、マルチタスクインスタンスが削除されるか、または自分の電話のアプリが削除されますか?あなたの答えはbtwありがとう。 –

+0

-beginBackgroundTaskWithExpirationHandler:を使用している限り、オブジェクトは詰まる必要があります。あなたのplistが空の場合、それはエンコーディングであろうとオブジェクト処理であろうと、何か正しいことをしていません。 – CodaFi

+0

よく私のplistは空です。私のplistのテンプレートはバンドルに置かれています(書き込みをしないで読むだけです)。私は私のドキュメントのルートにコピーします。私はplistと何をするべきかわかりません。私のルートドキュメントフォルダに作成された..またはどれくらいの期間続くか...私はあなたがちょうどリストアップしたその方法を読み上げるつもりです。 –

0

コードのリンクもそのリンクの吹き抜けリンクに従ってください。 .plistファイルへの書き込み/読み取り方法。

Plist

+0

うん、私はかなりこの男が話していることを行っている。しかし、マルチタスキングメニューからアプリを削除した場合、私の価値を維持する方法にもっと興味がありますか? > –

+0

マルチタスクメニューから削除されたplist値は削除できません。シミュレータ/デバイスからappを削除すると、値を削除することができなくなるまでplistから値を削除することができます。 – Thukaram

0

あなたは、迅速かつ容易にNSUserDefaults

にプレースメントリストを保存することができ、この簡単なチュートリアルチェックアウト:

http://www.cocoadev.com/index.pl?NSUserDefaults

+0

大丈夫ですよ、お元気ですか: –

+0

awesome私に知らせてください – pdesantis

+1

大丈夫ですので、私はこれを調べています。 plistをNSUserDefaultsに保存しますか?plistの構造のような値をNSUserDefaultsに保存しますか? –

関連する問題