问题
I've just built the latest version of my app, and have run into a problem where the List
s on all of my realm objects are not being stored.
Here is some sample code:
Object:
public class ReportItem: Object {
@objc dynamic var id: String!
@objc dynamic var someDate: Date?
// This contains another List as one of its properties
let list = List<OtherRealmObject>()
override public class func primaryKey() -> String {
return "id"
}
convenience public init(id: String, date: Date) {
self.init()
self.id = id
self.date = date
}
}
This object is being created by a json mapper from the response of a network request:
// Convert json to dictionary then
guard let id = json["id"] as? String else {
return nil
}
let date = json["date"] as? Date
let objects = json["someObjects"] as? [String: Any]
let someRealmObjects = [OtherRealmObject]()
objects.forEach { object in
// Create some realm object
someRealmObjects.append(newSomeRealmObject)
}
let reportItem: ReportItem?
if let date = date?.convertToDateFromString() {
reportItem = ReportItem(id: id, date: date)
} else {
return nil
}
reportItem!.list.append(objectsIn: someRealmObjects)
return reportItem!
Then this is passed back to my view controller, and stored like so:
// Report item is the item we just created in the json mapper
someNetworkOperation.success = { reportItem in
DispatchQueue.main.sync {
let realm = try! Realm()
try! realm.write {
realm.add(reportItem, update: true)
}
}
}
The item is then retrieved somewhere else, however list
is empty, and when I try and filter I get the error This method may only be called on RLMArray instances retrieved from an RLMRealm
. For some reason my list is not being persisted when I add the report object to the database.
This used to work, however in the last week or so it has stopped working. I'm wondering if it's to do with updating to Swift 4.2/Xcode 10. Also, my code just works fine in debug, not in release. Has anyone else run into this issue?
回答1:
This was because during the Swift 4.2 conversion Reflection Metadata Level
was somehow set to None
instead of All
. 🤦♂️
来源:https://stackoverflow.com/questions/52635664/realm-list-not-stored-in-swift-4-2-in-release-config