How does realm queries work? - ios

I am trying to understand Realm.
https://academy.realm.io/posts/jp-simard-realm-core-database-engine/
I am trying to understand what does the speaker mean
We are essentially building a tree what this results should look like
I think he meant that the Realm will read all objects with name and fulltime properties but not any other properties and then checks if the name is Jack and fulltime is true. Is realm iterating through all objects?

I think he meant that the Realm will read all objects with name and fulltime properties but not any other properties and then checks if the name is Jack and fulltime is true.
Not exactly. Realm get only objects that conform to filter queries.
Is realm iterating through all objects?
You can read in Realm docs about queries that:
All queries (including queries and property access) are lazy in Realm. Data is only read when the properties are accessed.
So, Realm return only (i believe) Results<Employee> that contains filtered objects, but technically read them from realm-db only in moment when you try to access them.

Related

Does realm support LIMIT query in iOS?

I am storing list of contacts from Phone Addressbook locally in my project and for that I am using realm db, problem now I am getting is to fetch batch of contacts (like a pagination). So thinking to do it using limit query. But there is no example with LIMIT query with realm. Is there any alternative for this to do pagination in realm?
In the Realm swift's document site (https://realm.io/docs/swift/latest/), they said
Since queries in Realm are lazy, performing this sort of paginating behavior isn’t necessary at all, as Realm will only load objects from the results of the query once they are explicitly accessed.
If for UI-related or other implementation reasons you require a specific subset of objects from a query, it’s as simple as taking the Results object, and reading out only the objects you need.
So you just simple get it all and process what you need. Example from Document site
// Loop through the first 5 Dog objects
// restricting the number of objects read from disk
let dogs = try! Realm().objects(Dog.self)
for i in 0..<5 {
let dog = dogs[i]
// ...
}
You do not need to implement fetching batches on your own as Realm Swift Queries are lazily loaded. "All queries (including queries and property access) are lazy in Realm. Data is only read when the properties are accessed."
So your query is very fast but accessing the data itself isn't as fast as using an array.

With Realm, should I use a List object or Results object as the data source for a UITableView?

There are at least 2 main collection types used in Realm:
List
Results
The relevant description from the documentation on a Results object says:
Results is an auto-updating container type in Realm returned from
object queries.
Because I want my UITableView to respond to any changes on the Realm Object Server, I really think I want my UITableView to be backed by a Results object. In fact, I think I would always want a Results object to back my UI for this reason. This is only reinforced by the description of a List object in the documentation:
List is the container type in Realm used to define to-many
relationships.
Sure seems like a List is focused on data modeling... So, being new to Realm and just reading the API, I'm thinking the answer is to use the Results object, but the tutorial (Step 5) uses the List object while the RealmExamples sample code uses Results.
What am I missing? Should I be using List objects to back my UITableViews? If so, what are the reasons?
Short answer: use a List if one already exists that closely matches what you want to display in your table view, otherwise use a Results.
If the data represented by a List that's already stored in your Realm corresponds to what you want to display in your table view, you should certainly use that to back it. Lists have an interesting property in that they are implicitly ordered, which can sometimes be helpful, like in the tutorial you linked to above, where a user can reorder tasks.
Results contain the results of a query in Realm. Running this query typically has a higher runtime overhead than accessing a List, by how much depends on the complexity of the query and the number of items in the Realm.
That being said, mutating a List has performance implications too since it's writing to the file in an atomic fashion. So if this is something that will be changing frequently, a Results is likely a better fit.
You should use Results<> as the Results is auto updating to back your UITableView. List can be used to link child models in a Realm model. where as Results is used to query the Realm Objects and you should add a Realm Notification Token so you know when the Results are updated and take necessary action (reload table view etc.) Look here for realm notifications: https://realm.io/docs/swift/latest/#notifications
P.S. The data in that example is just static and no changes are observed

Realm for iOS. Lazy data loading?

I am trying to use Realm in my new project.
But am interested in:
I have 10k objects of MyType.
And I want to filter and display only most recent 10 of them.
Will all of them be moved to RAM to operate/filter them?
'realm.objects(MyType).filter(...)' - I'm just worried about 10k objects and their data would litter the app memory in case of query like above.
No? It's OK?
When you filter objects, your query is constructed in a C++ DSL, which is efficiently evaluated, bringing in some optimizations to run it fast depending on the concrete property types.
Accessor objects are instantiated by the binding lazily when you retrieve each object out of your results. So in your case only for the first 10 objects.

Realm: Query for objects with same property

Is it possible in realm to query for objects that have the same property value?
Imagine a list of contacts with firstname and lastname. I want to query all contacts that have the same name and may be duplicates in the db.
As far as I'm aware, there's no automatic way to do that with NSPredicate (Of which Realm implements); it would need to be done manually.
That being said, it should be relatively trivial to do manually; simply loop through each object, performing a query that searches for that object's name properties, and see if the number of results returned is greater than 1.
That being said, depending on how big your data set is, this could become a very slow operation very quickly. Ideally, you might be better off ensuring that duplicate entries don't occur, or if they do, to somehow index them so they're easier to look up.

Need Help Understanding Transient Property in IOS Core Data

I don't understand why things have to be so difficult in core data. I have an entity that has 2 decimal attributes, "extended" and "qty"
All I want to do is extend these 2 values (multiply) and refer to this calculated value in a fetch. such as NSDecimalNumber * extendedPrice = [self.qty decimalNumberByMultiplyingBy:self.rate];
But in other cases I will want to #sum: this extended value attribute.
The web-available documentation and examples is very weak on how to do this which to me seems a very common thing to do.
Am I on the wrong track thinking I need a transient attribute and an awakefromfetch call? I get a crash when I try and refer to a transient attribute in a fetch.
You can't use transient attributes in fetch requests if you're using an SQLite store. This is because the fetch predicate is converted into an SQL query and no code is actually called. If the attribute doesn't exist in the store then it can't be used.
If you wanted to fetch the objects and then filter / sum them then that will work with a transient because at that point inTime you actually have the instances of the objects.

Resources