Swift - Get the parent of a Realm object; Always empty - swift

I've got a relationship where:
A Parent has many Children
ie:
class Factory: Object {
public let engines = List<Engine>()
}
class Engine:Object {
private let parents:LinkingObjects<Factory> = LinkingObjects(fromType: Factory.self, property: "engines")
var parent:Factory? {
return self.parents.first
}
}
I read the factories via JSON and create the children (Engine) manually in a for-loop, similar to this:
var engines:[Engine] = [Engine]()
for _ in stride(from:0, to: 3, by: 1) {
let engine: Engine = Engine.init()
engines.append(engine)
}
return engines
In my test I want to query the parent of a given engine to ensure that the parent is correct; or perhaps get a parent attribute.
However, whenever I try to grab an attribute via the parent its always empty;
for (_, element) in (factories.enumerated()) {
for (_, eng) in element.engines.enumerated() {
print (eng.parent ?? "N/A" as Any) // Always prints out N/A
}
}
Ideally I want to be able to access the parent's data; like the name of the parent, perhaps costs, etc.
I've tried resetting simulator and also deleting derived data; but regardless of what I do the results are always N/A or empty.
How can I query the given element and ensure that I can grab the parent data?
Many thanks

Turns out there were a number of issues that I had to do to resolve this.
I was using XCTest and Realm was causing issues where there were multiple targets.
Make all my model classes' public
Remove the models from the test target, this included a file where the JSON data was being loaded into memory
I had to write my data into Realm, which I had not done;
let realm = try! Realm()
try! realm.write {
for parent:EYLocomotive in objects {
for _ in stride(from:0, to: parent.qty, by: 1) {
let engine : EYEngine = EYEngine.init()
parent.engines.append(engine)
}
realm.add(parent)
}
}

Related

Realm filter not always finding data?

I have a Swift app that uses Realm as its database. In the app, I have a couple of models defined like this:
class Child: Object {
#objc dynamic var id: Int = generateChildId()
//other properties removed
}
class Parent: Object {
#objc dynamic var id: Int = generateParentId()
let children = List<Child>()
//other properties removed
}
Elsewhere in my app, I have a method that deletes a Child object like this:
static func deleteChild(parentId: Int, childId: Int) {
do {
let realm = try Realm()
realm.refresh()
guard let parent = realm.objects(Parent.self).filter("id == %#", parentId).first else {
print("Parent with ID: \(parentId) was not found!")
return
}
guard let child = parent.children.filter("id == %#", childId).first else {
print("Child with ID: \(childId) was not found!")
print("This parent object has \(parent.children.count) children:")
for child in parent.children {
print("child ID: \(child.id)")
}
return
}
realm.delete(child)
} catch {
log.error(error)
}
}
The Child objects are displayed in a UITableView. When I attempt to delete a Child object, I print out the ID to the Xcode console and then I call the deleteChild(parentId:childId) method above. Most of the time, this works as expected. But sometimes, the code that filters the Child objects by ID fails to find the matching object and prints "Child with ID: X was not found!" to the console. When this happens, I iterate through the .children property on the Parent object and print out all of the Child objects. The thing that's really confusing me is that when I do this, the Child object that I tried to find and delete appears to be there. For example:
Child with ID: 1559835636225 was not found!
This parent object has 5 children:
child ID: 1559835626285
child ID: 1559835628608
child ID: 1559835636225
child ID: 1559835643522
child ID: 1559835653041
Any idea what I'm doing wrong here?
---UPDATE---
My original post contained incorrect property names in the predicates (parentId and childId instead of just id). In my actual code, which I attempted to sanitize before posting, the property names are correctly specified as just "id". Sorry for the confusion.
Your models contain a property called id. But your predicate checks for a property called parentId or childId.
Apart from that, it might be easier to just delete a child based on its id, without retrieving its parent first. Another option would be to use primary keys, which allows you to directly fetch the child with realm.object(ofType: Child.self, forPrimaryKey: childId).
As it turns out, this was a bug in Realm and has been addressed since 3.17.1.

CoreData - Only fetch specific child managed Objects using a generic method

