Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get an object or value from _NSCoreDataTaggedObjectID

I was following the example by apple: (https://developer.apple.com/library/ios/documentation/DataManagement/Conceptual/CoreDataSnippets/Articles/fetchExpressions.html) to fetch distinct "values"

NSEntityDescription *ahrsMessage = [NSEntityDescription entityForName:@"AHRSMessage" inManagedObjectContext:self.managedObjectContext];
    NSFetchRequest *distinctFetch = [NSFetchRequest new];
    [distinctFetch setEntity:ahrsMessage];
    [distinctFetch setResultType:NSDictionaryResultType];
    [distinctFetch setReturnsDistinctResults:YES];
    [distinctFetch setPropertiesToFetch:@[@"flightRecordings"]];

    NSError *e = nil;
    id requestedValue = nil;
    NSArray *objects = [self.managedObjectContext executeFetchRequest:distinctFetch error:&e];
    if (objects == nil) {
        NSLog(@"ERROR");
    }


    for (NSDictionary *dict in objects) {
        NSLog(@"dict: %@", dict);

       [dict objectForKey:@"flightRecordings"];

    }

When I explore the value objects[1] in the debugger i see my key is _PFEncodedString * and my value is _NSCoreDataTaggedObjectID * What I'm unclear about is how to actually get my CoreData object back out of this datatype.

like image 337
Jeef Avatar asked Jul 28 '14 12:07

Jeef


1 Answers

I just came across this post looking for an answer about this myself. So in case someone runs into this issue, you can get the actual NSManagedObject by passing the value from the result's dictionary in NSManagedObjectContext's objectWithID: method.

So something along the lines of:

NSManagedObjectID *managedObjectID = [dict objectForKey:@"fetched_property_name"]
NSManagedObject *managedObject = [moc objectWithID:managedObjectID]
like image 119
NikosM Avatar answered Oct 26 '22 21:10

NikosM