Properties in Realm results are nil - swift

I don't understand why I am failing to print out the properties of my realm result. when I print existingCart[i].${property}, it always give me a default value for all iteration i.
the statement print(existingCart) has no problem. It contains non default values in the properties. It returns:
Results<Product> <0x7fe5d8f3a770> (
[0] Product {
itemImage = <ffd8ffe0 00104a46 49460001 01000048 00480000 ffe10058 — 232200 total bytes>;
itemName = Ground Beef;
price = 6.53;
unit = 450 g;
quantity = 1;
isInCart = 0;
},
[1] Product {
itemImage = <ffd8ffe0 00104a46 49460001 01000048 00480000 ffe1004c — 153015 total bytes>;
itemName = Chicken Drumsticks;
price = 9.06;
unit = 1.25 kg;
quantity = 1;
isInCart = 0;
},
[2] Product {
itemImage = <ffd8ffe0 00104a46 49460001 01000048 00480000 ffe10058 — 242980 total bytes>;
itemName = Ground Turkey;
price = 6.91;
unit = 450 g;
quantity = 2;
isInCart = 0;
},
[3] Product {
itemImage = <ffd8ffe0 00104a46 49460001 01000048 00480000 ffe10058 — 224128 total bytes>;
itemName = Canned Beans;
price = 1.79;
unit = 398 mL;
quantity = 1;
isInCart = 0;
},
[4] Product {
itemImage = <ffd8ffe0 00104a46 49460001 01000048 00480000 ffe10058 — 252231 total bytes>;
itemName = Frosted Flakes;
price = 9.49;
unit = 1.06 kg;
quantity = 1;
isInCart = 0;
},
[5] Product {
itemImage = <ffd8ffe0 00104a46 49460001 01000048 00480000 ffe1004c — 165948 total bytes>;
itemName = Gouda Cheese;
price = 4.99;
unit = 300 g;
quantity = 1;
isInCart = 0;
}
)
This is how I try to load the data from Realm:
private func loadExistingCart() {
let realm = try! Realm()
let existingCart = realm.objects(Product.self)
print(existingCart) // see above
for i in 0..<existingCart.count {
print(existingCart[i].itemName) // empty string
print(existingCart[i].price) // 0.0
print(existingCart[i].quantity) // 0
print(existingCart[i].unit) // empty string
cart.addItem(existingCart[i]) // adds
}
}
Here is the product class:
import UIKit
import Foundation
import Realm
import RealmSwift
class Product : Object {
// MARK: Properties
#objc var itemImage = Data()
#objc var itemName: String = ""
#objc var price: Float = 0.0
#objc var unit: String = ""
#objc var quantity: Int = 0
#objc var isInCart: Bool = false
convenience init?(itemImage: UIImage?, itemName: String, price: Float, unit: String, quantity: Int, isInCart: Bool = false) {
self.init()
// itemName, unit, category should not be empty
guard (!itemName.isEmpty && !unit.isEmpty) else {
return nil
}
// price should not be a negative number
guard (price >= 0) else {
return nil
}
// Initialize stored properties
self.itemImage = UIImageJPEGRepresentation(itemImage!, 0.9)!
self.itemName = itemName
self.price = price
self.unit = unit
self.quantity = quantity
self.isInCart = isInCart
}
override class func primaryKey() -> String? {
return "itemName"
}
}
This is how products are being saved
private func saveToCart(product: Product, update: Bool) {
let realm = try! Realm()
print(product)
try! realm.write {
realm.add(product, update: true)
}
}

You've made a tiny error in the declaration of the Product class there, and it's just a common gotcha with using Realm.
If you look back over all the Realm examples in their documentation, you'll see that each member field should be declared such as this corrected one:
#objc dynamic var itemImage = Data()
Yep, you've missed out the dynamic modifier on each of your properties. Go back and add that to each property and it should just start working. For completeness, this should be the properties declaration:_
// MARK: Properties
#objc dynamic var itemImage = Data()
#objc dynamic var itemName: String = ""
#objc dynamic var price: Float = 0.0
#objc dynamic var unit: String = ""
#objc dynamic var quantity: Int = 0
#objc dynamic var isInCart: Bool = false
You can google the dynamic keyword for an explanation. It's based on the fact that the object you have is just a proxy to a database object, and Realm needs to intercept the property access to actually interrogate the database and retrieve the value of the property. You can see the same effect as your problem if you ever try to look at an object in the debugger - it's just full of the default values.

