iphone - .plist 中的可变字符串集合,替代方案?

标签 iphone objective-c ios algorithm plist

我正在创建一个需要处理类别的应用程序。 将有一个基本类别集,我想与该应用程序一起提供,但它可以由用户编辑(删除、添加类别)。基本的 .plist 不会被更改,只会被读取一次,然后可变地存储在其他地方。

这是我的方法:

默认类别在 categoryCollection.plist 中随应用一起提供。 defaultCategories.plist 将是被操作的新 .plist 文件

Default Dictionary style categoryCollection.plist

将类别读入 NSMutableSet 我使用:

    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"]];

添加一个类别我调用这个函数:

-(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 操作、测试等的巨大开销,或者是否有更优雅的解决方案来存储字符串集合并让它们被操作?

或者如果您看到任何可能提高速度的减速带(因为当我有很多类别时,我的代码会有一些小的滞后)

有什么想法吗? 塞巴斯蒂安

最佳答案

您的方法工作正常,但它所做的工作比每次修改集合时写入结果所必需的要多。写入文件是您的 addremove 方法中成本最高的部分。

解决该问题的一种方法是在您的应用程序启动时读取您的类别,将您的 NSMutableDictionary 缓存在 singleton 对象中,并在您的应用程序运行时引用它.当最终用户进行更改时,您只在内存中更新字典;不要立即将数据写入文件。

在您的应用委托(delegate)中,监听 applicationDidEnterBackground: 事件,并将数据写入该处理程序中的文件。同时监听 applicationDidEnterForeground: 事件,并将您的文件读入 NSMutableDictionary。这将为您节省大量 CPU 周期,并提供更流畅的最终用户体验。

关于iphone - .plist 中的可变字符串集合,替代方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10335384/

相关文章:

ios - 将 UITableViewCell 设置为在代码中选中

iphone - 如何在 iPhone 上请求设备 token

ios - 区分单击和双击 iOS 中的主页按钮

iphone - iOS 设备因未知原因崩溃

ios - CPTScatterPlotInterpolationCurved 类型无法正常工作

iphone - Objc - Objective-C 语言中是否还需要 @private 指令?

ios - 访问另一个 View 的元素(iOS - 标签栏 Controller )

objective-c - 开源 html 解析类无法正确解析段落之间的空格

objective-c - 以编程方式 LZMA 解压缩静态 LZMA 压缩文件

ios - 使用 id3taggenerator 和 mediafilesegmenter 将定时元数据插入 HLS(HTTP 直播流)