iOS Swift 3 Storing NSArray Values To Realm - ios

In my Application we are using Realm Storage for storing values locally. I can Able To Store my Array Values As Sting. But Not Able To Store and Retrive Values As Array. Is It Possible To Store NSArray Values To Realm Object and Retrieve It As NSArray.
Here Is The Code I Have Used To Store String Values:
class PracticeView: Object
{
dynamic var PracticeArray = ""
}
And Usage:
let realm:Realm = try! Realm()
let PracticeDetails = PracticeView()
PracticeDetails.PracticeArray = "Test String Values"
try! realm.write
{
realm.add(PracticeDetails)
}
print("Values In Realm Object: \(PracticeDetails.PracticeArray)")
//Result Will Be
Values In Realm Object: Test String Values

No, Realm cannot store native arrays (Whether Objective-C NSArray objects or Swift arrays) as properties of the Object model classes.
In Realm Swift, there is an object called List that lets you store an array of Realm Object instances as children. These still can't be strings though, so it's necessary to encapsulate the strings in another Realm Object subclass.
class Practice: Object {
dynamic var practice = ""
}
class PracticeView: Object {
let practiceList = List<Practice>()
}
let newPracticeView = PracticeView()
let newPractice = Practice()
newPractice.practice = "Test String Value"
newPracticeView.practiceList.append(newPractice)
let realm = try! Realm()
try! realm.write {
realm.add(newPracticeView)
}
For more information, I recommend checking out the 'To-Many Relationships' section of the Realm Swift documentation. :)

Related

Appending to a Realm List Only if Unique Primary Key

I am implementing Realm in my iOS project and wondering if there is a way to append an Object to a List only if the Object's primary key is unique. Currently I have something like:
let realm = try! Realm()
let message = RealmMessage()
message.id = 99999
message.desc = "Please Help!"
let chatroom = realm.objects(RealmChatRoom.self)[0]
try! realm.write {
chatroom.messages.append(message)
}
However this will crash if the messages is already in the list.
I know that complete objects can be updated using something like:
try! realm.write {
realm.add(chatRoom, update: .modified)
}
But does something like this exist for append? I.e. only write if unique key otherwise overwrite?
List stores the object references and its elements auto-update with message update. So if chatroom.messages contains a message, no need to append it again. As a result you can use this code for updating:
try! realm.write {
realm.add(message, update: .modified)
if !chatroom.messages.contains(message) {
chatroom.messages.append(message)
}
}

How to compare two lists of String in Realm Swift?

I have the following Realm data structure:
class Pair {
#objc dynamic var kanji: String? = nil
let kana = List<String>()
}
I need to look up an entry in the Realm the Pair.kana property of which has the same elements. How do I achieve that (preferably using NSPredicate)?

Swift ios store Data and String in Array

I wonder if its possible to store both Data and String in a array?
I need a way to store images that user picks together with the image names inside a array so I later can append it to my API request when I upload it.
Now I am using a array tuple that I later append Data and String to
var imgArray = [(Data, String)]()
Then I add data to that array tuple like this:
if let firstImage = self.firstImage {
if let firstImageData = firstImage.compressImage() {
self.imgArray.append(firstImageData, self.randomImageName(length: 15))
}
I use the code above for everyimage the user uploads and it works, imgArray gets populated with both Data and String which I later send to my API.
But is there a way to use a array to store Data and String values to?
I am not sure if tuples is the best solution
}
Apple discourages from using tuples as data source.
Tuples are useful for temporary groups of related values. They are not suited to the creation of complex data structures. If your data structure is likely to persist beyond a temporary scope, model it as a class or structure, rather than as a tuple
The (object oriented) Swift way is a struct:
struct ImageData {
var data : Data
var name : String
}
var imgArray = [ImageData]()
if let firstImage = self.firstImage {
if let firstImageData = firstImage.compressImage() {
self.imgArray.append(ImageData(data:firstImageData, name:self.randomImageName(length: 15))
}
The benefit is to get the members by name
let imageName = imgArray[0].name

How to move objects in Realm

I have Realm data model
import RealmSwift
class Priority: Object {
dynamic var id = ""
dynamic var text = ""
dynamic var time = ""
}
And I can get all the stored objects
let realm = try! Realm()
let objects = realm.objects(Priority)
How to move an object from the index 7 in the index 3 and save the table?
Objects stored in a Realm are unordered. If you want to store an ordered list of objects you can do so using a List<T> property on a model class. List provides mutation methods that can be used to change the order of objects within the list.
You can put the Objects in Realm List, then you can use both move and swap methods to reorder.
Here is the List API: https://realm.io/docs/swift/latest/api/Classes/List.html

Do I need to write all the child objects in a class in realm as well?

Following the example code as shown:
// Define your models like regular Swift classes
class Dog: Object {
dynamic var name = ""
dynamic var age = 0
}
class Person: Object {
dynamic var name = ""
dynamic var picture: NSData? = nil // optionals supported
let dogs = List<Dog>()
}
// Use them like regular Swift objects
let myperson = Person()
let mydog = Dog()
mydog.name = "Rex"
myperson.dogs.append(mydog)
// Persist your data easily
let realm = try! Realm()
try! realm.write {
// do I need to add this statement??
realm.add(mydog)
realm.add(myperson)
}
Do I need to persist the mydog object as well, or that Realm is smart enough to know that it is a new child object of the myperson and it will persist it for me?
No you do not need to persist the actual dog object, if you already persist an object containing it.
For the users who are wondering about cascading delete, the answer is NO.
Realm supports cascading write but NOT delete. For delete you may have to query all the relations and delete one by one.

Resources