Adding multiple images in a story Core Data iOS 8

142 views Asked by At

I'm working on this Core Data app here. Its a story app. User can create stories. Now each story can have multiple pictures. Now the core data model is something like this:

Story Entity: Has one to many relation with Story Page, Story Page Entity: Has one to one relation with PictureBank, PictureBank Entity: Contains NSData* for storing picture

Now when I add different pictures in my story and save it, it works fine. But when I add one picture multiple times, only the last added picture gets saved.

For example if I have pic1, pic2, pic3 in picture bank. I create a story in this sequence: pic1, pic2, pic1, pic1 OR pic1, pic1, pic1 and save my story then only the last stored pic1 is available in the saved story and rest are gone. There length property returns 0.

Here is the code I'm using:

- (BOOL)createAndSaveStory:(NSMutableArray *)storyPages andTitle:(NSString *)title withBGColor:(NSString *)bgColor andTheme:(NSString *)themeName isEdited:(BOOL)edited withEditedStoryObject:(Story *)storyObj
{
    Story *newStory = nil;
    if (edited == NO && storyObj == nil)
        newStory = [NSEntityDescription insertNewObjectForEntityForName:@"Story" inManagedObjectContext:_context];
    else
        newStory = storyObj;
    newStory.title = title;
    newStory.bgcolor = bgColor;
    newStory.themeName = themeName;
    newStory.noofpages = [NSNumber numberWithLong:storyPages.count];
    NSMutableArray *pages = [[NSMutableArray alloc] initWithCapacity:5];
    for (int i = 0; i < storyPages.count; i++)
    {
        SSStoryPage *storyPage = [storyPages objectAtIndex:i];
        if (storyPage)
        {
            StoryPage *page = [NSEntityDescription insertNewObjectForEntityForName:@"StoryPage" inManagedObjectContext:_context];
            NSLog(@"Picture Size: %lu", (unsigned long)storyPage.storyPagePictureBank.picture.length);
            page.storyPicture = storyPage.storyPagePictureBank;
            NSLog(@"Picture Size: %lu", (unsigned long)page.storyPicture.picture.length);
            page.storyRecording = storyPage.storyPageRecording;
            page.storyText = storyPage.storyPageText;
            [pages addObject:page];
            page = [pages objectAtIndex:i];
            NSLog(@"Picture Size: %lu", (unsigned long)page.storyPicture.picture.length);
        }
    }
    for (StoryPage *sp in pages)
    {
        NSLog(@"Picture Size: %lu", (unsigned long)sp.storyPicture.picture.length);
    }
    newStory.pages = [[NSOrderedSet alloc] initWithArray:pages];
    NSError *err = nil;
    [_context save:&err];
    if (!err)
        return YES;
    else
        return NO;
}

I receive the pages in an array storyPages. Then in a for loop and iterate through them one by one and for each page create a "StoryPage" entity. Now all the NSLogs in the 1st for loop where I'm creating the pages print the right picture size. Like this:

2015-05-25 16:02:35.617 MySocialStory[4110:795855] Picture Size: 3336172
2015-05-25 16:02:36.687 MySocialStory[4110:795855] Picture Size: 3336172
2015-05-25 16:02:38.070 MySocialStory[4110:795855] Picture Size: 3336172
2015-05-25 16:02:39.650 MySocialStory[4110:795855] Picture Size: 3336172
2015-05-25 16:02:39.949 MySocialStory[4110:795855] Picture Size: 3336172
2015-05-25 16:02:40.973 MySocialStory[4110:795855] Picture Size: 3336172
2015-05-25 16:02:42.524 MySocialStory[4110:795855] Picture Size: 3336172
2015-05-25 16:02:42.920 MySocialStory[4110:795855] Picture Size: 3336172
2015-05-25 16:02:44.203 MySocialStory[4110:795855] Picture Size: 3336172

But immediately after that when I log them again I get:

2015-05-25 16:02:46.943 MySocialStory[4110:795855] Picture Size: 0
2015-05-25 16:02:47.842 MySocialStory[4110:795855] Picture Size: 0
2015-05-25 16:02:48.670 MySocialStory[4110:795855] Picture Size: 3336172

Top two items are zero, gone. And later when I view the saved story the ImageView is empty and last Image has the picture. I don't understand what is happening. Another thing I noticed while debugging was that when I retrieve the PictureBank object, all three objects are pointing to same memory reference. Any help please...???

1

There are 1 answers

0
Asad Javed On

I got it working. The issue was with the relationship between picture bank and story page. Initially I had set it one to one as I thought that a story page would obviously have one picture. But since in core data we work with inverse relationships also, the vice versa is not one to one, that is a picture can be in more than one story page. So I change the relation to one-to-many and since one-to-many relationship gives up warnings in Xcode, saying "Setting up one-to-many relationship is an advance setting" I switched to many-to-many, BUT I use it like one-to-many relationship, ignoring the NSOrderedSet property in my PictureBank NSManagedObject Class. And its works fine now :)