IOS Core Data executeFetchRequest returns an empty object

I am using Core Data to store an object that includes a transform attribute NSDictionary. I can see the object in the .SQLite file after I save it, so I think (?), I'm good there. However, when I try to get the whole object, I get an NSArray with one element [0], which is zero and (of course) crashes when trying to access any attribute.

HVBAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];

NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"Events" inManagedObjectContext:context];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:entityDesc];

NSError *error;
NSArray *objects = [context executeFetchRequest:fetchRequest error:&error];

// [objects count] = 1 but objects[0] = nil

// and the following line crashes of course
NSMutableDictionary *data = objects[0][@"data"];

Any idea what I'm doing wrong? Thank!

(Note that I created the Events class with the NSDictionary property and other properties.)

This is how I set up the object:

enter image description here

Events.h:

enter image description here

Events.m:

enter image description here

+4
source share
2 answers

I realized what was going on:

-, , "" , "", , . , , , setReturnsObjectsAsFaults:

[fetchRequest setReturnsObjectsAsFaults:NO];

, NSLogging ":".

-, , "" NSArray NSDictionarys NSMutableDictionarys. , [objects objectAtIndex:0]" with "objects[0], [objects[0] valueForKey:@"created"] [objects[0][@"created"]. .

, -!

+7

, willAccessValueForKey: @ "data"

[objects[0] willAccessValueForKey:@"data"];
NSMutableDictionary *data = objects[0][@"data"];
[objects[0] didAccessValueForKey:@"data"];
0

Source: https://habr.com/ru/post/1537757/


All Articles