UA-17470720-3

Jump to content


Photo
- - - - -

Setting predicate


  • Please log in to reply
11 replies to this topic

#1 rickbdotcom

rickbdotcom

    Jr. Member

  • STV 5.0 Pro
  • PipPip
  • 13 posts
Reputation: 4
Good

Posted 04 June 2015 - 10:20 PM

I'm having an issue with trying to set a predicate on a SCTableViewModel/SCObjectSection I have setup in IB. I'm trying to do something like this in viewDidLoad since there's no way to specify a predicate like this in IB.

 

NSPredicate* predicate = [NSPredicate predicateWithFormat:@"userEmail == %@",[PFUser currentUser].email];

SCArrayOfObjectsModel* tableViewModel = (id)self.tableModelView;

tableViewModel.dataFetchOptions = [SCDataFetchOptions optionsWithSortKey:@"createdAt" sortAscending:YES filterPredicate:predicate];

 
at this point tableViewModel is of type SCTableViewModel and only at some indeterminate point in the future does it get set to an object of SCArrayOfObjectModels (not even in viewDidAppear). I tried overriding setTableViewModel on the view controller but it's never called so I don't seem to have any well defined override point to set the predicate.
 
I know I can just create the model at runtime without specifying a tableview section in IB but then I lose all the UI customization I've done in IB. I think I'll just resort to taking the SCObjectSection out of the table view, setting an IBOutlet to it, then subclassing SCArrayOfObjectModels to return that section. Unless anyone has any other suggestions.


#2 rickbdotcom

rickbdotcom

    Jr. Member

  • STV 5.0 Pro
  • PipPip
  • 13 posts
Reputation: 4
Good

Posted 04 June 2015 - 10:43 PM

Hmm, I thought maybe the solution was in the model actions, but then I remembered the actual tableViewModel is getting changed so I still don't seem to have any place to override.


Edited by rickbdotcom, 04 June 2015 - 10:47 PM.


#3 wizgod

wizgod

    I'm what you guys call a User

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 575 posts
  • LocationThe Grid
Reputation: 149
Popular

Posted 05 June 2015 - 06:08 AM

Greetings Program!

 

You might want to try putting it in sectionActions.didFetchItemsFromStore or modelActions.didFetchItemsFromStore:

 

// self.tableViewModel.modelActions.didFetchItemsFromStore = ^(SCArrayOfItemsModel *itemsModel, NSMutableArray *items)

self.tableViewModel.sectionActions.didFetchItemsFromStore = ^(SCArrayOfItemsSection *itemsSection, NSMutableArray *items)
{
    // This is not the only way to sort, but probably the easiest when using STV
    NSPredicate* predicate = [NSPredicate predicateWithFormat:@"userEmail == %@",[PFUser currentUser].email];
    
    SCDataFetchOptions *fetchOptions = [SCDataFetchOptions optionsWithSortKey:@"createdAt" sortAscending:YES filterPredicate:predicate];
    
    [fetchOptions filterMutableArray:items];
    [fetchOptions sortMutableArray:items];
};

 

Wg


Edited by wizgod, 05 June 2015 - 06:09 AM.

P.S. I love Swift... talk Swift.. Never too old school to learn yet another programming language. LOL! ;-)


#4 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 05 June 2015 - 06:16 AM

Hi Rick,

 

Are you sure you're using an 'SCObjectSection' and not an 'SCArrayOfObjectsSection'? I am asking because an SCObjectSection only displays the contents of a single object, which makes having a predicate not useful at all. An SCArrayOfObjectsSection on the other had displays many objects, and having predicates for that makes perfect sense (and can be set in both IB and code).



#5 rickbdotcom

rickbdotcom

    Jr. Member

  • STV 5.0 Pro
  • PipPip
  • 13 posts
Reputation: 4
Good

Posted 05 June 2015 - 12:05 PM

yeah wizgod, I thought of that but the issue is that the tableViewModel actually *changes* to another object.

 

tarek, I'm definitely using a SCArrayOfObjectsSection. The table actually works fine and display a list of objects except for the fact I can't get hold of the correct table model to set the predicate in the code to filter it. The issue is that for some reason the model is of type SCTableViewModel in viewDidLoad then eventually changes to a SCArrayOfObjectModel later. Note this is all without me doing anything programmatically. I need to be able to set the predicate in the code since there's no way to specify something like [NSPredicate predicateWithFormat:@"userEmail == %@",[PFUser currentUser].emailin IB.

 

