Fetched Property in Core Data

孤人 提交于 2019-12-04 06:50:06

A fetched property will work, and indeed I used it in my own project with a Post->Comment relationship which needs to be sorted by 'date added index'.

There are a number of caveats: You cannot specify a sort descriptor in the visual editor and have to specify it in code.

I use 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 My Post entity I have a fetched property called "sortedComments" which is defined as:

post == $FETCH_SOURCE

where posts have a to-many "comments" relationship and comments have a "post" inverse

In opposition to the other answers here: The benefits of using a fetched property like this, is CoreData takes care of the caching and invalidating the cache as comments for a post or indeed the post that owns them changes.

If you want to gain some performance, do your fetch with an NSFetchedResultsController and have it working with a cache. Next time you perform the same fetch, the fetch will be faster. In your particular name, you will have to cache names. Take a look at the NSFetchedResultsController documentation.

A fetched property is basically a fetch request. I am not aware of ways to add sort descriptors to these properties in the GUI, but I may be wrong. But why not just create a fetch request in your carsByDatePurchased method and provide a sort descriptor? It returns an array or the results (which you can wrap cheaply in an NSOrderedSet with copyItems: flag set to no).

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;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!