I have a strange problem:
I have a NSManagedObject called ItemTemplate. It has many child managed objects, e.g. CustomItemTemplate or SpecialItemTemplate.
Now I have a list viewController that is supposed to show these child managed objects. For example only "CustomItemTemplaze" or only "SpecialItemTemplate". I wrote this generic method to fetch all ItemTemplates and filter out the desired child-objects (I haven't found a better way yet).
private func loadTemplates<T: ItemTemplate>(ofType type: T.Type) -> [ModelObject] {
// ModelObject is just a model for my managed objects
var templates = [ModelObject]()
do {
let request: NSFetchRequest<ItemTemplate> = ItemTemplate.fetchRequest()
let result = try mainViewContext.fetch(request)
for item in result {
if item is T { // this is somehow always true
templates.append(item.modelObject) // add the converted item to the array
}
}
} catch let error as NSError {
print("Error: ", error.debugDescription)
}
return templates
}
I call it like this:
enum Category {
case custom
case special
public var templateClass: ItemTemplate.Type {
switch self {
case .custom:
return CustomItemTemplate.self
case .special:
return SpecialItemTemplate.self
}
}
}
loadTemplates(ofType: currentCategory.templateClass)
However, it's not filtering. if item is T seems to be true for every item. It thus returns every ItemTemplate, instead of only certain child objects.
Why is that? I can't explain it.
Thanks for any help!

Realm - Adding children to a parent, then querying results on parent

I'm working on how to add children to a Realm (Swift) parent and I want to query the results.
However, I'm coming up with a crash
do {
let realm = try Realm()
try realm.write {
for locomotive in locomotives
{
realm.add(locomotive, update: true)
}
let locomotives = realm.objects(Locomotive.self)
for locomotive in locomotives {
print (locomotive.name)
for _ in stride(from: 0, to: locomotive.qty, by: 1) {
let engine : Engine = Engine.init()
locomotive.engines.append(engine)
}
}
}
} catch let error as NSError {
//TODO: Handle error
print(error.localizedDescription as Any)
}
I want to create a certain number of children, add it to the relationship
Then when I try to query it;
let locomotives = realm.objects(Locomotive.self)
print(locomotives.count)
// Find all children that are linked to this specific parent
for loco in locomotives {
let engines = realm.objects(Engine.self).filter("parent == \(loco)")
print("listing engines")
for engine in engines {
print ("engine: \(engine.parent)")
}
}
My parent class is (at its most basic minus any mapping code)
class Locomotive: Object, Mappable {
dynamic var engineid: String = ""
var engines = List<Engine>()
}
My child class is: (at its most basic minus any mapping code)
class Engine: Object {
let parent = LinkingObjects(fromType: Locomotive.self, property: "engines")
}
This causes a crash:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "parent == Locomotive {
I'd like to get a list of all parent names for a given child; usually I'd do this like:
for each child in parent.array
{
print child.parent.name
}
But in realm, I do not get access to the parent's name.
How can I make queries on parent-child relationships and also a similar command to the above (get the parent's name attribute)?
Many thanks
Realm LinkingObjects objects don't represent a single object; they represent an array of potentially multiple objects. As such, it's necessary to query to see if your object exists in that array, instead of querying for equality.
let engines = realm.objects(Engine.self).filter("%# IN parent", loco)
Additionally, since Realm queries conform to NSPredicate, it's necessary to use the old-school %# notation, instead of Swift's inline code syntax.

How can you create Results after creating records?

I have a method that should return Results, either by successfully querying, or by creating the records if they don't exist.
Something like:
class MyObject: Object {
dynamic var token = ""
static let realm = try! Realm()
class func findOrCreate(token token: String) -> Results<MyObject> {
// either it's found ...
let tokenResults = realm.objects(MyObject.self).filter("token = '\(token)'")
if !tokenResults.isEmpty {
return tokenResults
}
// ... or it's created
let newObject = MyObject()
newObject.token = token
try! realm.write {
realm.add(newObject)
}
// However, the next line results in the following error:
// 'Results<_>' cannot be constructed because it has no accessible initializers
return Results(newObject)
}
}
Maybe I should just be returning [MyObject] from this method. Is there any benefit to trying to keep it as Results instead of Array? I guess I'd lose any benefit of postponed evaluation since I'm already using isEmpty within the method, correct?
Results is an auto-updating view into underlying data in a Realm, which is why you can't construct it directly. So instead of return Results(newObject), you should return tokenResults, which will contain your newly added object, again because Results is an auto-updating view.

Replaced List<T> object not persisting consistently in Realm

I have a List<Workout> object that occasionally needs to be sorted (e.g., if a user adds a Workout out of order), but I can't seem to get the new sorted List<Workout> to persist. My code works the moment it runs (i.e., it shows up on the view as sorted), but when I exit the ViewController or restart the app, I see nothing. The nothing is due to the exercise.workoutDiary.removeAll() persisting, but apparently the subsequent assignment to the exercise.workoutDiary = sortedWorkoutDiary is not persisting. Any ideas why?
Everything else works just fine. The typical recordWorkout() case works assuming nothing is entered out of order. So the persisting is working in nearly all cases except for this overwrite of the sorted List.
The update happens here:
struct ExerciseDetailViewModel {
private let exercise: Exercise!
func recordWorkout(newWorkout: Workout) {
let lastWorkout = exercise.workoutDiary.last // grab the last workout for later comparison
let realm = try! Realm()
try! realm.write {
exercise.workoutDiary.append(newWorkout) // write the workout no matter what
}
if let secondToLastWorkout = lastWorkout { // only bother checking out of order if there is a last workout...
if newWorkout.date < secondToLastWorkout.date { // ...and now look to see if they are out of order
let sortedWorkoutDiary = exercise.sortedWorkouts
try! realm.write {
exercise.workoutDiary.removeAll()
exercise.workoutDiary = sortedWorkoutDiary
}
}
}
}
}
final class Exercise: Object {
var workoutDiary = List<Workout>()
var sortedWorkouts: List<Workout> {
return List(workoutDiary.sorted("date"))
}
}
final class Workout: Object {
dynamic var date = NSDate()
var sets = List<WorkSet>()
}
List<T> properties in Realm Swift must be mutated in place, not assigned to. The Swift runtime does not provide any way for Realm to intercept assignments to properties of generic types. Instead, you should use methods like appendContentsOf(_:) to mutate the List<T>:
exercise.workoutDiary.removeAll()
exercise.workoutDiary.appendContentsOf(sortedWorkoutDiary)
This limitation on assignment to properties of generic types is why the Realm Swift documentation recommends that you declare such properties using let rather than var. This will allow the Swift compiler to catch these sorts of mistakes.
One further note: for your sortedWorkouts computed property, it'd be preferable for it to return Results<Workout> instead to avoid allocating and populating an intermediate List<Workout>.