Received property in master data

In my main data model, a Person has one or more Cars defined by a disordered to many relationship. Often I need to get Person cars ordered by datePurchased , or dateLastUsed .

So far, I have added my own method to Person for carsByDatePurchased . This uses a sort descriptor to sort NSSet Cars and returns an NSArray.

Can the Fetched property be used instead for this? I experience some performance overhead by using a sort descriptor every time I need cars in a specific order, even if you plan to implement your own carsByDatePurchased caching. It seems like the selected property is cached for me - is this correct?

What are the limitations of the highlighted property and my own implementation?

And most importantly, is the property value obtained at boot time maintained between executions? If I update the selected property and keep my context, will the value be saved the next time the application starts?

+6
source share
4 answers

The acquired property will work, and I really used it in my own project with the Post-> Comment relation, which needs to be sorted by the index of the added date.

There are a few caveats: you cannot specify a sort descriptor in a visual editor and specify it in code.

I'm using something like this

  // Find the fetched properties, and make them sorted... for (NSEntityDescription *entity in [_managedObjectModel entities]) { for (NSPropertyDescription *property in [entity properties]) { if ([property isKindOfClass:[NSFetchedPropertyDescription class]]) { NSFetchedPropertyDescription *fetchedProperty = (NSFetchedPropertyDescription *)property; NSFetchRequest *fetchRequest = [fetchedProperty fetchRequest]; // Only sort by name if the destination entity actually has a "index" field if ([[[[fetchRequest entity] propertiesByName] allKeys] containsObject:@"index"]) { NSSortDescriptor *sortByName = [[NSSortDescriptor alloc] initWithKey:@"index" ascending:YES]; [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortByName]]; } } } } 

In the My Post object, I have a selected property called sortedComments, which is defined as:

 post == $FETCH_SOURCE 

where posts relate to many "comments" and comments have a "post" backlink

Contrary to other answers here: The benefits of using a selected property, such as CoreData, take care of caching and invalidating the cache as comments on a message or indeed a message that belongs to them.

+4
source

If you want some performance, make a selection using NSFetchedResultsController and run it with the cache. The next time you perform the same selection, the selection will be faster. In your specific name, you will have to cache the names. Take a look at the NSFetchedResultsController documentation .

+2
source

The acquired property is basically a select request. I do not know how to add sort descriptors to these properties in the GUI, but I could be wrong. But why not just create a select request in your carsByDatePurchased method and provide a sort handle? It returns an array or results (which you can trim cheaply in an NSOrderedSet with the copyItems: flag set to no).

+1
source
 AppDelegate *delegate = [UIApplication sharedApplication].delegate; NSManagedObjectContext *context = [delegate managedObjectContext]; NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"DataRecord" inManagedObjectContext:context]; [fetchRequest setEntity:entity]; NSError *error; fetchedObjects = [context executeFetchRequest:fetchRequest error:&error]; for (NSManagedObject *obj in fetchedObjects) { NSLog(@"Name: %@", [obj valueForKey:@"name"]); NSLog(@"Info: %@", [obj valueForKey:@"info"]); NSLog(@"Number: %@", [obj valueForKey:@"number"]); NSLog(@"Create Date: %@", [obj valueForKey:@"createDate"]); NSLog(@"Last Update: %@", [obj valueForKey:@"updateDate"]); } NSManagedObject *obj = [fetchedObjects objectAtIndex:0]; [self displayManagedObject:obj]; selectedObject = obj; 
0
source

All Articles