Realm is saving only my most recently added object - ios

I'm trying to save my array of objects in Realm, but Realm appears to be saving only the last object.
This is my model class:
class ContactEntry: Entry {
dynamic var contactId: Int = 0
dynamic var email: String? = ""
dynamic var phone: String? = ""
dynamic var building: String? = ""
dynamic var room: String? = ""
dynamic var contactDescription: String? = ""
dynamic var worktime: String? = ""
dynamic var address: String? = ""
dynamic var personEntries: PersonEntry?
}
This is the base class:
class Entry: Object {
dynamic var id: Int = 0
override static func primaryKey() -> String? { return "id" }
}
This is code I'm using for saving:
func createOrUpdate(entities: [Entity]) {
let entries = toEntries(entities)
let realm = try! Realm()
try! realm.write {
realm.add(entries, update: true)
}
}
func toEntry(entity: Contact) -> ContactEntry {
let entry = ContactEntry()
entry.contactId = entity.id
entry.email = entity.email
entry.phone = entity.phone
entry.building = entity.building
entry.room = entity.room
entry.contactDescription = entity.description
entry.worktime = entity.worktime
entry.address = entity.address
entry.personEntries = personDAO.toEntry(entity.person)
return entry
}
func toEntry(entity: Person) -> PersonEntry {
let entry = PersonEntry()
entry.personId = entity.id
entry.firstname = entity.firstname
entry.middlename = entity.middlename
entry.lastname = entity.lastname
entry.photo = entity.photo
return entry
}
I think that it may be because I have relationship in my model, but I'm not sure why they'd be a problem.
I'm using them as described in the Realm documentation.

Related

Write to Realm in a loop

I am storing workout information in Realm from this object class:
class Workout: Object {
#objc dynamic var date: Date?
#objc dynamic var name: String?
#objc dynamic var exercise: String?
#objc dynamic var sets = 0
#objc dynamic var reps = 0
#objc dynamic var kg: Double = 0.0
#objc dynamic var notes: String?
}
I have data stored as an array, that I want to write to Realm, like this:
var workoutName = ""
var exercises = [String]()
var sets = [Int]()
var reps = [Int]()
var kg = [Double]()
var notes = [String]()
#IBAction func saveWorkoutButton(_ sender: Any) {
let realm = try! Realm()
print(Realm.Configuration.defaultConfiguration.fileURL!)
let currenDate = Date() // Get the current date
let myWorkout = Workout()
myWorkout.date = currenDate
myWorkout.name = workoutName
for i in 0 ..< exercises.count {
myWorkout.exercise = exercises[i]
myWorkout.sets = sets[i]
myWorkout.reps = reps[i]
myWorkout.kg = kg[i]
myWorkout.notes = notes[i]
realm.add(myWorkout)
}
try! realm.write {
realm.add(myWorkout)
}
}
The problem in this case is that when running the realm.write ... it only adds the last value from the for i in 0 function. I tried putting the realm.write function inside of the for i in 0 loop, but then it crashes. Each of the arrays contains equal numbers of objects. Any ideas?
You can try this code:
var myWorkouts:[Workout] = []
for i in 0 ..< exercises.count {
let myWorkout = Workout()
myWorkout.date = currenDate
myWorkout.name = workoutName
myWorkout.exercise = exercises[i]
myWorkout.sets = sets[i]
myWorkout.reps = reps[i]
myWorkout.kg = kg[i]
myWorkout.notes = notes[i]
myWorkouts.append(myWorkout)
}
try! realm.write {
realm.add(myWorkouts)
}
You should only add and write once if possible for performance optimisation.
Hope this helps.

Adding an array of Json Data to Realm

