UA-17470720-3

Jump to content


Photo
- - - - -

SCPropertyTypeObjectSelection with many-to-many Core Data relationship


  • Please log in to reply
5 replies to this topic

#1 Dave Guerin

Dave Guerin

    Forum Master

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 539 posts
  • LocationIreland
Reputation: 134
Popular

Posted 23 September 2015 - 01:58 AM

Hi all,
 
I'm not sure where I'm going wrong here, I think it's the many-to-many relationship.
 
So I have SetlistEntity one-to-many SongEntity one-to-many InstrumentEntity.
And then SetlistEntity is also one-to-many InstrumentEntity.
And InstrumentEntity is many-to-many SongEntity.
 
Displaying/Adding/Editing of the SetlistEntity one-to-many SongEntity one-to-many InstrumentEntity is all working great, thanks to STV :-)
 
But I now want to display a list of the InstrumentEntity with a drill down to all the SongEntity they are played on, and then putting that list of SongEntity into Edit mode allows more SongEntity to be selected.
 
 
Here's my first go:
 

 SCEntityDefinition *songEntityDefinition1 = [SCEntityDefinition definitionWithEntityName:kdgSongEntity
                                                                     managedObjectContext:kdgManagedObjectContext
                                                                      propertyNamesString:@":(songName);"
                                                 ];
    songEntityDefinition1.orderAttributeName = kdgSongOrder;



    SCEntityDefinition *instrumentEntityDefinition1 = [SCEntityDefinition definitionWithEntityName:kdgInstrumentEntity
                                                                              managedObjectContext:kdgManagedObjectContext
                                                                               propertyNamesString:@":(instrumentName);:(instrumentSongs);"
                                                       ];
    instrumentEntityDefinition1.orderAttributeName = kdgInstrumentOrder;


    SCPropertyDefinition *instrumentNamePropertyDefinition1 = [instrumentEntityDefinition1 propertyDefinitionWithName:@"instrumentName"];
    instrumentNamePropertyDefinition1.existsInCreationMode = NO;
    instrumentNamePropertyDefinition1.existsInEditingMode = NO;
    instrumentNamePropertyDefinition1.existsInNormalMode = YES;
    instrumentNamePropertyDefinition1.existsInDetailMode = NO;


    SCPropertyDefinition *instrumentSongsPropertyDefinition1 = [instrumentEntityDefinition1 propertyDefinitionWithName:@"instrumentSongs"];
    instrumentSongsPropertyDefinition1.existsInCreationMode = NO;
    instrumentSongsPropertyDefinition1.existsInEditingMode = NO;
    instrumentSongsPropertyDefinition1.existsInNormalMode = YES;
    instrumentSongsPropertyDefinition1.existsInDetailMode = YES;

    SCArrayOfObjectsAttributes *instrumentSongsArrayOfObjectsAttributes1 = [SCArrayOfObjectsAttributes attributesWithObjectDefinition:songEntityDefinition1
                                                                                                                     allowAddingItems:NO
                                                                                                                   allowDeletingItems:NO
                                                                                                                     allowMovingItems:NO
                                                                                                           expandContentInCurrentView:YES
                                                                                                                 placeholderuiElement:nil
                                                                                                                addNewObjectuiElement:nil
                                                                                              addNewObjectuiElementExistsInNormalMode:NO
                                                                                             addNewObjectuiElementExistsInEditingMode:NO
                                                                            ];

    instrumentSongsPropertyDefinition1.attributes = instrumentSongsArrayOfObjectsAttributes1;

    NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"instrumentSetlist == %@", self.currentSetlistEntity];

    SCArrayOfObjectsSection *instrumentSongsSection1 = [SCArrayOfObjectsSection sectionWithHeaderTitle:nil
                                                                                      entityDefinition:instrumentEntityDefinition1
                                                                                       filterPredicate:predicate1
                                                        ];
    instrumentSongsSection1.detailViewControllerOptions.navigationBarType = SCNavigationBarTypeNone;

    instrumentSongsSection1.sectionActions.detailModelConfigured = ^(SCTableViewSection *section, SCTableViewModel *detailModel, NSIndexPath *indexPath) {
        DGBLog(@"instrumentSongsSection1 sectionActions detailModelConfigured");

        detailModel.cellActions.willConfigure = ^(SCTableViewCell *detailCell, NSIndexPath *detailIndexPath) {
            detailCell.accessoryType = UITableViewCellAccessoryNone;
            detailCell.selectable = NO;
        };
    };

 

   [self.tableViewModel addSection:instrumentSongsSection1];

 
 
