I really like my controllers db independent as per clean code architecture. This approach worked great in my all big projects where I have used CoreData. For my new project I am using Realm and following same architecture principles. Here is my code snippet:
dispatch_async(self.realmQueryQueue, ^{
RLMRealm *realm = [RLMRealm defaultRealm];
MyRealmObject *event = [MyRealmObject objectInRealm:realm forPrimaryKey:eventID];
MyNormalObject *displayData = [[MyNormalObject alloc] init];
[displayData updateForRealmData:event];
dispatch_async(dispatch_get_main_queue(), ^{
block(displayData);
});
});
So in updateForRealmData method I have converted RLMObject properties back to NSObject properties. But there is no way to convert RLMArray to NSArray. I really wanna figure out possible options to make my controller independent.
Right now if use @property (nonatomic) RLMArray
You can convert RLMArray to NSArray by iterating over the RLMArray and appending each object to a NSArray. Not sure if this is the best way to do it, but it works.
@sandalsoft Thank you for the suggestion. @jpsim @segiddins Do we have any recommended and best approach ?
@tariq235 if you really want to convert your RLMArray to an NSArray, you can do [rlmArray valueForKey:@"self"].
If the intent is to make a stand-alone copy of an RLMArray, then KVC alone won't be enough, since those RLMObjects still won't be standalone. You'd have to loop through and create new RLMObjects from the persisted values.
But I'd strongly encourage you to consider if it's worthwhile to materialize all these RLMObjects (and potentially large graph of relationships) into memory, while getting a copy of your objects that can then fall out of sync with the persisted representation.
Yeah [rlmArray valueForKey:@"self"] didn't worked because those are still not standalone objects.
My Architecture is:
I sync my realmDB and at business logic layer I convert RLMObjects to NSObject for the presentation and for any update or changes I just simply make use of API calls and call sync again. So this way I never fall out of sync.
For my original question - I think I have to iterate RLMArray and append each object in NSArray,
For my original question - I think I have to iterate RLMArray and append each object in NSArray,
No, that also wouldn't create standalone objects. See my last comment as to why.
Got it - So you're saying I have to create new RLMObjects using persisted values.
You can use -[RLMObject initWithValue:] to create standalone copies of persisted objects.
Thanks @segiddins @jpsim I'd to do some workaround because my main RLMObject was linked to another RLMObject i.e.
Comment : RLMObject
@property - User (User is another RLMObject)
I am not sure this is a correct way but its giving me standalone object:
NSMutableArray *array = [[NSMutableArray alloc] init];
for (RLMObject *object in comments) {
Comment *comment = [[Comment alloc] initWithValue:object];
User *user = [[User alloc] initWithValue:comment.user];
comment.user = nil;
comment.user = user;
[array addObject:comment];
}
Now this array contains all standalone objects.
how to save array of strings in realm with no keys in array ..
Most helpful comment
@tariq235 if you really want to convert your
RLMArrayto anNSArray, you can do[rlmArray valueForKey:@"self"].