views:

26

answers:

1

I want to create an NSManagedObject with the contents of a NsDictionary. and Visa Versa.

I have a NSDictionary with object and keys that is being brought in from a MYSQL database and stored to the documents directory. I can't find good info for editing a dictionary so I thought I would try NSManaged Object instead.

If the Object attributes get changed I want to be able to reverse the procedure and send the object back.

Any help of finding an example of this would be great.

Thanks,

Michael

A: 

Here is how I am doing this for creating the NSManagedObject, works like a charm:

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:kParentChildSKUSUrl,@"8"]];
NSArray *array = [[NSArray alloc] initWithContentsOfURL:url];

int j = 0;
int saveThreshold = 500;

for (NSDictionary* dict in array) {
  j+=1;
  ParentChildSKU *entity = (ParentChildSKU*) [NSEntityDescription   insertNewObjectForEntityForName:@"ParentChildSKU" inManagedObjectContext:managedObjectContext];
  [entity setValuesForKeysWithDictionary:dict];

  if (j%saveThreshold==0) {
    NSLog(@"Saving after 500 items");
    NSError *error;
    if (![managedObjectContext save:&error]) {
    // Handle the error.
     }
  }             
}

See this question also, this is where I got started: http://stackoverflow.com/questions/1343121/plist-array-to-nsdictionary

Slee