I'm not familiar enough with how Realm works to say why you can't seem to access the objects by index, but perhaps you could try accessing them as part of a Sequence instead?
let existingCart = realm.objects(Product.self)
for item in existingCart {
print(item.itemName)
print(item.price)
print(item.quantity)
print(item.unit)
}

Related

firebase firestore not accepting my write function from swift encoded json

this is my struct
struct CustomerDetail: Codable {
var customerID: String = ""
var firstname: String = ""
var lastname: String = ""
var age: Int = 0
var birthday: Int = 0
var country: String = ""
var pound: Bool = true
var feetboolean: Bool = true
var currentweight: Int = 0
var desiredweight: Int = 0
var sex: Bool = false
var feet: Int = 0
var inches: Int = 0
var cm: Float = 0.0
}
this is my write function
struct writetofirebase {
var customerdetails: CustomerDetail = CustomerDetail()
let db = Firestore.firestore()
var delegate: writefirebase?
func write(){
print(customerdetails)
do {
if let data = try? JSONEncoder().encode(customerdetails){
try db.collection("collection name").document("document name").setData(from: data)
delegate?.didSuccessfulWrite(true)
}else{
print("failed to encode")
}
} catch let error {
delegate?.didFailWithError(error)
}
}
}
this is my error
https://i.stack.imgur.com/cOpQK.png
this is my firebase structured data
https://i.stack.imgur.com/cOpQK.png
The error is saying that the data it is receiving inside the setData method is not compatible and therefore malformed for the request. You should be updating or setting the data appropriately by defining a custom dictionary or passing the object directly.
db.collection("collection name").document("document name").setData(data, merge: true)
Source: https://firebase.google.com/docs/firestore/manage-data/add-data#set_a_document

Filter Realm Results array returns elements that should be filtered

