I\'m writing some unit test code for a model class and want to simulate the behavior of the class during app exit and relaunch. I could achieve this by deleting and re-allocing
static MyModel *singleton = nil;
static dispatch_once_t onceToken;
+ (instancetype) sharedInstance
{
dispatch_once(&onceToken, ^ {
if (singleton==nil){
singleton = [[MyModel alloc] initSharedInstance];
}
});
return singleton;
}
+(void)setSharedInstance:(MyModel *)instance {
onceToken = 0;
singleton = instance;
}
Nil it:
[MyModel setSharedInstance:nil];
Note that you can also set it to an arbitrary class to mock it.
[MyModel setSharedInstance:someMock];