A
A
Alexander Zarochintsev2014-06-15 15:32:33
Objective-C
Alexander Zarochintsev, 2014-06-15 15:32:33

CoreData - how to save data correctly?

Good day, there was a problem when saving data, from the beginning:
There is JSON: (Data about the user - the name of the entity "Client")

{
    "area_residence" = "\U041a\U0438\U0435\U0432";
    "b_day" = "1991-12-29";
    comment = "";
    email = "[email protected]";
    gender = 1;
    id = 4142;
    "last_visit" = "2014-06-11 15:30:21";
    marital = 0;
    name = "\U0410\U043b\U0435\U043a\U0441\U0430\U043d\U0434\U0440";
}

I save data:
for (NSDictionary *dictionary in data) {
        NSManagedObject *managedObject = [NSEntityDescription insertNewObjectForEntityForName:@"Client" inManagedObjectContext:managedObjectContext];

        for (NSString *key in [dictionary allKeys]) {
            if ([managedObject respondsToSelector:NSSelectorFromString(key)]) {
                if (!) {
                    [managedObject setValue:[dictionary objectForKey:key] forKey:key];
                }
            }
        }

        [managedObjectContext insertObject:managedObject];
    }

    [managedObjectContext save:&error];

Saving is successful, but the task has not changed much, and now you need to store in the Client entity and the user's nationality, which looks like:
nationality =     {
        id = 216;
        ar = Ukraine;
        de = Ukraine;
         en = Ukraine;
         fr = Ukraine;
         ru = "\U0423\U043a\U0440\U0430\U0438\U043d\U0430";
         ua = "\U0423\U043a\U0440\U0430\U0438\U043d\U0430";
    };

JSON in full:
{
    "area_residence" = "\U041a\U0438\U0435\U0432";
    "b_day" = "1991-12-29";
    comment = "";
    email = "a[email protected]";
    gender = 1;
    id = 4142;
    "last_visit" = "2014-06-11 15:30:21";
    marital = 0;
    name = "\U0410\U043b\U0435\U043a\U0441\U0430\U043d\U0434\U0440";
    nationality =     {
        id = 216;
        ar = Ukraine;
        de = Ukraine;
         en = Ukraine;
         fr = Ukraine;
         ru = "\U0423\U043a\U0440\U0430\U0438\U043d\U0430";
         ua = "\U0423\U043a\U0440\U0430\U0438\U043d\U0430";
        };
    }

And so the entity "Client":
3359d7774da2460bb3b17a44d499e3fb.png
The entity "Nationality":
7ea89967b80f4aa6aec3d45c8c6cbae8.png
General screen:
11a27abbd2504224a273654baf2beb3f.png
When I restart, I get an error:
2014-06-15 15:26:50.092 CoreDataEntity[3444:60b] -[__NSCFDictionary managedObjectContext]: unrecognized selector sent to instance 0x8d77720
2014-06-15 15:26:50.094 CoreDataEntity[3444:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary managedObjectContext]: unrecognized selector sent to instance 0x8d77720'

Apparently, the object cannot be written to 'NSManagedObject':
[managedObject setValue:[dictionary objectForKey:key] forKey:key];

How then can you proceed so that you can quickly and painlessly record data? Thank you in advance!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Denis Morozov, 2014-06-15
@morozovdenis

well, you set the dictionary in the relationship. set the condition not to check that this is a relationship, write the second loop and set the field values

C
CT4H, 2014-06-15
@CT4H

- (void)parseData:(NSArray*)data
{
    for (NSDictionary *clientDictionary in data) {
        
        NSManagedObject *managedObject = [NSEntityDescription insertNewObjectForEntityForName:@"Client" inManagedObjectContext:managedObjectContext];
        //Вытаскиваете нужное нужное значение, добавляете его в объект
        ...
        [managedObject setValue:simpleValue forKey:key];

        NSDictionary* dictNational = [clientDictionary  objectForKey:@"nationality"];
        NSManagedObject *natObject = [NSEntityDescription insertNewObjectForEntityForName:@"nationality" inManagedObjectContext:managedObjectContext];
        //Аналогично парсите dictNational в natObject
       ...
       [natObject setValue:simpleValue forKey:key];

       [managedObject setValue: natObject forKey:@"nationality"];
    }
    [managedObjectContext save:&error];
}

V
Vanya Ivanov, 2015-01-04
@mr_cloud

https://vk.com/videos-58860049?z=video-58860049_16...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question