0

参考这篇文章链接 ,我使用相同的概念实现了一个类似的类别,即使用 NSMutableDictionary 来存储我需要的信息。但是在原始帖子中有一件事让我感到困惑

- (NSMutableDictionary *)addl_associatedDictionary
{
     NSMutableDictionary *res = nil;
     @synchronized(self) 
     {
        if (!(res = [self addl_associatedDictionaryPrimitive])) 
        {
           res = [self addl_generateAssociatedDictionary];
        }
     }
    return res;
}

我知道@synchronized 关键字是对多线程的保护。但是当我浏览其他示例时,大多数时候都没有使用保护。那么保护有必要吗?我也可以使用静态 dispatch_once_t 来替换 @synchronized 吗?下面是我在 .m 文件中的代码片段

@dynamic associatedDict;

-(void)setAssociateValue:(NSMutableDictionary*)dict
{
    objc_setAssociatedObject(self, @selector(associatedDict), dict,   OBJC_ASSOCIATION_RETAIN);
} 

-(id)getAssociateValue
{
    return objc_getAssociatedObject(self, @selector(associatedDict));
}

-(NSMutableDictionary*)associatedDict
{
    NSMutableDictionary* dict=[self getAssociateValue];
    if(!dict)
    {
       dict=[[NSMutableDictionary alloc]init];
       [self setAssociatedDict:dict];
    }
    return dict;
 } 


 -(void)setAssociateDict:(NSMutableDictionary *)associatedDict
{
    [self setAssociatedDict:associatedDict];
}

-(id)associate_getObjectForKey:(NSString*)key
{
    return self.associatedDict[key];
}

-(void)associate_setObject:(id)obj forKey:(NSString*)key
{
   [self.associatedDict setObject:obj forKey:key];
}
4

2 回答 2

2

向后退,不,您不能简单地dispatch_once_t用于此特定任务。正确使用dispatch_once_t需要一个全局变量,并且您的任务需要为每个对象实例完成一次 - 即您需要为每个实例提供一个唯一的全局变量......

你需要保护@synchronized吗?这是为了防止两个或多个线程都创建字典。如果在每个线程的第一次调用中没有它,当然取决于时间,每个都可能返回不同的字典。在随后的调用中,每个都将返回由最后一个线程创建的字典以分配给关联的变量,所有其他创建的字典都将丢失。

重要提示NSMutableDictionary本身不是线程安全的。如果您确实有多个线程读取和写入字典,那么您需要额外的同步/锁定以避免问题。有多种方法可以做到这一点,只需搜索并找到适合您需求的解决方案。当然,如果您没有多个线程,那么所有这些都是没有意义的,NSMutableDictionary可以并且一直安全地使用,而不是线程安全的。

高温高压

于 2015-07-02T19:26:54.590 回答
1

例如,它可能有助于回答对锁定成本的隐含担忧:我注意到您使用的是OBJC_ASSOCIATION_RETAIN而不是OBJC_ASSOCIATION_RETAIN_NONATOMIC. 考虑到您的情况,这似乎是多余的@synchronize,因为如果您拥有后者,那么您就可以放弃对前者的锁定。此刻,您要为同步支付两次费用。要么支付一次,要么根本不支付。

最好的整体解决方案可能是:

NSMutableDictionary *res; // no need to assign to `nil`; it's implied in ARC

// you're using an atomic property, so this is inherently safe
if (!(res = [self addl_associatedDictionaryPrimitive])) 
{
    // okay, doesn't exist, but two or more threads may get to
    // here simultaneously so we'll need to synchronise, and...
    @synchronized(self) 
    {
        // ... check again. As someone else may already have proceeded past the
        // outer if and created it while this thread was waiting to
        // enter the synchronised block. Assuming this dictionary
        // is created more rarely than it's accessed, this is no great issue
        if (!(res = [self addl_associatedDictionaryPrimitive])) 
        {
           res = [self addl_generateAssociatedDictionary];
        }
    }
}
return res;

...并坚持使用OBJC_ASSOCIATION_RETAIN. 还要注意 CRD 的观点:可变字典本身不是线程安全的。因此,如果您确实需要线程安全,那么这并不能完全解决问题。如果您不需要线程安全,则切换到OBJC_ASSOCIATION_RETAIN_NONATOMIC并转储@synchronized.

于 2015-07-02T19:54:55.413 回答