sortedResultsUsingProperty realm iOS - ios

in iOS realm has the ability to return a sorted RLMResults from your database.
sortedResultsUsingProperty(..)
Is it possible to 'not return anything, and just to actually sort your locally saved database ?

Realm does not currently have any way to sort all of your objects in place, as it doesn't make any guarantees about the order objects are stored in the underlying database (which allows for better performance). Most of the time, however, sorting an RLMResults is going to be fast enough that it's not a concern.

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.

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.

Effective use of Core Data and NSManagedObjectContext

I was wondering, and never found any documentation to it, as to, first of all, what is an efficient way of fetching CD objects. In my app I have to store thousands of objects in my NSMOC and need to fetch them as fast as possible. Ois usi g NSPredicate the best way to fetch the entities that fit what I want the best way?
Secondly, if I want to keep an "archive" type of thing where I store thousands of objects not used often, but I need to have them, is it possible to have two NSMOCs, and if so, will it speed up the process of fetching entities?
Thanks
If you want to fetch CoreData objects efficiently, you have to consider a few approaches:
If you fetch a lot of data, you should do it on a background thread so that you don't block the UI.
NSFetchRequest has a property called propertiesToFetch which you can use to fetch only the properties you need
You can prefetch some of the relationships for later use using the relationshipKeyPathsForPrefetching property of NSFetchRequest
You should set a predicate on your NSFetchRequest object so that you fetch only the data you need
It is also important to structure your entity graph correctly so that you don't fetch unnecessary data when you don't need it.
You can't say that NSPredicate is the best way to fetch CoreData objects because you use NSFetchRequest for this task, but it is definitely is a good idea to use NSPredicate as well.
I don't think that having a separate MOC used as an "archive" is going to help your performance. You should probably read Apple documentation on CoreData performance

CoreData sorted entities - performance

I would like to have my CoreData entities stored in a sorted array/set, so that I don't have to sort it every time in a fetch using NSSortDescriptor. In iOS 4 and below, I believe this is my only option, but sorting the entire data set (not just the fetched results) every time - that sounds terribly inefficient, even for a relatively small data set of ~10k.
In iOS 5 there are sorted sets; I wonder if the performance gain is enough to warrant dropping iOS 4 support? Any experiences to be shared?
You don't need to sort the results - you can do your fetch and pass sortOrderings to the fetch. If you need to traverse a relationship, you can add a method of your own on the parent that does a raw fetch on the child, has a predicate added where the parent relationship is self, add your own sort orderings and returns a nice NSArray :)
I mean hey, if you can force iOS5, great, but if you need to keep iOS 4 around, letting the database do the sorting is very performant.

Resources