【发布时间】:2019-04-26 15:43:32
【问题描述】:
我有一个具有强属性SomeClass 的类实例,它只被这个单个实例引用。在某些时候,这个强属性会被一个新创建的实例覆盖,该实例分配给这个强属性。
@implementation SomeClass
- (instancetype)init;
{
static NSInteger idx = 0;
NSLog(@"I am %", idx++);
self = [super init];
return self;
}
- (oneway void)dealloc;
{
NSLog("bye");
}
@end
那么,在类中持有引用:
@property (nonatomic, strong) SomeClass *prop;
...然后
self.prop = [[SomeClass alloc] init]; /// first time assigned
...然后
self.prop = [[SomeClass alloc] init]; /// second time assigned
通常,顺序是:
- 新实例被分配(调用它的自定义 -init*)
- OLD 实例被释放(调用其自定义 -dealloc)
输出如下:
I am 0
I am 1
bye
但是,有没有可能顺序颠倒?
有没有可能得到这样的输出?:
I am 0
bye
I am 1
例如,如果SomeClass -init方法执行了一些CPU繁重的任务,但仍然在同一个/主线程中?
【问题讨论】:
标签: objective-c automatic-ref-counting