0
votes

Let's assume i have a database with following structure:

@interface User: RLMObject
  RLMArray<Match>* matches;
@end

@interface Match: RLMObject
  RLMArray<Round>* rounds;
  RLMArray<User>* players;
@end

@interface Round: RLMObject
  User* nextMoveUser;
@end

And i would like to update existing User object, that is already managed in Realm with all nested properties:

User* user = ... // Get unmanaged User, 
                 // parsed from API 
                 // with unmanaged parsed nested mathces and round

RLMRealm* realm = [RLMRealm defaultRealm];
[realm beginWriteTransaction];
[realm addOrUpdateObject: user];
[realm commitWriteTransaction];

The question is: would Realm update all nested objects as well as the main one User? P.S.: I am asking this, because I'm facing some troubles with updating [Round nextMoveUser]. It becomes nil after update. Though the debugger show that is everything allright with parsed entity.

Also i would uppreciate if some one could explain, how things work internally, when it comes to such types of references.

1

1 Answers

1
votes

You need to be careful when updating an RLMObject by using an unmanaged copy of itself. Since nil is a valid value, if the copy doesn't contain all of the same information as the original, those original values will be deleted.

This will apply to the RLMArray objects as well, since an empty array also counts as a valid value; you'll need to ensure that your arrays and contents are valid too.

One easy way to make this easier to deal with is to use an NSDictionary instead of an unmanaged copy to update the Realm object. This way, you are able to explicitly dictate which properties are updated, and the which ones are left alone.

I'm not sure exactly how to answer your question on how things work internally. But suffice it to say, child objects aren't directly owned by their parent objects. More, both objects exist and operate independently of each other, but the parent maintains a linked-list relationship pointing to the child. Even if that relationship is deleted, the object will still remain in the database.