So that does give me a list of InstrumentEntity with a detailView showing all the related SongEntity. Great! But no selection of SongEntity is possible this way.
 
 
So my second go was this:
 

    SCEntityDefinition *instrumentEntityDefinition2 = [SCEntityDefinition definitionWithEntityName:kdgInstrumentEntity
                                                                              managedObjectContext:kdgManagedObjectContext
                                                                               propertyNamesString:@"instrumentSongs"
                                                       ];
    instrumentEntityDefinition2.orderAttributeName = kdgInstrumentOrder;

    SCPropertyDefinition *instrumentSongsPropertyDefinition2 = [instrumentEntityDefinition2 propertyDefinitionWithName:@"instrumentSongs"];
    instrumentSongsPropertyDefinition2.type = SCPropertyTypeObjectSelection;
    instrumentSongsPropertyDefinition2.existsInCreationMode = NO;
    instrumentSongsPropertyDefinition2.existsInEditingMode = YES;
    instrumentSongsPropertyDefinition2.existsInNormalMode = YES;
    instrumentSongsPropertyDefinition2.existsInDetailMode = YES;

    NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"instrumentSetlist == %@", self.currentSetlistEntity];

    instrumentSongsPropertyDefinition2.attributes = [SCSelectionAttributes attributesWithItems:[self.currentSetlistEntity.setlistSongs allObjects]
                                                                        allowMultipleSelection:NO
                                                                              allowNoSelection:YES
                                                                         autoDismissDetailView:YES
                                                                   hideDetailViewNavigationBar:NO
                                                     ];



    SCArrayOfObjectsSection *instrumentSongsSection2 = [SCArrayOfObjectsSection sectionWithHeaderTitle:nil
                                                                                      entityDefinition:instrumentEntityDefinition2
                                                                                       filterPredicate:predicate2
                                                        ];

    instrumentSongsSection2.cellActions.willConfigure = ^(SCTableViewCell *cell, NSIndexPath *indexPath) {
        InstrumentEntity *instrument = (InstrumentEntity*)cell.boundObject;
        cell.textLabel.text = instrument.instrumentName;
    };

    instrumentSongsSection2.detailViewControllerOptions.navigationBarType = SCNavigationBarTypeEditRight;
    instrumentSongsSection2.detailViewControllerOptions.allowEditingModeCancelButton = NO;

        [self.tableViewModel addSection:instrumentSongsSection2];

But instrumentSongsSection2.cellActions.willConfigure is not giving me a SCSelectionCell, and drilling down gives me a single cell "Instrument Songs" which drills down to a table to select from, but selecting anything gives a crash:

 

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unacceptable type of value for to-many relationship: property = "instrumentSongs"; desired type = NSSet; given type = SongEntity; value = <SongEntity: 0x7faf037bb800> (entity: SongEntity; id: 0xd000000000a00002 <x-coredata://035578E7-775F-47FF-95F6-15BE24D1F48C/SongEntity/p40> ; data:...... 

 

So I'm obviously going wrong somewhere.

 

Does anyone have any pointers of where I've gone wrong?


Cheers,

Dave

www.dgapps.ie

#2 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 23 September 2015 - 01:27 PM

Hi Dave,

 

I apologize if I am not following correctly since it has been a long day, but in your first option, shouldn't 'instrumentSongsArrayOfObjectsAttributes1' be of type SCObjectSelectionAttributes instead of SCArrayOfObjectsAttributes? Similarly, 'instrumentSongsPropertyDefinition1' should be of type SCPropertyTypeObjectSelection.



#3 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 23 September 2015 - 01:32 PM

I have to add that following data definitions in code now is almost impossible :-)


  • Dave Guerin likes this

#4 Dave Guerin

Dave Guerin

    Forum Master

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 539 posts
  • LocationIreland
Reputation: 134
Popular

Posted 24 September 2015 - 02:29 AM

OK, so I've taken the plunge and I'm trying it with a Storyboard!!!

 

 

Here's the storyboard:

 

Attached File  xcode1.png   320.42K   17 downloads

 

which gives screenshot1:

 

Attached File  screenshot1.png   97.08K   23 downloads

 

then screenshot2:

 

Attached File  screenshot2.png   47.96K   22 downloads

 

and then screenshot3:

 

Attached File  screenshot3.png   96.52K   16 downloads

 

 

Wow! That was relatively easy!

 

 

But I want to go from screenshot1 directly to screenshot3 without the intermediate screenshot2. And then remove all the non selected Songs from display, and put them back only in Edit mode, but I think I should be able to work that bit out :-) So how do I go directly from screenshot1 to screenshot3?

 

I tried making the SCCustomCell in the Array Of Objects Section a SCObjectSelectionCell instead  but that gives me the list of Instruments that don't do anything when I select them, so I'm obviously missing something somewhere.


Cheers,

Dave

www.dgapps.ie

#5 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 25 September 2015 - 01:45 PM

Great job with the IB data definitions :)

 

So isn't this the same as marking the 'Expand Contents in View' checkbox for the 'Array of Objects' property definition? For instance, if you did that in our bundled 'TasksApp', tapping on the task will immediately display the task steps without having to dig deeper. 



#6 Dave Guerin

Dave Guerin

    Forum Master

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 539 posts
  • LocationIreland
Reputation: 134
Popular

Posted 25 September 2015 - 02:06 PM

Hi Tarek,

 

I was surprised how simple it was to do the IB data definitions. The next app I start will be Storyboards and IB all the way! :-) This app is oh so nearly ready for release that I don't want to go back and change all of it! Well beta release anyway...

 

 

I don't have a 'Expand Contents in View" checkbox for the instrumetSongs property when it's an Object Selection:

 

 

Attached File  xcode2.png   284.47K   2 downloads

 

 

I do have the checkbox for instrumentSetlist which is just an Object:

 

 

Attached File  xcode3.png   255.21K   1 downloads

 

 

Or am I looking in the wrong place?


Cheers,

Dave

www.dgapps.ie




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users