Byron Coetsee Byron Coetsee - 1 year ago 122
Swift Question

Realm creation and population sequence

I'm trying to build up a Realm of a bunch of data. This wouldn't be a problem but I've hit a wall - a gap in experience shall we say.

Creating one record is fine. However, one of the fields of that record is an array (

) of records from another table. Now my 2 questions are:

  1. Does Realm support that? A list or array of Objects as one of the fields for a record... Answering no here will leed me on to an answer of my question - I will simply need to make an array of "primary keys" and query with those when I need to. If the answer is yes, proceed to question 2.

  2. How would I go about creating those lists, bearing in mind that those tables might be created at a fraction of a second later than the current one, meaning those records don't yet exist and therefore can't be added to the list...


class baseRLMObject: Object {
// Creates an id used as the primary key. Also includes a few methods.
class Film: baseRLMObject {
var name: String!
var episodeId: Int!
var characters = List<Character>()
class Character: baseRLMObject {
var name: String!
var films = List<Film>()

See how all the film objects need to be created first before the character objects? Otherwise I could try add a film which does not yet exist and then it all crashes and burns :( Reason I want to try find a better way is, I'm dealing with 10 tables, a few hundred records and variable connection speeds. It would be too long to wait for each data retrieval to finish before the next one starts. AND since they are all suffering from the same problem (inter-connections), regardless of which I start with, it won't work...

Thank you :)

Answer Source

As discussed, for the object that haven't been created, you should create an empty object with only the primary key, then re-fetch and add value after the other network request called

For Many-to-many relationship, you can use Realm's Inverse Relationships to create linking between these objects like:

class Character: baseRLMObject {
  var name: String!
  var films = LinkingObjects(fromType: Film.self, property: "characters")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download