2

I'm using magical record for working with coredata. how to save group of entities in model.

For saving a single object i used following code

StoryData *storyDataEntity=[StoryData MR_createInContext:localContext];
storyDataEntity.stories=storyData;
storyDataEntity.categoryType=categoryType;
storyDataEntity.timeStamp=[NSDate date];

[localContext MR_saveToPersistentStoreWithCompletion:^(BOOL success,NSError *error)
 {
     if (success) {

         }
         if (error) {
             NSLog(@"error:%@",[error localizedDescription]);
         }

     }
 }];

how to save group of entities i.e., more than one entity at a time. I have array of stories i want to store them as an individual entity.

user2823044
  • 315
  • 2
  • 5
  • 14

1 Answers1

0

You need something like that:

for (NSString *oneStoryData in yourArray) //yourArray is your array of stories
{
    StoryData *storyDataEntity=[StoryData MR_createInContext:localContext];
    storyDataEntity.stories= oneStoryData;
    storyDataEntity.categoryType=categoryType;
    storyDataEntity.timeStamp=[NSDate date];
}

[localContext MR_saveToPersistentStoreWithCompletion:^(BOOL success,NSError *error)
 {
     if (success) {

         }
         if (error) {
             NSLog(@"error:%@",[error localizedDescription]);
         }

     }
 }];

Also remember that StoryData is not an NSObject, it is a NSManagedObject.

From Apple documentation:

NSManagedObject is a generic class that implements all the basic behavior required of a Core Data model object. It is not possible to use instances of direct subclasses of NSObject (or any other class not inheriting from NSManagedObject) with a managed object context. You may create custom subclasses of NSManagedObject, although this is not always required. If no custom logic is needed, a complete object graph can be formed with NSManagedObject instances.

What is the object inside your NSArray? A NSString or what?

If your array have StoryData entities, then this is your problem, how are you creating these entities? With [[StoryData alloc]init] ?

Then inside the for you can do something like:

for (StoryData *oneStoryData in yourArray) //yourArray is your array of stories
{
    StoryData *storyDataEntity=[StoryData MR_createInContext:localContext];
    storyDataEntity.property1 = oneStoryData.property1;
    storyDataEntity.property2 = oneStoryData.property2;
}

EDIT

Check this tutorial if you really want to save a whole NSArray/NSMutableArray in Core Data:

Gabriel.Massana
  • 8,165
  • 6
  • 62
  • 81
  • can i store array of storyDataEntities in model – user2823044 Mar 18 '14 at 10:11
  • Check this one: http://stackoverflow.com/a/1562725/1381708. However, for what the hell you want to store an Array of entities? – Gabriel.Massana Mar 18 '14 at 10:18
  • There's no need to log in the completion handler. MagicalRecord will log everything for you if you turn on logging – casademora Mar 18 '14 at 10:37
  • how to store array of emtities in coredata @ Gabriel.Massana – user2823044 Mar 19 '14 at 07:18
  • finally i have done in following way for (int i=0;i<[articles count];i++) { Article *article=[articles objectAtIndex:i]; ArticleData *articleDataEntity=[ArticleData MR_createInContext:localContext]; articleDataEntity.artcle=[NSKeyedArchiver archivedDataWithRootObject:article]; articleDataEntity.articleURL=article.article_url; articleDataEntity.categoryType=categoryType; articleDataEntity.articleType=@"main_article"; [localContext MR_saveToPersistentStoreAndWait]; //[articlesData addObject:articleDataEntity]; } – user2823044 Mar 25 '14 at 11:46