Cannot store lists - Realm Swift - ios

I am trying to create a one to many relationship between medication table and side effects table. RealmObject is a custom class created from Object class.
Model definitions:
class Medication: RealmObject {
#objc dynamic var name : String?
#objc dynamic var form : String?
#objc dynamic var id : String?
let medToSideEffect = List<SideEffect>()
override class func primaryKey() -> String? {
return "id"
}
}
class SideEffect: RealmObject {
#objc dynamic var masterSideEffectId : String = ""
#objc dynamic var entityType : String = ""
#objc dynamic var entityId : String = ""
#objc dynamic var sideEffect : String = ""
#objc dynamic var id : String = ""
#objc dynamic var uniqueId : String = ""
override class func primaryKey() -> String? {
return "uniqueId"
}
}
Test code:
let medicationItem = Medication(dict: medication)
let sideEffectItems = List<SideEffect>()
for sideEffect in sideEffectList {
let sideEffectItem = SideEffect()
sideEffectItem.id = self.getMongoId()
sideEffectItem.entityType = "Medicine"
sideEffectItem.entityId = medicationItem.id!
sideEffectItem.sideEffect = (sideEffect as? String)!
sideEffectItem.uniqueId = "\(medicationItem.id!)_\(((sideEffect as? NSString)?.replacingOccurrences(of: " ", with: "_"))!)"
sideEffectItems.append(sideEffectItem)
medicationItem.medToSideEffect.append(sideEffectItem)
}
After this process, when i print the medicationItem, Output is this
Medication {
name = Paracetomol 650mg;
form = tab;
id = 5af96e79efb27f6bd5c25a66;
}
The side effects were supposed to be added to the medication object. but that is not the case anymore. It was working fine until the latest update Realm 3.11.0.
Please let me know, if the code is incorrect.

Maybe because yYou declare your list as 'immutable', and maybe because your list is a struct and therefore a copy is returned.
let medToSideEffect = List<SideEffect>()
And so it cannot be changed. Try (warning untested code)
public private(set) var medToSideEffect = List<SideEffect>()
and add a func
public func append(sideEffect: SideEffect) {
self.medToSideEffect.append(sideEffect)
}
The 'public private (set)' bit means that the list is visible externally but can only be modified by the owning class instance.

EDIT:
https://stackoverflow.com/a/52704564/6700116
Found the solution.
Go to your target Build Settings and set Reflection Metadata Level flag to All
The issue is discussed here.
https://github.com/realm/realm-cocoa/issues/5944#issuecomment-426948127

Related

How to store Array of Dictionaries to Realm Database in Swift

I am getting JSON data from server by api call in swift application.
So, I want to store that into Realm data base and again need to fetch to show in tableview.
I have no idea about Realm database, After, checked few forums, I got basic idea for creating Object class.
So, I have installed Realm pod file and imported that library to my classes.
My JSON data is
[{
"type": "story",
"story": 
{
"author-name": "",
"headline": "Quotes ",
"summary": "Best quotes of Muhammad Ali",
"hero-image": "https://image”
}
},
{
"type": “Trending”,
"story": 
{
"author-name": "",
"headline": "Quotes ",
"summary": "Best quotes of Muhammad Ali",
"hero-image": "https://image”
}
},
{
"type": “Technology”,
"story": 
{
"author-name": "",
"headline": "Quotes ",
"summary": "Best quotes of Muhammad Ali",
"hero-image": "https://image”
}
},
{
"type": “Top”,
"story": 
{
"author-name": "",
"headline": "Quotes ",
"summary": "Best quotes of Muhammad Ali",
"hero-image": "https://image”
}
}
]
And I have each type keyword has different model class saved data from api data to show in Tableview
like
let storyObj = StoryModule()
let trending = StoryModule()
let technology = StoryModule()
let stotopryObj1 = StoryModule()
and I am saving each key value for every type
if abc.type == "story" {
let storyObj = abc.story
storyObj.authorname = storyObj?.authorname
storyObj.heroimage = storyObj?.heroimage
storyObj.headline = storyObj?.headline
storyObj.summary = storyObj?.summary
self.treningStoriesList.append(storyObj)
}
It is same for remaining Trending, Top and Technology objects.
and the Realm module is
import RealmSwift
class DemoInfo: Object {
#objc dynamic var category = ""
let items = List<DemoList>()
}
class DemoList : Object {
#objc dynamic var authorName = ""
#objc dynamic var imageUrl = ""
#objc dynamic var summary = ""
#objc dynamic var headLine = ""
}
And In MainViewController class,
let realmDB = try! Realm()
But, Here I got struck, How to save those storyObj,technology,top, etc module data and fetch.
Can anyone suggest me?
If you want to add a realm object in your db, you must define a primary key for each realm object classes. So, you need to change your JSON file, after you can create your realm objects like this;
DemoObject.swift
import RealmSwift
class DemoObject: Object {
#objc dynamic var id: String = ""
#objc dynamic var type: String = ""
#objc dynamic var subObject: SubObject?
override static func primaryKey() -> String? {
return "id"
}
}
SubObject.swift
import RealmSwift
class SubObject: Object {
#objc dynamic var id: String = ""
#objc dynamic var authorName: String = ""
#objc dynamic var imageUrl: String = ""
#objc dynamic var summary: String = ""
#objc dynamic var headLine: String = ""
override static func primaryKey() -> String? {
return "id"
}
}
Then, you can use these codes to add your db.
let realm = try! Realm()
let demo = DemoObject()
demo.id = "1"
let sub = SubObject()
sub.id = "1"
sub.authorName = "Author Name"
sub.headLine = "Head Line"
sub.summary = "image Url"
demo.subObject = sub
try! realm.write {
realm.add(demo, update: true)
}

Realm model contains property but not found it at runtime

I am getting error as below
Invalid property name , reason: 'Property 'IsRecordDeleted' not found in object of type 'MyCustomModel'
Where as my Model is as Under
#objcMembers public class MyCustomModel : Object {
dynamic var Id : String = ""
dynamic var ProductId : String? = ""
dynamic var IsRecordDeleted : Bool? = false
dynamic var ProductBarcode : String? = ""
override public class func primaryKey() -> String? {
return "Id"
}
}
and I am making query like this :
let mSavedItems = mDbHelper.realmObj.objects(MyCustomModel.self).filter("IsRecordDeleted = false")
What could be problem here. I do not know why my app is crashing with the same error. But If I change the value like
let mSavedItems = mDbHelper.realmObj.objects(MyCustomModel.self).filter("ProductId = 0")
The app gets run, but crashed on when I use IsRecordDeleted in predicate.
Please tell me what could be problem
You can try
let mSavedItems = mDbHelper.realmObj.objects(MyCustomModel.self)
let filtered = mSavedItems.filter { $0.IsRecordDeleted == false }
and
let mSavedItems = mDbHelper.realmObj.objects(MyCustomModel.self)
let filtered = mSavedItems.filter { $0.ProductId == "0" }
For both
let mSavedItems = mDbHelper.realmObj.objects(MyCustomModel.self)
let filtered = mSavedItems.filter {
$0.IsRecordDeleted == false
&& $0.ProductId == "0"
}
//
let resultPredicate = NSPredicate(format: "ProductId == '0' AND IsRecordDeleted == false")
let filtered = mSavedItems.filter(resultPredicate)
I think you'll find that simply switching to filter blocks instead of string predicates might stop the crash, but will not produce the expected results.
This is because IsRecordDeleted never gets saved to the database. It is not a type that can be represented in Objective-C, therefore it cannot be dynamic, so Realm ignores it.
Take as an example the following class:
#objcMembers class MyObject: Object {
dynamic var id = ""
dynamic var testBool: Bool? = false
override static func primaryKey() -> String {
return "id"
}
}
And say we initialize them like this:
let obj1 = MyObject()
obj1.id = "1"
obj1.testBool = true
let obj2 = MyObject()
obj2.id = "2"
obj2.testBool = false
let realm = try? Realm()
try? realm?.write {
realm?.add(obj1, update: true)
realm?.add(obj2, update: true)
}
If we query Realm for these objects using realm.objects(MyObject.self), you'll get something like this
Results<MyObject> <0x7fe410c0ad90> (
[0] MyObject {
id = 1;
},
[1] MyObject {
id = 2;
}
)
And you'll see that in the database, there indeed is no property named testBool, which was our optional Bool.
You can easily see that the optional Bool may cause problems if you write it out like this instead:
class MyObject: Object {
#objc dynamic var id = ""
#objc dynamic var testBool: Bool? = false // This line will not compile.
override static func primaryKey() -> String {
return "id"
}
}
I'm curious why the IsRecordDeleted needs to be optional in the first place, since it seems to have a value from the get-go. If it doesn't, then something like this will work as expected:
#objcMembers public class MyCustomModel: Object {
dynamic var Id: String = ""
dynamic var ProductId: String? = ""
dynamic var IsRecordDeleted: Bool = false
dynamic var ProductBarcode: String? = ""
override public class func primaryKey() -> String? {
return "Id"
}
}
and you can query via string like you were trying to do in the first place.
If it has to be optional, then Realm provides a RealmOptional for this exact case, that you can look into here.

How to save array into Object in Realm Database

