I am creating a Realm object like below
import RealmSwift
class Item: Object {
#objc dynamic var ID = 0
#objc dynamic var notificationTitleString = ""
#objc dynamic var notificationBodyString = ""
#objc dynamic var notificationDateString = ""
#objc dynamic var notificationType = ""
#objc dynamic var notificationUrl = ""
#objc dynamic var notificationActivity = ""
#objc dynamic var notificationIsRead = ""
override static func primaryKey() -> String? {
return "ID"
}
}
I am using the below method to get a particular item type
import UIKit
import RealmSwift
class DBManager {
private var database: Realm
static let sharedInstance = DBManager()
private init() {
database = try! Realm()
print(Realm.Configuration.defaultConfiguration.fileURL!)
}
func fetchNotificationType(type: String) -> Results<Item> {
let predicate = NSPredicate(format: "notificationType = %#", type)
let results : Results = database.objects(Item.self).filter(predicate)
return results
}
}
Using the above fetchNotificationType method i am able to get a single object, But i want all the objects in a single query. I do not know how to do that, I am trying Realm for the first time.
I searched SO, but i did not get any related answers.
Related
I am using IceCream Library and want to achieve the following functionality.
In app delegate, I have written
syncEngine = SyncEngine(objects: [
SyncObject(type: Recipient.self, uListElementType: String.self),
SyncObject(type: SMSSchedule.self, uListElementType: Recipient.self),
SyncObject(type: Template.self),
SyncObject(type: ContactsGroup.self, uListElementType: Recipient.self)
])
where Recipient class is...
#objc class Recipient: Object {
#objc dynamic var rec_id = ""
#objc dynamic var firstName = ""
#objc dynamic var lastName = ""
var phoneNumbers = List<String>()
#objc dynamic var email = ""
#objc dynamic var colorTag = "#FFFFFFFF"
#objc dynamic var isDeleted = false // IceCream requirement
override static func primaryKey() -> String? {
return "rec_id"
}
// initialization code
}
Look at the phoneNumbers property. It is a list of string objects. However the SyncObject doesn't accept String as uListElementType. How can I solve this issue?
So, I am a COMPLETE newbie, learning by doing real hands-on projects. I am attempting to create an app that can keep track of car parts. I'm trying to use Realm Database. In order to be organized, I'd like to keep everything separated by key bunches: "partInfo", "storeInfo", and "mechanicInfo" and then bring this back together again with the list feature of realm. Please see my code below and tell help me learn how to go about it? Thanks in advance!
I'm also trying to do MVC pattern--not sure if I did this right by separating everything out or not.
Here's my code for storeinfo.swift
import Foundation
import RealmSwift
class StoreInfo: Object {
#objc dynamic var _id = ObjectId.generate()
#objc dynamic var storeName: String = ""
#objc dynamic var storeNumber: String? = ""
#objc dynamic var storeAddress: String = ""
var catalog = LinkingObjects(fromType: Catalog.self, property: "stores")
//var itemImgs = List<String>()
override static func primaryKey() -> String? {
return "_id"
}
}
Here's my code for partinfo.swift
import Foundation
import RealmSwift
class PartInfo: Object {
#objc dynamic var _id = ObjectId.generate()
#objc dynamic var partName: String = ""
#objc dynamic var partNumber: String = ""
#objc dynamic var partDescription: String? = ""
#objc dynamic var partCost: Double = 0.00
#objc dynamic var partQuantity: Int = 0
#objc dynamic var purchaseDate: String = ""
#objc dynamic var hasWarranty: Bool = true
#objc dynamic var warrantyLength: String? = ""
var catalog = LinkingObjects(fromType: Catalog.self, property: "parts")
//var itemImgs = List<String>()
override static func primaryKey() -> String? {
return "_id"
}
}
here's my code for mechanicinfo.swift
import Foundation
import RealmSwift
class MechanicInfo: Object {
#objc dynamic var _id = ObjectId.generate()
#objc dynamic var mechanicName: String = ""
#objc dynamic var mechanicNumber: String = ""
#objc dynamic var mechanicAddress: String? = ""
#objc dynamic var mechanicCost: Double = 0.00
#objc dynamic var serviceDate: String = ""
var catalog = LinkingObjects(fromType: Catalog.self, property: "mechanics")
//var itemImgs = List<String>()
override static func primaryKey() -> String? {
return "_id"
}
}
and finally (where I'm stuck at) -- the code for Catalog.swift
import Foundation
import RealmSwift
class Catalog: Object {
#objc dynamic var _id = ObjectId.generate()
var parts = RealmSwift.List<PartInfo>()
var stores = RealmSwift.List<StoreInfo>()
var mechanics = RealmSwift.List<MechanicInfo>()
//var itemImgs = List<String>()
override static func primaryKey() -> String? {
return "_id"
}
when I go to try to use it, I'm getting an error of already using the same name. can't use it twice: (so I had to do 3 instances of wipers, which works, but it's not what I wanted...please help!)
My ViewController:
import UIKit
import RealmSwift
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let realm = try! Realm()
print(Realm.Configuration.defaultConfiguration.fileURL!)
let wipers = PartInfo()
wipers.partName = "TRICO HD 32 Inch Wiper Blades"
wipers.partNumber = "67-324"
wipers.hasWarranty = true
wipers.warrantyLength = "2 years"
wipers.purchaseDate = "04/21/2021"
wipers.partCost = 34.99
wipers.partQuantity = 2
wipers.partDescription = "Great set. Lifespan of 6 - 9 months."
let wipers2 = StoreInfo()
wipers2.storeName = "O'Reilly AutoParts"
wipers2.storeAddress = "1003 W Broad St, Groveland, FL 34736"
wipers2.storeNumber = "4474"
let wipers3 = MechanicInfo()
wipers3.serviceDate = "04/24/2021"
wipers3.mechanicAddress = "17605 Sunshine Circle, Winter Garden, FL, 34787"
wipers3.mechanicCost = 289.58
wipers3.mechanicName = "Bob Barnicles"
wipers3.mechanicNumber = "353-256-7893"
try! realm.write {
realm.add(wipers)
realm.add(wipers2)
realm.add(wipers3)
}
}
}
I'm developing an iOS application using Realm and have a object MedicationObject which contains the information about the Medication being used. This then has a many to one relationship class MedicationRecording which stores the information when the time the medication was taken.
import Foundation
import RealmSwift
class MedicationObject : Object {
//This model requires a Primary Key
#objc dynamic var id = 0
override static func primaryKey() -> String? {
return "id"
}
#objc dynamic var medicationName : String = ""
//stores the refrences to medication recoridngs
var messurements = List<MedicationRecording>()
public func addNewMesurment() {
print(self)
let medicationRecording = MedicationRecording()
medicationRecording.medicationTaken = self
medicationRecording.timeTaken = Date()// this saves the time to now
medicationRecording.medicationTaken = self
RealmAccess().updateMedicationObject(medicationObject: self, medicationRecord: medicationRecording) //this should work
}
}
here is my MedicationRecording:
import Foundation
import RealmSwift
class MedicationRecording : Object {
#objc dynamic var medicationTaken : MedicationObject? = MedicationObject()
#objc dynamic var timeTaken : Date = Date()
#objc dynamic var id = 0
override static func primaryKey() -> String? {
return "id"
}
}
and this is the method I'm calling to save the data to
func updateMedicationObject(medicationObject : MedicationObject, medicationRecord : MedicationRecording) {
let realm = try! getRealmAccess()
let mediObject = MedicationObject()
mediObject.medicationName = medicationObject.medicationName
do {
try! realm.write {
realm.add(medicationRecord, update: true)
medicationObject.messurements.append(medicationRecord)
}
}
}
At the moment when I'm saving the data it is then losing all the information in MedicationObject and only saving the last data.
Any help with be greatly appreciated
Thank you
I'm having a strange issue with objects in Realm. When i fetch an object from Realm database at first time & deleted object from database. unwrapping an optional value nil.
Model:
class Outlet: Object {
#objc dynamic var id:string = ""
#objc dynamic var name:string = ""
#objc dynamic var name1:Double = 0.0
#objc dynamic var name2:Double = 0.0
#objc dynamic var name3:Double = 0.0
#objc dynamic var name4:Double = 0.0
#objc dynamic var name5:Int = 0
let OutletListS = List<OutletList>()
override class func primaryKey() -> String? {
return "id"
}
}
Class:
class DBManager {
private var database: Realm
static let sharedInstance = DBManager()
private init() {
database = try! Realm()
}
func getDataFromDB() -> Outlet {
let result = database.objects(Outlet.self)
return result.first!
}
}
and calling method:
let selectedOutlet = DBManager.sharedInstance.getDataFromDB()
Error:
Printing description of result:
Results <0x7fb64f02c230> (
)
i could't solve it myself. help me please.
For example, there are two models Realm Information
import Foundation
import RealmSwift
class Music: Object {
dynamic var id = ""
dynamic var title = ""
dynamic var url = ""
}
class DownloadMusic: Object {
dynamic var id = ""
dynamic var title = ""
dynamic var path = ""
}
And there is a certain function
func test(object: AnyObject) {
}
When a call is transferred as an argument 'realm.objects(Music)[0]'
let realm = try! Realm()
test(realm.objects(Music)[0])
Can I check in function, the object belongs to a class 'Music' or to a 'DownloadMusic'
Try this:
func test(object: AnyObject) {
if object is Music {
print("music")
} else if object is DownloadMusic {
print("downloadmusic")
}
}