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 ^_^
Related
Using Swift 5 for iOS13.
I am trying to update an existing Realm record with a Contact Picker result. The function deletes all the object content except for the new content.
My code
class Person: Object {
#objc dynamic var personId = UUID().uuidString
#objc dynamic var firstName: String = ""
#objc dynamic var surname: String = ""
#objc dynamic var mobileNumber: Int = 0
#objc dynamic var password: String = ""
#objc dynamic var myContactID: String = ""
override static func primaryKey() -> String? {
return "personId"
}
}
extension HomeController: CNContactPickerDelegate {
func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) {
picker.dismiss(animated: true, completion: nil)
let person = Person()
let me = realm.objects(Person.self).filter("mobileNumber == %#", mobileNumber)
person.myContactID = contact.identifier
person.personId = me.first!.personId
try! realm.write {
realm.add(person, update: .modified)
}
self.viewWillAppear(true)
}
}
All the existing content of the Person class in the Realm database disappears except for myContactID and personID.
That is because you are updating the Person with new data.
The line let person = Person() creates a new instance with all the default values. (firstName: String = "" etc..)
So, when you assign myContactID and personId to this newly created person, it will look like this:
Person {
personId = FAE4C224-D37E-4C77-B6F1-C60A92F188D0;
firstName = ;
surname = ;
mobileNumber = ;
password = ;
myContactID = contactIdentifier;
}
And when you call realm.add(person, update: .modified), it will overwrite the record associated with the primary key with this newly created person.
You want to fetch an existing person and modify it. You can do something like this:
guard let me = realm.objects(Person.self).filter("mobileNumber == %#", mobileNumber).first else { return }
try! realm.write {
me.myContactID = contact.identifier
}
I have product realm object like this:
class Product : Object {
#objc dynamic var productID : String = ""
#objc dynamic var name : String = ""
#objc dynamic var unitPrice: Double = 0.0
#objc dynamic var quantity = 0
#objc dynamic var descriptionProduct : String = ""
#objc dynamic var hasBeenAddedToWishList : Bool = false
#objc dynamic var hasBeenAddedToCart : Bool = false
#objc dynamic var isNewProduct : Bool = false
var imagePaths = List<String>()
override static func primaryKey() -> String? {
return "productID"
}
func toDictionary() -> [String:Any] {
return [
"productID" : productID,
"name" : name,
"unitPrice": unitPrice,
"quantity": quantity,
"descriptionProduct": descriptionProduct,
"hasBeenAddedToWishList": hasBeenAddedToWishList,
"hasBeenAddedToCart": hasBeenAddedToCart,
"isNewProduct": isNewProduct,
"imagePaths": imagePaths
]
}
}
I want to update data of a product using this function:
static func updateProductDataInRealmDatabase(oldProductData: Product, newProductData: Product) {
guard let matchingProduct = RealmService.shared.realm.objects(Product.self).filter("productID == %#", oldProductData.productID).first else {return}
var newDataProductDictionary = newProductData.toDictionary() // the definition of toDictionary is in above code
newDataProductDictionary["productID"] = nil // to avoid updating the primary key
RealmService.shared.update(object: matchingProduct, for: newDataProductDictionary)
}
and here is the definition of realm service to update data:
class RealmService {
private init() {}
static let shared = RealmService()
var realm = try! Realm()
func update<T: Object>(object: T, for dictionary: [String: Any]) {
do {
try realm.write {
for (key,value) in dictionary {
object.setValue(value, forKey: key)
}
}
} catch {
post(error)
}
}
}
I actually can update some of the data to be the new one like the productName, productPrice etc, But I can't update the imagePaths property which is a List<String>
the array will always to be zero after updating the data like the image below, the imagePaths array become zero :
so how to update the product data properly ? especially to update the List data in my realm object. what went wrong in here ? I use the code below to update the realm database :
func update<T: Object>(object: T, for dictionary: [String: Any]) {
do {
try realm.write {
for (key,value) in dictionary {
object.setValue(value, forKey: key)
}
}
} catch {
post(error)
}
}
Since Product class already has the primary key, no need to search for the existing realm object using productID.
Realm will add new object or update the old object with this query mentioned below.
You can query like this.
let realm = try! Realm()
try! realm.write {
realm.add(anyProductObject,update: true)
}
// You can customize this using generics and also use try catch to handle the errors in case you pass the non realm defined class object
I'm stuck putting all of the above together. I'll appreciate if I can get any input.
Here's my short setup:
typealias RealmObject = Object
/// Extension to ignore undefined keys when mapping
extension RealmObject : EVReflectable {
open override func setValue(_ value: Any?, forUndefinedKey key: String) { }
}
Sample Realm models:
class Product: RealmObject {
dynamic var productId: String = ""
let productLanguages = List<ProductLanguage>()
override static func primaryKey() -> String? {
return "productId"
}
}
class ProductLanguage: RealmObject {
dynamic var productLanguageId: String = ""
dynamic var languageCode: String = ""
dynamic var productName: String = ""
override static func primaryKey() -> String? {
return "productLanguageId"
}
}
To fetch product details I use Moya and RxSwift:
func getProduct(productItemKey: String) -> Observable<Product> {
return provider.request(.product(productId: productItemKey)).map(to: Product.self)
}
I think .map(to: Product.self) does not work with realm Lists out of the box. For each object inside the list I get an error:
ERROR: Could not create an instance for type
dict:{
CreateDate = "2015-10-12T11:11:50.013Z";
IngredientList = "Acao ingredient";
LanguageCode = "en-US";
ProductId = "d6bb0084-6838-11e5-9225-00ac14ef2300";
ProductLanguageId = "f96848d0-df77-4594-99b7-d390bb127891";
ProductName = Acao;
Tagline = "The smart drink - 100% organic, vegan energy booster with guara"
}
Is there any other way to map Moya response into Realm objects?
Thank you very much for any input!
Turns out it was a bug in EVReflection. Fixed in 4.17.0
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.
I am using the alamofire to get response JSON from server, then using ObjectMapper to map string to Realm object.
The realm object is:
class SolutionVideo: Object, Mappable {
dynamic var svID = 0
dynamic var solutionTitle = ""
dynamic var videoName = ""
dynamic var relatedInfo = ""
dynamic var shortDesc = ""
override static func primaryKey() -> String? {
return "svID"
}
required convenience init?(_ map: Map) {
self.init()
}
func mapping(map: Map) {
svID <- map["svID"]
solutionTitle <- map["solutionTitle"]
videoName <- map["videoName"]
relatedInfo <- map["relatedInfo"]
shortDesc <- map["shortDescription"]
}
}
The json string is:
[
{
"svID": "10",
"solutionTitle": "Video10",
"videoName": "Video10",
"realtedInfo": "",
"shortDescription": ""
},
{
"svID": "9",
"solutionTitle": "Video9",
"videoName": "Video9",
"realtedInfo": "",
"shortDescription": ""
}
]
in my viewController:
#IBAction func updateBtn(sender: AnyObject) {
// download file to update Realm
let url = "http://janicedemo.com/updates.json"
Alamofire.request(.GET, url).responseArray { (response: Response<[SolutionVideo], NSError>) in
let Array = response.result.value
print(Array)
if let Array = Array {
for video in Array {
let dbURL = Realm.Configuration.defaultConfiguration.fileURL
let realm = try! Realm(fileURL: dbURL!)
try! realm.write{
print("will save")
realm.add(video, update: true)
}
}
}
}
The problem is that I can add the object successfully. But the svID(primark key) keeps 0, instead of 10 or 9 (set in JSON). Is it because I have set default value to svID? Can someone gives me a hint? Thanks
Try
class SolutionVideo: Object, Mappable {
dynamic var svID = 0
dynamic var solutionTitle = ""
dynamic var videoName = ""
dynamic var relatedInfo = ""
dynamic var shortDesc = ""
func setCompoundID(id: Int) {
self.svID = svID
compoundKey = compoundKeyValue()
}
func setCompoundID(id: Int) {
self.solutionTitle = solutionTitle
compoundKey = compoundKeyValue()
}
func setCompoundID(id: Int) {
self.videoName = videoName
compoundKey = compoundKeyValue()
}
func setCompoundID(id: Int) {
self.relatedInfo = relatedInfo
compoundKey = compoundKeyValue()
}
func setCompoundID(id: Int) {
self.shortDesc = shortDesc
compoundKey = compoundKeyValue()
}
dynamic lazy var compoundKey: String = self.compoundKeyValue()
override static func primaryKey() -> String? {
return “compoundKey”
}
func compoundKeyValue() -> String {
return "\(svID)\(solutionTitle)\(videoName)\(relatedInfo)\(shortDesc)”
}
}
The main thing I can think of is that the primary key values are coming down as strings instead of proper integers (i.e., "10" instead of simply 10). It's possible that the mapper isn't smart enough to handle conversion of strings to integers, so it's just defaulting to 0.
According to the ObjectMapper documentation, you should be able to perform this conversion inline in your mapping function:
svID <- (map["svID"], TransformOf<Int, String>(fromJSON: { Int($0!) }, toJSON: { $0.map { String($0) } }))
Also, as a sidenote, I noticed that one of your JSON key names has a spelling error: "realtedInfo". So I'd recommend double-checking that works too. :)