How to retrieve an object that is in a list in another object using realm swift?

eeschimosu

I have some Realm classes that look like this:

class Friends: Object {
    dynamic var name = true
    dynamic var role = true
    dynamic var type = true
    dynamic var owner: Profile?
}

class Profile: Object {
    dynamic var uuid = NSUUID().UUIDString
    dynamic var name = ""
    dynamic var date = NSDate(timeIntervalSinceNow: 1)
    dynamic var section = 0
    dynamic var code = ""
    dynamic var gender = 0
    dynamic var type = ""
    let friends = List<Friends>()

    override static func primaryKey() -> String? {
       return "uuid"
    }
}

class Sub: Profile {
    dynamic var owner: Master?
}

class Master: Object {
    dynamic var type = ""
    dynamic var name = ""
    dynamic var date = ""
    let subs = List<Sub>()
}

I understand that to retrieve the objects from realm I have to do this:

var master = try! Realm().objects(Master)
let profile = master[indexPath.row]
let date = profile.date
let name = profile.name
let type = profile.type

The question is: How do I retrieve objects from the 'subs'(List) ?

joern

When you retrieve a master object you can access its subs list like any other property:

let subs = profile.subs

This gives you a list that you can iterate over:

for sub in profile.subs {
    // do something with the sub object
}

Or you can filter the subs to find a particular object:

if let subjectWithId = profile.subs.filter("uuid == '7382a8d83'").first {
    // do something with the subject
}

Or you can use subscripting to access elements by index:

let secondProfile = profile.subs[1]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to delete object in realm in Swift

how to update an object in realm swift

Realm Swift - save a reference to another object

Storing an array of Realm objects inside another Realm object (Swift)

How to convert list of object to another list object using streams?

Realm Swift - Filtering results with List<object> with List<object>

Realm Swift - Object not updating when using modal

How to model a Swift dictionary property on a Realm object?

How to filter object from Realm database in Swift

How to retrieve nested list from object using Stream API?

Transform an object to a list[object] in Realm as migration ( How can i replace a list[objects] with object field in Realm? )

How to prevent delete of an object referenced from another object in Realm?

Querying for an object in a list in Realm

Store nested model object with list in Realm swift4

Delete specific object from LinkingObjects list - Realm Swift

Realm , Object is already managed by another Realm

how to retrieve the name and value in object list

How to retrieve the reference of an object stored in a list?

How to get object_list of another user using ListView in Django?

How to obtain a list of objects inside another object using reflection

Realm Object returning nil (Swift)

Get associated object value from filter query using Realm swift

How to assign an object to another object using Scanner

Append existing object to Realm List

query Object from Realm List

Updating a List of Object from another list of object using java stream

How to retrieve Django model object using ForeignKey?

How to retrieve/import object using MEF in Prism

How to retrieve a multidimensional array using json object?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive