Realm-iOS: Object reference set to nil after save
For all Realm Swift properties (except for List
), you need to declare the properties as dynamic
. Changing your model definitions to the following should help!
class Company:Object {
dynamic var name:String = ""
dynamic var employee:Employee?
override static func primaryKey() -> String? {
return "name"
}
}
class Employee:Object {
dynamic var name:String = ""
dynamic var age:Int = 0
override static func primaryKey() -> String? {
return "name"
}
}
Realm object as member is nil after saving
After comparing this code to one of the Realm sample projects, it would appear that simply setting an Object
as a child of another does not implicitly write it to the database as well.
Instead, you may need to refactor your code slightly, but make sure you explicitly add your Medicine
object to Realm in a write transaction, before you set its relation to MedPack
and then write MedPack
to the database.
Realm-iOS: Model object becomes nil when it's added to realm
You should keep strong reference to your Article objects. When you load them from realm you need keep them in array. if you operate on results from realm they are released when method TableViewCell setupFavIcon ends.
Realm Swift - save a reference to another object
You need to change the declaration of workoutReference
. First of all, you need to make it Optional
by writing ?
after the type. Secondly, you shouldn't assign a default value to it, it needs to be Optional
for a reason. The linked docs clearly state that
to-one relationships must be optional
, and workoutReference
is clearly a to-one relationship.
class FavouriteObject: Object {
@objc dynamic var favouriteWorkoutName = ""
@objc dynamic var workoutReference:WorkoutSessionObject?
override class func primaryKey() -> String? {
return "favouriteWorkoutName"
}
}
What's the correct way to delete all future occurrences of a Realm object?
What I typically do is create a unique NSPredicate for each constraint you want to place on the collection. You can mix and match each predicate to form a unique set of filters. For example:
extension NSPredicate {
static func transactionDescriptionEqualTo(_ description: String?) -> NSPredicate {
if let description = description {
return .init(format: "transactionDescription == %@", description)
}
return .init(format: "transactionDescription == nil")
}
static func transactionCategoryEqualTo(_ category: Category?) -> NSPredicate {
if let category = category {
return .init(format: "transactionCategory == %@", category)
}
return .init(format: "transactionCategory == nil")
}
static func transactionDateIsAfter(_ date: Date) -> NSPredicate {
return .init(format: "transactionDate >= %@", date as NSDate)
}
static func transactionIDNotEqualTo(_ id: String) -> NSPredicate {
return .init(format: "transactionID != %@", id)
}
static func futureRepeats(of transaction: Transaction) -> NSPredicate {
return NSCompoundPredicate(andPredicateWithSubpredicates: [
.transactionDescriptionEqualTo(transaction.transactionDescription),
.transactionCategoryEqualTo(transaction.transactionCategory),
.transactionDateIsAfter(transaction.transactionDate),
.transactionIDNotEqualTo(transaction.transactionID),
])
}
}
Also note that you don't need to loop over the results. You can delete all objects in the results like this:
let realm = try Realm()
try realm.write {
realm.delete(transactions.filter(.futureRepeats(of: transaction)))
}
Lastly, it's a good idea to include the transactionID as a constraint. Otherwise the original transaction will be included in the results.
Related Topics
Swift Throws Python Errors from Terminal
How to Use Bit Field with Swift to Store Values with More Than 1 Bit
Swipe Gesture Interrupts Uislider Control in iOS 13, But Not Previous iOS Versions
Using Foreach with a String Array - [String] Has No Member 'Identified'
Querying in Firebase by Child of Child
Swift ? Must Be Followed by a Call, Member Lookup, or Subscript
Parse.Com Pfgeopoint.Geopointforcurrentlocationinbackground Not Doing Anything
Swift: Search Bar Created at Auto Focus
Swiftui: How to Set Position of Scrollview to Top Left? (Both Scrolls Is Enabled)
How to Make a Custom Mkannotationview with Xib
Swift Firebase Check If User Exists
Firestore Query Across Multiple Fields for Same Value
Why There Is a Overflow with Swift Language When Assign a 8 Bits Binary Value to a Var of Int8 Type