0

I want to save the records in an array and delete them from the CoreData. I have tried using NSCopying but it seems that copyWithZone doesn't work on NSManagedObject. I am really stuck, any help will be appreciated.

Saleh
  • 380
  • 3
  • 19
  • you need to read the values, store the values in an array and get rid of your core data stuff. One of the reasons I never liked core data is because it is such a pain to learn yet another system – Antwan van Houdt Nov 16 '11 at 13:29
  • Yes, i was thinking of copying the values in some strings and store them in array. CoreData really is pain. Thanks for your reply. – Saleh Nov 16 '11 at 13:39
  • can you explain more what do you want to do? do you want to save them for short time or permanently? core data is not a pain, when you get it's quite simple. and BTW your problem seems to be not a core data problem. – shannoga Nov 16 '11 at 13:46
  • i want to save them in my array and delete them from the core data. I get results in my array correctly but when i delete the entity from the core data my the pointers in the arrays start pointing to null as the original values have been deleted. I want a deep copy of the values when i fetch the results. – Saleh Nov 16 '11 at 13:49

1 Answers1

1

Copy all the field values of your NSManagedObject to an Dictionary, and store these dictionaries in the array.

I've put together a small routine that you can implement in a NSMutableObject category that could be used to obtain that.

(please, be aware that I'm not on a Mac computer right now and there might be typos on the code, but other than that, it should work fine).

-(NSDictionary*)retrieveAsDict {
  NSMutableDictionary *aux = [[NSMutableDictionary alloc] init];
  NSDictionary *attributes = [self attributesByName];
  NSArray *attributeNames = [attributes allKeys];

  for (NSString *key in attributeNames) {
    [aux setValue:[self valueForKey:key] forKey:key];
  }

  NSDictionary *ret = [NSDictionary dictionaryWithDictionary:aux];

  // Uncomment this if not using ARC
  //[aux release];

  return(ret);
}

If you need further clarification, be sure to comment and I'll answer shortly.

Sergio Moura
  • 4,888
  • 1
  • 21
  • 38