I'm trying to save an array data(from CarteData object) into CustomerData object in realm but I got the error from the line of append data to Object. I'm just new with Swift, please tell me the right way of storing data array to object. Thanks so much.
let carte = CarteData()
carte.carteID = record.value(forKey: "carteID") as! String
carte.carteMemo = record.value(forKey: "carteMemo") as! String
newCustomer.cartes.append(carte)
Error crash message:
My CustomerData Object:
class CustomerData: Object {
#objc dynamic var cusName: String = ""
#objc dynamic var cusID: String = ""
#objc dynamic var cusMail: Int = 0
#objc dynamic var cusBirth: Date?
let cartes = List<CarteData>()
}
My CarteData Object:
class CarteData: Object {
#objc dynamic var carteID: String = ""
#objc dynamic var carteMemo: String = ""
var parentCategory = LinkingObjects(fromType: CustomerData.self, property: "cartes")
}

Not need to update object in Realm

I try to read json and create Realm, so my code:
func workWithFileJSON () {
//local file JSON
let file = Bundle.main.path(forResource: "MobileDbSchema", ofType: "json")!
let url = URL(fileURLWithPath: file)
let jsonData = NSData(contentsOf: url)!
//Parce JSON
let json = try! JSONSerialization.jsonObject(with: jsonData as Data, options: [])
try! realm.write {
//Create data from JSON to our objects
realm.create(DataRoot.self, value: json, update: true)
}
}
and file with classes:
import Foundation
import RealmSwift
class DataRoot: Object {
dynamic var id = 0
dynamic var name = ""
let transport_type = List<Transport_type>()
override class func primaryKey() -> String? {
return "id"
}
}
class Transport_type: Object {
dynamic var id = 0
dynamic var name = ""
let routes = List<Routes>()
override class func primaryKey() -> String? {
return "id"
}
}
class Routes: Object {
dynamic var id = 0
dynamic var name = ""
let directions = List<Directions>()
override class func primaryKey() -> String? {
return "id"
}
}
class Directions: Object {
dynamic var id = 0
dynamic var name = ""
dynamic var dayIdFrom = 0
dynamic var dayIdTo = 0
let stops = List<Stops>()
override class func primaryKey() -> String? {
return "id"
}
}
class Stops: Object {
dynamic var id = 0
dynamic var busStop: BusStop?
let timetable = List<Timetable>()
override class func primaryKey() -> String? {
return "id"
}
}
class BusStop: Object {
dynamic var id = 0
dynamic var name = ""
dynamic var descript = ""
override class func primaryKey() -> String? {
return "id"
}
}
class Timetable: Object {
dynamic var hour = 0
dynamic var minute = 0
dynamic var group_index = 0
dynamic var notes = ""
}
after my first run I see good data in Realm:
but after second run I see data in Timetable x 2 and etc. time after each run.
In Timetable there are no primary keys (here don't need it). Why after each update (run) I see increase data in Timetable and how to resolve my mistake?
Even if your app doesn't need primary keys, Realm.add(_:update:) requires your Object class to implement one so it is able to identify pre-existing entries as opposed to new ones. If you do not specify a primary key, even if update: is set to true, it will add each item from JSON as a new object.
Ideally, you should be able to implement some kind of primary ID for each entry in the JSON feed so you can simply pass that along to Realm.
However, if you cannot implement primary keys, but you know that every new JSON object you pull down is a complete snapshot of your timetables, then you could also simply consider deleting all of the pre-existing timetable objects in your Realm file before adding the latest ones from the JSON file.

Realm.objects() returns empty objects

My class has all properties as dynamic but still when retrieving them from realm i get a collection of empty objects, and check the realm db with the realm browser and the data is there, this is my class:
class ProjectEntity: Object {
/**
Property: All properties of the ProjectEntity
**/
dynamic var ProjectId = 0
dynamic var ProjectTitle = ""
dynamic var ProjectSubtitle = ""
dynamic var ProjectType = ""
dynamic var ProjectClass = ""
dynamic var ProjectCoordinates = ""
dynamic var ProjectGraphType = ""
dynamic var ProjectModifiedOn = NSDate(timeIntervalSince1970: 0)
dynamic var ProjectCity = ""
dynamic var ProjectCounty = ""
dynamic var ProjectZip = ""
override static func primaryKey() -> String? {
return "ProjectId"
}
func getShape() -> MapShape{
let adapter = ProjectsJSONAdapter()
let shape: MapShape = adapter.parseShape(id: self.ProjectId, type: self.ProjectGraphType, jsonStr: self.ProjectCoordinates)
return shape
}
}
here is how i'm reading the data:
let projectsList = realm.objects(ProjectEntity)
for project in projectsList {
projects.append(project)//The properties in project have all their default/empty values
}
any ideas?
How are you retrieving the persisted ProjectEntity objects? The following code snippet should do the trick:
let entities = Realm().objects(ProjectEntity)
first your class need to inherit RLMObject :
class ProjectEntity: RLMObject {
...
}
and after if you want all the ProjectEntity objects try this :
let allProjectEntityObjects: RLMResults = ProjectEntity.allObjects()
if you need some extra help you can follow this tutorial from Realm :
Building a To-Do App with Realm

Resources