Answer the question
In order to leave comments, you need to log in
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";
}
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];
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";
};
{
"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";
};
}
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'
[managedObject setValue:[dictionary objectForKey:key] forKey:key];
Answer the question
In order to leave comments, you need to log in
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
- (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];
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question