Any ideas why this filter is not working correctly ?
for item in activeItems {
print("item.product: \(item.product), \(item.spaceRequired)")
}
returns
item.product: nil, 40.0
Filtering where product is nil
let f1 = activeItems.filter{$0.product != nil}
print("f1: \(f1)")
print("f1.count: \(f1.count)")
returns a count of ZERO but the array still appears to contain an item
f1: LazyFilterSequence<Results<AssortmentItem>>(_base: Results<AssortmentItem> <0x109ce2c90> (
[0] AssortmentItem {
...
f1.count: 0
And then filtering and mapping just spaceRequired
let f11 = f1.filter{$0.product!.isProduct == true}.map({$0.spaceRequired})
print("f11: \(f11)")
returns the same array with a single item
f11: LazyMapSequence<LazyFilterSequence<Results<AssortmentItem>>, Double>(_base: Swift.LazyFilterSequence<RealmSwift.Results<Merchandise_Manager.AssortmentItem>>(_base: Results<AssortmentItem> <0x109ce2c90> (
[0] AssortmentItem {
And then trying to reduce crashes
let w = f11.reduce(0,+)
This seems to fix the problem
let width = Array(activeItems.filter{$0.product != nil}).filter{$0.product!.isProduct == true}.map({$0.spaceRequired}).reduce(0,+)
Is this a bug in Swift 5 or in Realm ?
EDIT: It looks like this is a bug in Realm's handling of things.
To be a bit cleared below is a more complete set of the Realm objects.
import Foundation
import RealmSwift
let activeDate: NSDate = Date() as NSDate
let defaultWidth: Double = 40.0
class MyObject: Object {
#objc dynamic var number: Int = 0
#objc dynamic var name: String?
let items = List<ChildObject>()
}
extension MyObject {
var activeItems: Results<ChildObject> {
let activeDate = activeDate // Some globally defined value
let active = items.filter("startDate <= %# && (endDate >= %# || endDate == nil)", activeDate, activeDate).sorted(byKeyPath: "number")
return active
}
/// Works Correctly
var totalWidth: Double {
let width = Array(activeItems.filter{$0.product != nil}).filter{$0.product!.isProduct == true}.map({$0.spaceRequired}).reduce(0,+)
let width2 = Array(activeItems.filter{$0.product == nil}.map({$0.spaceRequired})).reduce(0,+)
return width+width2
}
/// Crashes
var totalWidth: Double {
let width = activeItems.filter{$0.product != nil}.filter{$0.product!.isProduct == true}.map({$0.spaceRequired}).reduce(0,+)
let width2 = activeItems.filter{$0.product == nil}.map({$0.spaceRequired}).reduce(0,+)
return width+width2
}
}
class ChildObject: Object {
#objc dynamic var parent: MyObject?
#objc dynamic var number: Int = 0
#objc dynamic var product: Product?
#objc dynamic var name: String?
#objc dynamic var spaceRequired: Double = 40.0
#objc dynamic var startDate: NSDate?
#objc dynamic var endDate: NSDate?
}
extension ChildObject {
var spaceRequired: Double {
if let p = product {
return p.width
} else {
return defaultWidth
}
}
}
class Product: Object {
#objc dynamic var isProduct: Bool = false
#objc dynamic var width: Double = 30.0
}
There's a couple of issues at work here but the main problem is that Realm Results are live updating; while you can filter data using the Swifty
let f1 = activeItems.filter{$0.product != nil}
it's going to give intermittent results since Realm doesn't know which items are filtered or not as .filter { is not a Realm function and Realm won't know what to update within the results.
You should generally use the built in Realm filtering mechanism
let results = realm.objects(ItemClass.self).filter("product != nil")
Those results will be live updating - if an object leaves the filter parameter, the results follow that. If an object matches the filter the results are updated as well.
I believe this Github issue #2138 provides some more light on the issue.
If you absolutely need static data, then I would suggest extending the Results class to return an array; like this
extension Results {
func toArray() -> [Element] {
return compactMap { $0 }
}
}
Keeping in mind this will use more memory as Realm objects are lazy loaded and and array isn't.
EDIT:
There's some additonal information in the question so I crafted up a simple example trying to replicate the issue. There's a HouseClass object which contains a List of RoomClass objects and then the HouseClass is extended to return the total width of all of the rooms in its list.
class RoomClass: Object {
#objc dynamic var room_name = ""
#objc dynamic var width = 0
#objc dynamic var length = 0
#objc dynamic var belongs_to_house: HouseClass!
}
class HouseClass: Object {
#objc dynamic var house_id = NSUUID().uuidString
#objc dynamic var house_name = ""
let rooms = List<RoomClass>()
override static func primaryKey() -> String? {
return "house_id"
}
}
extension HouseClass {
var totalWidth: Int {
let width = Array(rooms).map {$0.width}.reduce(0,+)
return width
}
var anotherTotalWidth: Int {
let width = rooms.map {$0.width}.reduce(0,+)
return width
}
}
and then the code to get all of the houses and output their room widths based on two different functions (see the HouseClass extension)
let houseResults = realm.objects(HouseClass.self)
for house in houseResults {
let w0 = house.totalWidth
print(w0)
let w1 = house.anotherTotalWidth
print(w1)
}
I added 100 houses each with three rooms and ran the above code several times without crash.
Count of f1 is 0 so map is not worked.
You can optimize your width calculation as following
let width = activeItems
.filter { $0.product?.isProduct ?? false }
.map { $0.spaceRequired }
.reduce(0,+)

How to avoid "Key paths that include an array property must use aggregate operations" exception when querying a Realm?

I have a Realm object database with the following structure:
class ItemModel: Object {
dynamic var id: Int = 0
dynamic var catId: Int = 0
dynamic var subId: Int = 0
dynamic var itemName: String = ""
dynamic var sortOrder: Int = 0
dynamic var favorite: Bool = false
override static func primaryKey() -> String {
return "id"
}
}
class SubCategoryModel: Object {
dynamic var id: Int = 0
dynamic var catId: Int = 0
dynamic var subCatName: String = ""
dynamic var desc: String = ""
dynamic var sortOrder: Int = 0
let haveUnit = List<ItemModel>()
override static func primaryKey() -> String {
return "id"
}
}
class CategoryModel: Object {
dynamic var id: Int = 0
dynamic var catName: String = ""
dynamic var sortOrder: Int = 0
override static func primaryKey() -> String {
return "id"
}
}
To query the database I'm using the following code:
var data = List<ItemModel>()
let index = indexPath.section
var query = "catId == \(inCategory)"
if (searchTextValue != "") {
query += " AND itemName CONTAINS[c] \(searchTextValue)"
}
if index == 0 {
let unitFavorite = DBManager.realm.objects(ItemModel.self).filter("\(query) AND favorite == true")
data += unitFavorite
} else {
var subquery = "catId == \(inCategory)"
if (searchTextValue != "") {
subquery += " AND ANY haveUnit.itemName CONTAINS[c] %#"
let subCat = DBManager.realm.objects(SubCategoryModel.self).filter(subquery, searchTextValue).sorted(byKeyPath: "sortOrder", ascending: true)
data += subCat[indexPath.section - 1].haveUnit
} else {
let subCat = DBManager.realm.objects(SubCategoryModel.self).filter(subquery).sorted(byKeyPath: "sortOrder", ascending: true)
data += subCat[indexPath.section - 1].haveUnit
}
}
When running this code I'm always seeing the following exception:
Terminating app due to uncaught exception 'Invalid predicate', reason: 'Key paths that include an array property must use aggregate operations'
Can you please help me understand why I'm seeing this exception and what I can do to avoid it?

Realm/Swift: compilation error: Missing argument for parameter 'forPrimaryKey' in call

I'm trying to get my head around swift & realm, so I've created some kind of a test pad programme.
My model is defined like so
class RealmRecord: Object {
// properties
dynamic var id: Int = 0;
dynamic var text: String = ""
dynamic var var1: Double = 0.0
dynamic var var2: Int = 0
dynamic var var3: Double = 0.0
dynamic var var4: Int = 0
dynamic var cdate: Date = Date()
dynamic var cusr: String = ""
dynamic var mdate: Date = Date.distantPast
dynamic var musr: String = ""
dynamic var mcnt: Int = 0
// methods
convenience init(id: Int? = 0, text: String? = "", var1: Double? = 0.0,
var2: Int? = 0, var3: Double? = 0.0, var4: Int? = 0,
cusr: String? = "") {
self.init()
self.id = id!
self.text = text!
self.var1 = var1!
self.var2 = var2!
self.var3 = var3!
self.var4 = var4!
self.cdate = Date()
self.cusr = cusr!
self.mdate = Date.distantPast
self.musr = ""
self.mcnt = 0
} // init
override static func primaryKey() -> String? {
return "id"
} // primaryKey
} // RealmRecord
Persisting the data is accomplished by
try recRealm?.write {
recRealm?.add(self.rec, update: true)
} // try
But when adding the data retrieval via
if let inrec = self.recRealm?.object(RealmRecord.self) {
return inrec
} else {
return List<RealmRecord>()
} // if/else
I'm receiving an error message while compiling the code, reading
DataRealmRecord.swift:84:39: Missing argument for parameter 'forPrimaryKey' in call
Looking at the Realm documentation reveals only retrieving all persisted data--apparently without having a primary key defined--, or, alternatively, a single object, specified by the primary key.
Sifting through The Net brings up pretty much the same.
Given the model above, how can I retrieve all persisted data?
-- Sil68
EDIT
I've also defined a class facilitating this Realm model of mine, which basically carries out the following steps:
generate some random data;
persist data via the Realm model;
read data in again;
compare generated with read data.
The code
import Foundation
import RealmSwift
class DataRealmRecord {
// properties
private(set) var recDBPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
private(set) var recDBSubPath = "Persistency"
private(set) var recDBFile = "data.realm"
private(set) var recRealm: Realm?
private(set) var recRealmCfg: Realm.Configuration?
private(set) var rec = List<RealmRecord>()
private(set) var startTime = 0.0
private(set) var stopTime = 0.0
private(set) var runTime = 0.0
private(set) var outLog = ""
private(set) var realmOk = false
// methods
init() {
// assemble destination folder/database name
do {
try FileManager.default.createDirectory(atPath: recDBPath + "/" +
recDBSubPath,
withIntermediateDirectories: true,
attributes: nil)
recDBFile = recDBPath + "/" + recDBSubPath + "/" + recDBFile
realmOk = true
} catch let error as NSError {
outLog += error.localizedDescription
realmOk = false
} // do/try/catch
// configure realm database
if (realmOk) {
self.recRealmCfg = Realm.Configuration(fileURL: Foundation.URL(string: self.recDBFile))
do {
self.recRealm = try Realm(configuration: self.recRealmCfg!)
realmOk = true
} catch let error as NSError {
outLog += error.localizedDescription
realmOk = false
} // do/try/catch
} // if
} // init
// generate test data
func generateData(noRecs: Int? = 1000, simDat: SimulateData?) {
for i in 1...noRecs! {
let realmRec = RealmRecord(id: i,
text: String(format: "Record #%04d", i),
var1: simDat?.datnorm[i - 1] ?? 1.1,
var2: simDat?.datpois[i - 1] ?? 2,
var3: simDat?.datunif[i - 1] ?? 3.3,
var4: simDat?.datbern[i - 1] ?? 4,
cusr: "me")
self.rec.append(realmRec)
} // for
} // generateData
// retrieve test data from persistent storage
func loadData() -> List<RealmRecord> {
if let inrec = self.recRealm?.object(RealmRecord.self) {
return inrec
} else {
return List<RealmRecord>()
} // if/else
} // loadData
// save test data to persistent storage
func saveData() {
do {
try recRealm?.write {
recRealm?.add(self.rec, update: true)
} // try
} catch let error as NSError {
outLog += error.localizedDescription
} // do/try/catch
} // saveData
// compare two data sets
func compareData(rec1: List<RealmRecord>, rec2: List<RealmRecord>) -> Bool {
var rc = false
if rec1.count == rec2.count {
rc = true
for i in 0..<rec1.count {
rc = rc && (rec1[i] == rec2[i])
} // for
} // if
return rc
} // compareData
// run a full evaluation cycle
// (1) generate test data;
// (2) save test data to persistant storage;
// (3) retrieve test data from persistant storage;
// (4) compare generated data with retrieved data.
func fullCycle(noRecs: Int? = 1000, simDat: SimulateData?, prnData: Bool? = false) {
// start execution time measurement
self.startTime = Double(CFAbsoluteTimeGetCurrent())
// execute the full cycle
self.generateData(noRecs: noRecs, simDat: simDat) // (1)
self.saveData() // (2)
let rec2 = self.loadData() // (3)
let cmpRec = compareData(rec1: self.rec, rec2: rec2) // (4)
// stop execution time measurement & calculate elapsed time
self.stopTime = Double(CFAbsoluteTimeGetCurrent())
self.runTime = self.stopTime - self.startTime
} // fullCycle
} // DataRealmRecord
Issue at hand is, this code fails to compile due to the error message mentioned above (in method loadData()).
The Swift compiler is telling you that it thinks you're trying to call Realm.object(ofType:forPrimaryKey:), which retrieves a single object based on the value of its primary key. It sounds like you really want to call Realm.objects(_:) to retrieve all objects of a given type. Note that this returns a Results<T>, not a List<T>.

Problems linking objects in realm swift

I am having problems linking objects in realm. I read the question and answer titled "Nested objects in realm" and tried the answer suggested but it did not work for me.
Here are my object classes:
import Foundation
import RealmSwift
class Job: Object {
dynamic var id = NSUUID().UUIDString
dynamic var name = ""
dynamic var address = ""
dynamic var phone = ""
dynamic var email = ""
dynamic var notes = ""
dynamic var material = 0.0
dynamic var edge = 0.0
dynamic var splash = 0.0
dynamic var discount = 0.0
dynamic var trip = 0.0
let rooms = List<Room>()
override static func primaryKey() -> String {
return "id"
}
}
import Foundation
import RealmSwift
class Room: Object {
dynamic var id = NSUUID().UUIDString
dynamic var name = ""
dynamic var material = 0.0
dynamic var edge = 0.0
dynamic var splash = 0.0
dynamic var sinkType = ""
dynamic var sinkModel = ""
dynamic var numberOfSinks = 0
dynamic var faucet = ""
dynamic var rangeType = ""
dynamic var notes = ""
dynamic var jobs: Job?
let countertops = List<Countertop>()
//var linksToJob: [Job] {
// return linkingObjects(Job.self, forProperty: "rooms")
//}
override static func primaryKey() -> String {
return "id"
}
}
import Foundation
import RealmSwift
class Countertop: Object {
dynamic var id = NSUUID().UUIDString
dynamic var depth = 0.0
dynamic var width = 0.0
dynamic var cabDescription = ""
dynamic var sqFt = 0.0
dynamic var room: Room?
//var linkToRoom: [Room] {
// return linkingObjects(Room.self, forProperty: "countertops")
//}
override static func primaryKey() -> String {
return "id"
}
}
Here are the functions I use to save the data:
#IBAction func saveButton() {
jobs.name = nameTF.text!
jobs.address = addressTF.text!
jobs.phone = phoneTF.text!
jobs.email = emailTF.text!
jobs.notes = notesTV.text!
jobs.discount = Double(discountTF.text!)!
jobs.material = Double(materialTF.text!)!
jobs.edge = Double(edgeTF.text!)!
jobs.splash = Double(splashTF.text!)!
jobs.trip = Double(tripTF.text!)!
do {
try! realm.write {
realm.add(jobs)
}
}
print(jobs)
// print(Realm.Configuration.defaultConfiguration.path!)
}
func saveData(){
rooms.name = nameTF.text!
rooms.material = Double(materialTF.text!)!
rooms.edge = Double(edgeTF.text!)!
rooms.splash = Double(splashTF.text!)!
rooms.notes = notesTV.text
rooms.sinkType = sinkTypeTF.text!
rooms.sinkModel = sinkModelTF.text!
rooms.numberOfSinks = Int(numberSinksTF.text!)!
rooms.faucet = faucetTF.text!
rooms.rangeType = rangeTF.text!
rooms.jobs?.id = keyValueLabel.text!
//rooms.linksToJob
do {
try! realm.write {
realm.add(rooms)
}
}
print(rooms)
}
and:
#IBAction func addNextButton(sender: AnyObject) {
// self.realm.beginWrite()
let realm = try! Realm()
if widthTF.text != "" {
Calculations.depth = Double(depthTF.text!)!
Calculations.width = Double(widthTF.text!)!
let depthAdded = depthTF.text
cabinetDepth.append(Double(depthAdded!)!)
let widthAdded = widthTF.text
cabinetWidth.append(Double(widthAdded!)!)
let descriptionAdded = pickerLabel.text
cabinetDescription.append(descriptionAdded!)
let runningTotal = Calculations.squareFeet()
squareFeetToBeAdded.append(runningTotal)
let sum = squareFeetToBeAdded.reduce(0,combine: {$0 + $1})
let roundedSqFt = Double(round(sum * 10) / 10)
sqFtLabel.text = "\(roundedSqFt)"
countertops.depth = Double(depthTF.text!)!
countertops.width = Double(widthTF.text!)!
countertops.cabDescription = pickerLabel.text!
countertops.sqFt = Double(sqFtLabel.text!)!
//countertops.linkToRoom
do {
try realm.write {
realm.add(countertops)
}
print(countertops)
} catch {
print("an error occurred")
}
widthTF.text = ""
numberTable.reloadData()
widthTF.becomeFirstResponder()
}
}
When I save the data and print the results here is what I get:
Job {
id = 00F95F55-54D8-426E-B483-C990A4171002;
name = Ken;
address = Address;
phone = phone;
email = email;
notes = Notes :;
material = 8;
edge = 0;
splash = 4;
discount = 1;
trip = 0;
rooms = RLMArray <0x7ffef9df29c0> (
);
}
Room {
id = 7D3F86B9-FCD7-4CB7-AD6E-9B8141A7390C;
name = Kitchen;
material = 9;
edge = 0;
splash = 4;
sinkType = Undermount Kitchen;
sinkModel = 50/50 Stainless Steel;
numberOfSinks = 1;
faucet = Single Hole;
rangeType = Free Standing Range;
notes = Notes:;
jobs = (null);
countertops = RLMArray <0x7ffef9df3720> (
);
}
Countertop {
id = 992B8BAE-392F-4513-85DC-CBA191D2AE08;
depth = 25.5;
width = 65;
cabDescription = Cabinet;
sqFt = 11.5;
room = (null);
}
As you can see the links return null.
Please tell me what I am doing wrong here.
Note: I comment out the linkingObjects in the models because it caused a crash. I'm not sure why.
You should use references to existing realm objects when adding them. So rather than this:
func saveData(){
rooms.name = nameTF.text!
rooms.material = Double(materialTF.text!)!
rooms.edge = Double(edgeTF.text!)!
rooms.splash = Double(splashTF.text!)!
rooms.notes = notesTV.text
rooms.sinkType = sinkTypeTF.text!
rooms.sinkModel = sinkModelTF.text!
rooms.numberOfSinks = Int(numberSinksTF.text!)!
rooms.faucet = faucetTF.text!
rooms.rangeType = rangeTF.text!
rooms.jobs?.id = keyValueLabel.text!
//rooms.linksToJob
do {
try! realm.write {
realm.add(rooms)
}
}
print(rooms)
}
You would use something like this:
let realm = try! Realm()
let room = realm.create(Room.self)
room.name = nameTF.text!
room.material = Double(materialTF.text!)!
...
let job = realm.create(Job.self)
job.name = "My job name"
...
room.job = job
Since it looks like you're entering these on separate screens, you will add a room, then you want to add a job:
if let existingJob = realm.objects(Job).filter("name == '\(jobNameToLookup)'").first {
room.job = existingJob
}
For a list, just do room.countertops.append(newCountetop), where newCountertop is the result of realm.create
Because you add each objects into the Realm individually without linking. If you'd like to link the objects, you should add the objects to the List properties, such like rooms and countertops. Also for to-one relationships, you should assign the object to the property.
So you need write the code something like the following:
...
rooms.faucet = faucetTF.text!
rooms.rangeType = rangeTF.text!
...
rooms.countertops.append(countertops)
...
jobs.rooms.append(rooms)
And,
...
countertops.rooms = rooms
...
rooms.jobs = jobs
For more details, see also the documents:
Thanks to Austin who got me on the right track. Here is the code that finally did it.
#IBAction func roomSaveNextBtnPushed(_ sender: UIButton) {
let room = Room()
room.roomName = roomTF.text!
room.material = materialTF.text!
room.edge = edgeTF.text!
room.job = realm.objects(Job.self).last
try! realm.write {
realm.add(room)
print(room)
}
}
The link is made in the ' realm.object(Job.self).last ' line of code. This adds a room to a job as expected.
The suggestion in Austin's example ' let room = realm.create(Room.self) ' caused an error, and the project would not compile.
I am using Xcode 8 with Swift 3 and Realm-swift 1.1.0
Thanks again to those who offered help.