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
Related
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)?
I am new to realm and iOS development so I apologize in advance if something isn’t explained properly or is just incorrect.
I have 2 Realm Object classes:
class Category: Object {
#objc dynamic var name: String = ""
#objc dynamic var color: String = ""
let trackers = List<Tracker>()
}
and
class Tracker: Object {
#objc dynamic var timeSegment: Int = 0
var parentCategory = LinkingObjects(fromType: Category.self, property:
"trackers")
}
I’m able to store new timeSegment properties consistently; however, the issue is that I cannot retrieve & display a collection of timeSegment values relating to their parentCategory. setting
var entries : Results<Tracker>?
produces all results for every category, which is the only result i'm able to pull so far after testing.
Any help is appreciated, and can follow up with any additional details. Thanks
You need to call objects on your Realm object with a filter for fetching only results that match a predicate. The realm object in this code is an instance of the Realm class.
func getTrackersWithName(_ name: String) -> Results<Tracker> {
return realm.objects(Tracker.self).filter("name = \"\(name)\"")
}
This tells Realm to fetch all objects that match the filter predicate. In this case, the filter predicate matches any object where the value of the "name" property matches the string that is passed into the method.
Here is my operate order:
1: fetch data from the servers
2: update UI
3: save data to realm
So I have an issue: When I fetch the data again, if the results contain the same data like before, So I don't want to save it to realm again. How can I solve it?
You should create a primary key for your class like
class Foo: Object {
dynamic var yourPrimaryKey = 0
dynamic var otherProperty1 = ""
// and so on
override class func primaryKey() -> String? {
return "yourPrimaryKey"
}
}
Then when you save data
let foo = Foo()
//set properties for foo
realm.add(foo, update: true)
The documentation says:
parameter update: If true, the Realm will try to find an existing copy of the object (with the same primary key), and update it.
Otherwise, the object will be added.
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. :)
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.