Welcome, Guest
Username Password: Remember me

Need a -(BOOL)canInsertManagedObject... like method
(1 viewing) (1) Guest

TOPIC: Need a -(BOOL)canInsertManagedObject... like method

Need a -(BOOL)canInsertManagedObject.​.. like method 1 year, 4 months ago #1

I'm saving user login information using core data. I'm using a SCArrayOfObjects to list them in the table. It adds the login information fine using a detail controller but what I would like to do is catch and verify that the login information is valid with the external server before the save is made.

Is there a -(BOOL)canInsertManagedObject:(NSManagedObject*)managedObject withEntityName:(NSString*)entityName like delegate function that would allow me to disallow or warn about invalid data?

Gary
  • panamind
  • OFFLINE
  • Senior Boarder
  • Posts: 77
  • Karma: 4

Re: Need a -(BOOL)canInsertManagedObject.​.. like method 1 year, 4 months ago #2

Hi Gary,

You can provide your own custom validation on the detail view. Here is how to do so:

1. Specify which properties you want to provide custom validation for in your class definition by setting their autoValidate property to FALSE.

 
...
SCClassDefinition *classDef = [SCClassDefinition definitionWithEntityName:@"My Entity"
withManagedObjectContext:myContext
withPropertyNames:[NSArray arrayWithObjects:@"field1", @"field2", @"field3", nil]];
[classDef propertyDefinitionWithName:@"field1"].autoValidate = FALSE;
...
 


2. Implement the tableViewModel:valueIsValidForRowAtIndexPath: SCTableViewModelDelegate method, returning TRUE if the cell's value is valid, otherwise return FALSE.

 
- (BOOL)tableViewModel:(SCTableViewModel *)tableViewModel
valueIsValidForRowAtIndexPath:(NSIndexPath *)indexPath
{
SCTableViewCell *cell = [tableViewModel cellAtIndexPath:indexPath];
if([cell isKindOfClass:[SCTextFieldCell class]])
{
NSString *field1Value = ((SCTextFieldCell *)cell).textField.text;
 
// your validation code here
...
}
}
 


Please tell me if this works for you.
  • tarekskr
  • OFFLINE
  • Administrator
  • Posts: 2404
  • Karma: 72

Re: Need a -(BOOL)canInsertManagedObject.​.. like method 1 year, 4 months ago #3

Hey Tarek,

Unfortunately I need to validate using two rows at the same time (loginID and password). On top of that the validation has to wait for a response from a remote server. So I'll need to subclass or create a category to resolve this.

Where should I look to find the code to catch the NSManagedObject before it is saved by the NSManagedObjectContext?
  • panamind
  • OFFLINE
  • Senior Boarder
  • Posts: 77
  • Karma: 4

Re: Need a -(BOOL)canInsertManagedObject.​.. like method 1 year, 4 months ago #4

I see Gary.

We will add a new delegate method to our 2.0 version (which should be out soon) that will look something like this:

 
- (BOOL)tableViewModel:willAddItemForSectionAtIndexPath:item:
 


Implementing this method and returning FALSE will simply cancel adding the new item.
  • tarekskr
  • OFFLINE
  • Administrator
  • Posts: 2404
  • Karma: 72

Re: Need a -(BOOL)canInsertManagedObject.​.. like method 1 year, 4 months ago #5

Can you also add a method:

 
 
- (void)tableViewModel:addItemForSectionAtIndexPath:item
 
 


so I can manually pass the item back after validation?

Thanks
  • panamind
  • OFFLINE
  • Senior Boarder
  • Posts: 77
  • Karma: 4

Re: Need a -(BOOL)canInsertManagedObject.​.. like method 1 year, 4 months ago #6

I am not sure I understand what you're trying to achieve Gary, would you please elaborate?
  • tarekskr
  • OFFLINE
  • Administrator
  • Posts: 2404
  • Karma: 72
Time to create page: 0.98 seconds