共享NSDateFormatter - 最佳实践? [英] Shared NSDateFormatter - Best Practices?

查看:106
本文介绍了共享NSDateFormatter - 最佳实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的团队发现我们在我们的代码库中使用了各种 NSDateFormatter 对象,并开始研究如何避免分配/初始化常见的成本/混乱

My team found that we were using a variety of NSDateFormatter objects throughout our code base, and started looking into how we could avoid the cost/confusion of allocating/initializing common formatters in a bunch of separate places.

一个想法是在 NSDateFormatter 类上创建一个类别将提供对通常配置的格式化器的静态实例的引用。例如,我们在几个地方使用短时间日期格式化程序,并且希望添加以下类方法:

One idea we had was to create a category on the NSDateFormatter class that would provide a reference to a static instance of a commonly configured formatter. For example, we were using the "short time" date formatter in several places, and were looking to add the following class method:

@implementation NSDateFormatter (NSDateFormatter_PDDateFormatters)

static NSDateFormatter * shortTimeFormatter = nil;

+ (NSDateFormatter *) PDSharedShortTimeFormatter {

    @synchronized([NSDateFormatter class]){

        if( shortTimeFormatter == nil){

           // Create new formatter for SHORT times (e.g. 12:00 pm)

           shortTimeFormatter = [[NSDateFormatter alloc] init];
           [shortTimeFormatter setDateStyle: NSDateFormatterNoStyle];
           [shortTimeFormatter setTimeStyle:NSDateFormatterShortStyle];
       }

      return shortTimeFormatter;

   }

  return nil;
}

@end

这种方法是我们目前不保护 NSDateFormatter 被更改。由于格式化程序在我们的应用程序中基本上是共享的,如果另一个对象要更改格式化程序的配置(例如时间/日期样式),这可能会导致问题。

One of the issues I have with this approach is that we are not currently "protecting" the NSDateFormatter from being changed. Since the formatter is essentially "shared" throughout our application, this could potentially cause problems if another object was to change the formatter's configuration (e.g. time/date style).

因为我们在内部使用,所以我不会过度关注我们的团队滥用这项功能的风险(例如,它是一个小团队,并且清楚地评论过)。

Because we are using this internally, I'm not overly concerned with the risk of our team misusing this functionality (i.e. it's a small team, and clearly commented).

但是,我想知道这里的最佳做法。

However, I was wondering about best practices here.

有一种方法可以返回一个不可变的引用日期格式化程序?如果我返回一个格式化程序的副本,是否比我们现在做的alloc / inits更便宜?

Is there a way to return an immutable reference to the date formatter? If I return a copy of a formatter, is that any less expensive than doing the alloc/inits that we're doing now?

这里有一些其他的方法?

Is there some other approach to take here?

我们会开始运行这个,但是总是好的,在写更好代码时得到一些反馈。

We'll be up and running with this, but it's always good to get some feedback in writing "better" code.

推荐答案

通常,你不用担心。 Obj-C会让你搞砸几乎任何东西的多汁内部。即使 @private 也不能保护 -valueForKey:_thatFunPrivateIvar

Normally, you would just not worry about it. Obj-C will let you fiddle with the juicy insides of almost anything. Even @private doesn't protect against -valueForKey:_thatFunPrivateIvar. And if all else fails, you can just invoke runtime functions.

但是,这里最简单的解决方法是公开一个内部使用缓存格式化程序的API,但不提供访问它正在使用的格式化程序。然后,您的代码将使用 + [Formatter shortTimeStringFromDate:] 来执行您的示例代码。格式化程序可以被延迟分配,你可以使用可清除的内存,因此缓存的格式化程序可以在内存压力下以LRU方式清除。

However, the simplest workaround here would be to expose an API that internally uses cached formatters, but that provides no access to the formatters it is using. Your code would then use +[Formatter shortTimeStringFromDate:] to do what your sample code is doing now. The formatter in question could be lazily allocated, and you could use purgeable memory so cached formatters could be cleared in a LRU fashion under memory pressure.

这篇关于共享NSDateFormatter - 最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