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.
Related
This question already has answers here:
Swift 3. NSFetchRequest propertiesToFetch
(2 answers)
Closed 4 years ago.
I just need to fetch values of a single attribute from an entity and do not need to fetch all the attributes unnecessarily. To achieve this I have tried using 'propertiesToFetch' property with my fetch request, but it seems to return all the attribute values for the entity.
Say, I have an entity named 'Person' which has attributes 'name', 'age', 'height', 'weight'. Now I need to get only the height values. My fetch request is as below
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Person")
request.propertiesToFetch = ["height"]
Now when I retrieve the values, I expect name, age, weight for each person be nil, and all persons have only height values.
But it's not that way, and I can still see the values for name, age, the weight which seems to me that all the properties are trying to be fetched which I want to avoid.
The above is just an example. As I am dealing with enormous data, I am trying to optimize the fetch time by picking up only the necessary values wherever possible.
Can someone please point out what I am missing here to make it proper?
I you have a large amount of data in you Entity and you don't want to load it into memory every time the object is faulted the solution is to use relationships. Generally the large amount of data is a single property (imageData, or something similar), and can be changed to a one-to-one relationship with an entity with a single property. This will allow you to assess the data just as easily (i.e. myObject.dataHolder.data instead of myObject.data), but you can't search or sort by it. You can do the same things for all properties that you don't need to search or sort by, but unless those properties are very large I don't see a big return on that.
As pointed out in the comments, propertiesToFetch is only for dictionaryResultType and is generally not a good solution for most applications where a managedObject subclass is needed.
Is there a way to use a 'transient' field or something like that and in some way sort accordingly with a NSFetchedResultsController. I want to do the following:
I have location of places in a database. When a person opens the list, I want to show the nearest place on top, an then sort accordingly to distance. But clearly,this depends on the users location, so I cannot use a static field. I was hoping to use a transient field, as you can use for the section headers.
Is there anybody who can give a solution or workaround for this situation?
You cannot use a transient property in a fetch request for a SQlite base Core Data store.
See Fetching Managed Objects in the "Core Data Programming Guide":
You cannot fetch using a predicate based on transient properties
(although you can use transient properties to filter in memory
yourself). ... To summarize, though, if you execute a fetch directly, you should
typically not add Objective-C-based predicates or sort descriptors to
the fetch request. Instead you should apply these to the results of
the fetch.
You can use a transient property for sectionNameKeyPath, but even then you need a first sort descriptor for the sections that is based on a persistent attribute.
So the only workaround is probably to fetch all objects and then sort the fetched array. But then of course you don't have the advantages of a fetched results controller anymore.
Suppose I have this core data structure:
code
name
address
value
is that possible to add 100 to value during the fetch? I mean, to receive a fetch results where all values are their values on the database + 100?
I know I can enumerate all results after and change the value but I am wondering if there is a way to do that while I fetch.
No, all a fetch can do is give you the literal contents of the persistent store. If you're fetching a managed object subclass you might consider adding a computed property as a category method that always returns the named field plus 100; if you're fetching dictionaries then you'll probably have to modify them yourself (which will be a hassle as they'll come back immutable).
I'm using NSFetchedResultsController (NSFRC) to display information in a UITableView. I'm trying to create the option for the user to sort the cells in sections as opposed to alphabetically. The problem is, the sections would then be determined using downloaded information. On top of this the section for each item will be changing relatively often so I don't want to save the section. I have noticed the mention of transient attributes, in my research of similar problems, but i've never used these before I'm not sure if I can use them baring in mind that all the calculations are done once the data has already been loaded, and I also want this solution to be compatible with my previous Core Data database. Also I'm not particularly great at Core Data, (nor Objective-C at that!) so I'm not entirely sure how I'd go about doing this.
So here's what I want to go for if we're using transient attributes (this next bit is theoretical as I don't know if transient attributes are the correct way forward). I would like 4 possible sections, 0-3 (I'll rename them using the TableView delegate to get around sorting problems). When the calculations are done, each cell will be assigned the transient attribute (if needed, the default section would be 2). I hope this all makes sense.
Right, now for some theoretical code. First I create the transient property in the Data Model screen-thing, and make it transient by checking the transient check box... Sounds simple enough.
In the code for the calculations in willDisplayCell (needs to be done in wDC for a couple of reasons), the entity could be saved like this:
MyEntity *myEntity = [self.fetchedResultsController objectAtIndexPath:indexPath];
myEntity.sectionTransientProperty = 2;
if (![self.managedObjectContext save:&error]) {
NSLog(#"Error: %#", error);
FATAL_CORE_DATA_ERROR(error);
return;
}
Done, right? Is that how we assign a value to a transient property?
Then I change the sorting option in NSFRC when I alloc it:
fetchedResultsController = [[NSFetchedResultsController alloc]
initWithFetchRequest:fetchRequest
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:#"sectionTransientProperty"
cacheName:#"MyEntity"];
How are we doing, what else do I need to do? Or have I got this so horribly wrong I should just give up on Core Data and NSFRC? If you guys could help guide me through this I'd really appreciate it. If you need me to post any more code I would be happy to.
Regards,
Mike
If you want an FRC with sections, you have to add a sort descriptor to the fetch request, and that sort descriptor cannot be based on transient attributes.
See the documentation of initWithFetchRequest:managedObjectContext:sectionNameKeyPath:cacheName:`:
If the controller generates sections, the first sort descriptor in
the array is used to group the objects into sections; its key must
either be the same as sectionNameKeyPath or the relative ordering
using its key must match that using sectionNameKeyPath.
and Fetch Predicates and Sort Descriptors in the "Core Data Programming Guide":
The SQL store, on the other hand, compiles the predicate and sort
descriptors to SQL and evaluates the result in the database itself.
This is done primarily for performance, but it means that evaluation
happens in a non-Cocoa environment, and so sort descriptors (or
predicates) that rely on Cocoa cannot work. The supported sort
selectors are ...
In addition you cannot sort on transient properties using the SQLite store.
This means that you cannot create sections purely on transient attributes. You need a persistent attribute that creates the ordering for the sections.
UPDATE: A typical use of a transient attribute as sectionNameKeyPath is: Your objects have a "timeStamp" attribute, and you want to group the objects into sections with one section per month (see the DateSectionTitles sample code from the iOS Developer Library). In this case you have
a persistent attribute "timeStamp",
use "timeStamp" as first sort descriptor for the fetch request,
a transient attribute "sectionIdentifier" which is used as sectionNameKeyPath. "sectionIdentifier" is calculated from "timeStamp" and returns a string representing the year and the month of the timestamp, e.g. "2013-01".
The first thing the FRC does is to sort all fetched objects according to the "timeStamp" attribute. Then the objects are grouped into sections according to the "sectionIdentifier" attribute.
So for a FRC to group the objects into sections you really need a persistent attribute. The easiest solution would be to add a persistent attribute "sectionNumber" to your entity, and use that for "sectionNameKeyPath" and for the first sort descriptor.
I only have a limited experience in using NSSortDescriptor.
It was sorting on one key and it worked fine.
But here is what I need now, I have a set of pairs of numbers, for example :
{(2,3), (44,5), (6,17), (33,7) ……(173,21)}
I want to sort the pairs (x,y) according to the value of a given function myfunction(x,y).
There is the trivial idea of making triplets (x,y,z) where z would be the computation of myfunction(x,y) and then sort the set of triplets, but this not what I want.
Is there a proper way to use NSSortDescriptor to do what I need?
Thanks for any information.
Unless you are using this sort descriptor with Core Data using a SQLite store, you could create a transient attribute on the object where the attribute represented z, the computation of your function. You could then sort on that transient attribute and it would produce the results you want, without storing the z values.
However, if you are using Core Data with a store like SQLite where the entire store contents are not read into memory, you cannot use transient attributes to sort on and you would have to either store your z values actually in the managed objects in order to achieve what you are describing, or read all of your managed objects into an array, and then sort that array of objects on the transient property... which would work OK if you only had a limited number of managed objects, but not so well otherwise.