Jeef
Jeef

Reputation: 27275

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.

Upvotes: 8

Views: 1691

Answers (1)

NikosM
NikosM

Reputation: 1131

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]

Upvotes: 6

Related Questions