I'm making an app for airports and I'm getting an array of data from one api, like so:
"data":[
{"id":"001","code":"ABZ","name":"Aberdeen","country":"United Kingdom"},
{"id":"002","code":"AUH","name":"Abu Dhabi","country":"United Arab Emirates"},
.
.
.
]
AND :
"airports":[
{"from":"001",
"to":["1","3","11","13","12","20","23","27","29","31","33"]
},
.
.
.
]
I have created realm model classes:
class AirportsDataRealm: Object {
#objc dynamic var name: String = ""
#objc dynamic var id: Int = 0
#objc dynamic var code: String = ""
#objc dynamic var country: String = ""
override static func primaryKey() -> String? {
return "id"
}
}
class AirportsFromToRealm: Object {
#objc dynamic var fromID: Int = 0
var toID = List<Int>()
override static func primaryKey() -> String? {
return "fromID"
}
}
now I want to save it into realm, I'm using swiftyJSON and I have used for-loop to do it and it is working fine but I think it's taking long time since the array is very long, here is what I've done:
// Airports Data
let countData = json["data"].count
for i in 0...countData - 1{
let airportsDataModel = AirportsDataRealm()
airportsDataModel.code = json["data"][i]["code"].stringValue
airportsDataModel.name = json["data"][i]["name"].stringValue
airportsDataModel.country = json["data"][i]["country"].stringValue
airportsDataModel.id = Int(json["data"][i]["id"].stringValue)!
try! realm.write {
realm.add(airportsDataModel, update: true)
}
}
//Airports FROM-TO
let countFromTo = json["airports"].count
for i in 0...countFromTo - 1{
let fromToDataModel = AirportsFromToRealm()
fromToDataModel.fromID = Int(json["airports"][i]["from"].stringValue)!
let arrayTo = json["airports"][i]["to"].arrayValue.map{ $0.intValue }
fromToDataModel.toID.append(objectsIn: arrayTo)
try! realm.write {
realm.add(fromToDataModel, update: true)
}
}
is there any way to save the whole array in realm in one shot without for-loop?
P.S
"there should be a relation between the two tables because each from 'id' has a list of 'to' id's and the id's are from the data table, for now I managed to create this relations when fetching the data using filters ,, so just ignore this"
Thank you
Simply use map method,
First I needed to add initializers to my object classes and pass json array as a parameter, like so:
class AirportsDataRealm: Object {
#objc dynamic var name: String = ""
#objc dynamic var id: Int = 0
#objc dynamic var code: String = ""
#objc dynamic var country: String = ""
convenience required init(withJSON json : JSON) {
self.init()
self.name = json["name"].stringValue
self.id = json["id"].intValue
self.code = json["code"].stringValue
self.country = json["country"].stringValue
}
override static func primaryKey() -> String? {
return "id"
}
}
class AirportsFromToRealm: Object {
#objc dynamic var fromID: Int = 0
var toID = List<Int>()
convenience required init(withJSON json : JSON) {
self.init()
self.fromID = json["from"].intValue
let toArray = json["to"].arrayValue.map{ $0.intValue }
self.toID.append(objectsIn: toArray)
}
override static func primaryKey() -> String? {
return "fromID"
}
}
Then by using map method the code will look like this:
func updateAirport(json: JSON) {
// Airports Data
let airportsData : [AirportsDataRealm]
let airportsDataJsonArray = json["data"].array
airportsData = airportsDataJsonArray!.map{AirportsDataRealm(withJSON: $0)}
//Airports FROM-TO
let airportsFromTo : [AirportsFromToRealm]
let airportsFromToJsonArray = json["airports"].array
airportsFromTo = airportsFromToJsonArray!.map{AirportsFromToRealm(withJSON: $0)}
//Write To Realm
try! realm.write {
realm.add(airportsData, update: true)
realm.add(airportsFromTo, update: true)
}
}
No for loops anymore ^_^

Array of Strings in Realm

I'm using UICollectionView to display various images. I'm storing the path of images in realm.I want to create an array of images and store it in realm with all images in one array.
My code:
My realm Object
class cityDetail: Object {
dynamic var id = UUID().uuidString
dynamic var cityName = ""
dynamic var descrp = ""
dynamic var state = ""
dynamic var season = ""
dynamic var longitude = ""
dynamic var latitude = ""
var imgList = List<cityImage>()
override static func primaryKey() -> String? {
return "id"
}}
class cityImage:Object {
dynamic var imgStr = ""}
My function:
#IBAction func setIMG(_ sender:UIButton) {
self.img.isHidden = true
self.imgArray.append(imgURL)
print(imgArray)
newImage.imgStr = imgURL
citydetails.imgList.append(newImage)
let realm = try! Realm()
try! realm.write {
realm.add(newImage)
}
self.collection.reloadData()
}
Function in which I want array of path of images:
#IBAction func sendData(_ sender:UIButton) {
let citydetails = cityDetail()
let newImage = cityImage()
citydetails.cityName = self.cityName.text!
citydetails.descrp = self.descrp.text!
citydetails.state = self.state.text!
citydetails.season = self.season.text!
citydetails.latitude = String(lon)
citydetails.longitude = String(lat)
citydetails.imgList = newImage
let realm = try! Realm()
do {
try realm.write() {
realm.add(citydetails)
}
} catch {
}
self.collection.reloadData()
self.img.isHidden = true
}

