After trying the various options I came to the conclusion that using singleton as observer on NSNotificationCenter event is not thread safe. After moving observer logic from singleton to normal controller everything works just fine. I have tried various implementations of singleton that should be thread safe with ARC but none of them worked as an observer.
Tested singleton implementations:
1) My implementation of singleton
+(RefreshService *)getDefaultRefreshService {
if (DefaultRefreshService == nil) DefaultRefreshService = [[self allocWithZone:nil] init];
return DefaultRefreshService;
}
2) implementation from here How do I implement an Objective-C singleton that is compatible with ARC?
+ (RefreshService *)getDefaultRefreshService
{
static RefreshService *DefaultRefreshService = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
DefaultRefreshService = [[self alloc] init];
});
return DefaultRefreshService;
}
3) and implementation from here: What should my Objective-C singleton look like?
+ (void)initialize
{
static BOOL initialized = NO;
if(!initialized)
{
initialized = YES;
DefaultRefreshService = [[RefreshService alloc] init];
}
}
For every three options I have used this init() method:
-(id)init {
self = [super init];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:NSSelectorFromString(@"refresh:") name:@"refresh_event" object:nil];
}
return self;
}