2012-04-26 23 views
1

カテゴリを扱う必要があるアプリケーションを作成しています。 アプリで配信したい基本カテゴリが設定されますが、ユーザーが編集(カテゴリの削除、追加)することができます。基本的な.plistは変更されずに一度だけ読み込まれ、次に別の場所に変更可能に保存されます。アプリでcategoryCollection.plistで配信.plistの代替文字列での可変文字列のコレクション?

デフォルトカテゴリー:

は、ここに私のアプローチです。

NSString *mainBundlePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
    NSString *dictPath = [mainBundlePath stringByAppendingPathComponent:@"defaultCategories"]; 
    NSDictionary * dict = [[NSDictionary alloc]initWithContentsOfFile:dictPath]; 

    // testing if the file has values, if not load from categoryCollection.plist 
    if (dict.count == 0) { 
     NSString *tempPath = [[NSBundle mainBundle] pathForResource:@"categoryCollection" ofType:@"plist"]; 
     dict = [NSMutableDictionary dictionaryWithContentsOfFile:tempPath]; 
     [dict writeToFile:dictPath atomically:YES]; 
    } 

    // load into NSMutableSet 
    [self.stringsCollection addObjectsFromArray:[[dict objectForKey:@"categories"]objectForKey:@"default"]]; 

私は、この関数を呼び出すカテゴリを追加する:

defaultCategories.plistは私が使用 NSMutableSetにカテゴリを読み取るために

​​categoryCollection.plist

を操作します新しいの.plistファイルになります

-(void)addCategoryWithName:(NSString *)name{ 

    NSString *mainBundlePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
    NSString *dictPath = [mainBundlePath stringByAppendingPathComponent:@"defaultCategories"]; 
    NSMutableDictionary * dict = [[NSMutableDictionary alloc]initWithContentsOfFile:dictPath]; 
    [[[dict objectForKey:@"categories"]objectForKey:@"default"]addObject:name]; 

    [dict writeToFile:dictPath atomically:YES]; 

    self.needsToUpdateCategoryCollection = YES; 
} 

文字列を削除するには:

-(void)removeCategoryWithName:(NSString *)name{ 

    NSString *mainBundlePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
    NSString *dictPath = [mainBundlePath stringByAppendingPathComponent:@"defaultCategories"]; 
    NSDictionary * dict = [[NSDictionary alloc]initWithContentsOfFile:dictPath]; 

    NSMutableArray *temp = [NSMutableArray arrayWithArray:[[dict objectForKey:@"categories"]objectForKey:@"default"]] ; 

    for (NSString *string in temp) { 
     if ([string isEqualToString:name]) { 
      [temp removeObject:string]; 
      break; 
     } 
    } 
    [[dict objectForKey:@"categories"]removeObjectForKey:@"default"]; 
    [[dict objectForKey:@"categories"] setValue:temp forKey:@"default"]; 


    [dict writeToFile:dictPath atomically:YES]; 
    self.needsToUpdateCategoryCollection = YES; 
} 

コードは実際には非常にうまく機能しますが、この大規模な複数のI/O操作のオーバーヘッド、テストなどが本当に必要であるか、よりエレガントな解決策がある場合は文字列のコレクションを格納し、それらを聞かせする場合、私は疑問に思います操作される?

たり、速度を向上させる可能性のあるspeedbump(カテゴリの多くを持つとき、私はそのコードといくつかの小さなラグを得るため)

任意の考えを参照してください場合は? sebastian

答えて

1

あなたのアプローチはうまくいきますが、コレクションが変更されるたびに結果を書き込むことよりも多くの作業が必要です。 addremoveメソッドの中で最もコストがかかるファイルの書き込みです。問題は、あなたのアプリが起動したとき、あなたのカテゴリを読んシングルトンオブジェクトであなたのNSMutableDictionaryをキャッシュして、アプリの実行としてそれを参照するようになります対処する

一つの方法。エンドユーザーが変更を加えると、メモリ内でのみ辞書が更新されます。すぐにデータをファイルに書き込まないでください。

アプリデリゲートでapplicationDidEnterBackground:イベントを聞き、そのハンドラのファイルにデータを書き込みます。また、applicationDidEnterForeground:イベントに耳を傾け、NSMutableDictionaryにファイルを読んでください。これにより、CPUサイクルが大幅に節約され、よりスムーズなエンドユーザーエクスペリエンスが提供されます。

+0

はうまく動作します。ありがとう:) –

関連する問題