How to combine arrays depending on user selection?

I want to see what user selected like the name of book and its assocaited chapters
I did this
struct bookChpt {
var book:[String] = []
var chapter:[[Int]] = []
}
let chptSelected = [bookChpt(book:bookArr,chapter:chptArr)]
var bookArr:[String] = []
var chptArr:[[Int]] = []
I have this in viewDidLoad()
if let bTitle = result.value(forKey: "bookTitle") as? String
{
bookArr.append(bTitle)
}
if let cNo = result.value(forKey: "chpNo") as? [Int]
{
chptArr.append(cNO)
}
print(chptSelected)
I am getting this
bookChpt( book: ["Hobbit", "LOTR"], chapter: [[3,5],4])
but I like to see this
["Hobbit", 3, 5], ["LOTR", 4]
There are a couple of possibilities. You could add a function to the struct to display its contents in the way you want:
struct BookChapter {
var book:[String] = []
var chapter:[[Int]] = []
func display() -> [[Any]] {
var output = [[Any]]()
for i in 0..<book.count {
output.append([book[i], chapter[i]])
}
return output
}
}
Or you could modify the structure of the struct to contain the book and chapters as tuples:
struct BookChapter {
var book:[(String, [Int])]
}
Going a bit further, anywhere you see a loop - such as in the display function above - you might also consider using map to achieve the same thing:
func display() -> Any {
return book.enumerated().map { $0.element + " " + chapter[$0.offset].description }
}
If you use an Dictionary like this, you can print the key and value whatever way you wanted.
var bookChapters = [String: [Int]]()
bookChapters["Hobbit"] = [1,2,3]
bookChapters["Hobbit"]?.append(contentsOf: [4])
for (book, chapter) in bookChapters {
print("\(book): \(chapter)")
}
Change your struct to
struct BookChapt {
var book: String = ""
var chapter: [Int] = []
}
and in viewDidLoad()
var bookName = ""
var chapters:[Int] = []
if let bTitle = result.value(forKey: "bookTitle") as? String
{
bookName = bTitle
}
if let cNo = result.value(forKey: "chpNo") as? [Int]
{
chapters = cNo
}
let chptSelected = BookChapt(book: bookName, chapter: chapters)
print(chptSelected)

Access data of class in class

I have this classes:
class FacebookDB: Object {
dynamic var userName: String = ""
dynamic var profilePicture: String = ""
}
class TwitterDB: Object {
dynamic var userName: String = ""
dynamic var profilePicture: String = ""
}
class UserDB: Object {
dynamic var id = 0
override class func primaryKey() -> String {
return "id"
}
dynamic var userName: String = ""
dynamic var firstName: String = ""
dynamic var lastName: String = ""
dynamic var email: String = ""
dynamic var facebookAccount = FacebookDB?()
dynamic var twitterAccount = TwitterDB?()
}
After I put a value inside the userName
//user value come from facebook
let testUser = UserDB()
let realm = try! Realm()
testUser.FacebookAccount?.userName = user.userName
print(user.userName)
print(testUser.FacebookAccount?.userName)
Here is the output:
vivieng
nil
Why can't I print testUser.FacebookAccount?.userName?
BTW I can access every value from UserDB but none of FacebookDB nor TwitterDB, any idea?
Finally I found how to deal with this.
I need to create a value like that:
let facebookAccount = FacebookDB()
And then put everything I want inside:
facebookAccount.userName = user.userName
And finally copy the class inside the class:
testUser.FacebookAccount = facebookAccount
And voilĂ , that's work now!

Resources