I'll create an example project to replicate.



#6 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 05 June 2015 - 02:25 PM

Hi Rick,

 

A view controller's SCTableViewModel will never change to an SCArrayOfObjectsModel unless you specifically set it to that. Is that the case? If you're not doing any modifications, then you probably have a regular SCTableViewModel with an SCArrayOfObjectsSection. In this case, you can easily set the predicate in IB by selecting the property definition responsible for that section and setting the value there. Just let me know if that's your case and I'll attach some screenshots of what I mean.


Edited by Tarek, 05 June 2015 - 02:29 PM.


#7 rickbdotcom

rickbdotcom

    Jr. Member

  • STV 5.0 Pro
  • PipPip
  • 13 posts
Reputation: 4
Good

Posted 06 June 2015 - 10:20 AM

Oh I see. OK that makes more sense now. I just assumed that the tableViewModel would have been a SCArrayOfObjectsModel since I set the tableview to have a SCArrayOfObjectsSection.

 

‚ÄčI still have the issue with setting the predicate in IB. Yeah I know there's a text entry field there to set the predicate but I don't see how I could use it for a predicate that depends on stuff that needs to be set at runtime, like the userEmail in my example above. If create the SCArrayOfObjectsModel at runtime I lose all the UI customization, I need some way to "get at" the UI I setup in IB.

 

OK I'll futz around around some more now that I have a slightly clearer understanding. Yeah I'm not sure why I was seeing it change to SCArrayOfObjectsModel,  I must have left in the code that was explicitly setting it during testing.



#8 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 06 June 2015 - 11:21 AM

In cases like yours where the predicate depends on some runtime value, it's best of course to set that in code. You could easily fetch the data definition you created in IB at runtime and set the predicate there. For example, the following code sets a predicate for the TaskSteps in our CoreDataApp bundled sample:

 

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Fetch the entity definition created in IB
    SCDataDefinition *taskEntityDef = [self dataDefinitionWithIBName:@"TaskEntity"];
    
    SCPropertyDefinition *taskStepsPDef = [taskEntityDef propertyDefinitionWithName:@"taskSteps"];
    SCArrayOfObjectsAttributes *taskStepsAttributes = (SCArrayOfObjectsAttributes *)taskStepsPDef.attributes;
    taskStepsAttributes.objectsFetchOptions.filterPredicate = ...;  // whatever you want here   
}

  • wizgod likes this

#9 rickbdotcom

rickbdotcom

    Jr. Member

  • STV 5.0 Pro
  • PipPip
  • 13 posts
Reputation: 4
Good

Posted 06 June 2015 - 12:55 PM

Ah thanks! I knew there had to be some way to do it, I just wasn't sure how to go about it.

 

Thanks for prompt responses, and the great framework, it's worth every penny.  You guys rock!


  • Tarek likes this

#10 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 06 June 2015 - 12:58 PM

Thanks Rick, really appreciate your kind complements :)



#11 rickbdotcom

rickbdotcom

    Jr. Member

  • STV 5.0 Pro
  • PipPip
  • 13 posts
Reputation: 4
Good

Posted 06 June 2015 - 01:54 PM

That last code snippet wasn't quite what I needed since it's setting the predicate on the properties. However, I finally got it working now thanks to all your help. It was super simple once my initial confusion over the relationship between the tableViewModel and tableSection was was cleared up. So for future reference:

 

SCArrayOfObjectsSection* section = (id)[self.tableViewModel sectionAtIndex:0];

section.dataFetchOptions.filterPredicate = [NSPredicate predicateWithFormat:@"userEmail == %@",[PFUser currentUser].email];


  • Tarek and wizgod like this

#12 Morten Jacobsen

Morten Jacobsen

    Experienced Member

  • STV 5.0 Pro
  • PipPipPipPip
  • 52 posts
Reputation: 10
Good

Posted 29 December 2015 - 04:43 PM

Or in Swift:

 

let section = self.tableViewModel.sectionAtIndex(0) as! SCArrayOfObjectsSection

section.dataFetchOptions.filterPredicate = NSPredicate(format:"userEmail == %@"PFUser.currentUser, email)

 

PS. I'm not sure about PFUser.currentUser is a correct translation of [PFUser currentUser]. But it is tested with other variables in viewDidlLoad() in other context.

  • Tarek and wizgod like this




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users