jeudi 1 janvier 2015

Error while using Realm Object as a singleton. Is it a good idea?

I'm currently trying to use Realm in my app and I thought of using a subclass of RLMObject as a singleton to store the app state.


My first thought is if it's a very bad idea.


My AppState class has 2 main methods:



+ (instancetype)sharedInstance
{
static dispatch_once_t once;
static id sharedInstance = nil;

RLMResults *result = [[self class] allObjects];

if (result.count == 0) {
dispatch_once(&once, ^{
RLMRealm *realm = [RLMRealm defaultRealm];
sharedInstance = [[self alloc] init];
[realm beginWriteTransaction];
[realm addObject:sharedInstance];
[realm commitWriteTransaction];
});
}
else {
sharedInstance = [[[self class] allObjects] lastObject];
}

return sharedInstance;
}

- (void)update {
RLMRealm *realm = self.realm;

[realm beginWriteTransaction];
[realm addOrUpdateObject:[[self class] sharedInstance]];
[realm commitWriteTransaction];
}


I have properties for storing several params about the app state.


In one of my view controllers I get the following error:



'Attempting to modify object outside of a write transaction - call beginWriteTransaction on an RLMRealm instance first.'



The code snippet where the error occurs is as below:



AppState *defaultState = [AppState sharedInstance];
defaultState.appStateX = newAppStateValue; // This is where the app crashes.
[defaultState update];


I changed self.realm to [RLMRealm defaultRealm] in my -(void)update method. No dice!


I feel there is something fundamentally wrong with my understanding of Singletons and/or RLMObjects. Any help is appreciated.




Update


Based on Sakamoto's answer I modified my -(void)update method to accept a block as an argument. There is lot of room for error with this approach but I can go ahead and continue with my current implementation without making any drastic changes.



- (void)update:(void(^)(void))block {
RLMRealm *realm = self.realm;

[realm beginWriteTransaction];
block();
[realm commitWriteTransaction];
}


I update all the properties within the block.




Aucun commentaire:

Enregistrer un commentaire