0

if set min. value of property say "X" equal to 1. Than for some scenario, the value goes to 0, how will the model behave? throw error/ warning? My scenario is I wanted to show count of messages in a label but when there is no messages then zero comes up in view. Instead of code I wanna try through xcmodel only (if possible) i.e. if the count goes less then min. value of that property then label should hide. It do hides if zero FOR the first time run BUT some transitional movements of view controllers leading to update that count label causes zero to show up.

djay
  • 375
  • 2
  • 18

3 Answers3

0

I have made a game one's and used this for keeping score:

var Score: Int = 0 {
    didSet {
        ScoreLabel.text = "Score: \(Score)"
    }
}

you could put a if statement in it to hide the label:

Label.text = Score
If score < min {
Label.hidden = true
}else{
Label.hidden = false
}
Cing
  • 806
  • 1
  • 11
  • 29
0

Core Data won't let you save the context if the value goes out of boundaries you have set while modelling the data.

Entity name is SomeEntity. It has an attribute count (of Integer 16 type), which is set to Minimum = 0.

Here is the snipped where the new object:

SomeEntity *object = [NSEntityDescription insertNewObjectForEntityForName:NSStringFromClass([SomeEntity class])
                                                       inManagedObjectContext:context];

object.count = @(-1);

NSError *error;
BOOL success = [context save:&error];

// success == NO here

if (!success) {
                NSLog(@"ERROR saving data: %@", error.localizedDescription);
            }

Here is the error object debug description:

Printing description of error:
Error Domain=NSCocoaErrorDomain Code=1620 "The operation couldn’t be completed. (Cocoa error 1620.)" UserInfo={NSValidationErrorObject=<SomeEntity: 0x7876eed0> (entity: SomeEntity; id: 0x7876d970 <x-coredata:///SomeEntity/tEAD37BAD-81E7-47AE-A182-E35D9BCC69DB2> ; data: {
    count = "-1";
    someUUID = "CD69E59E-B8AD-4615-8257-1F1BD6632B3E";
}), NSValidationErrorKey=count, NSLocalizedDescription=The operation couldn’t be completed. (Cocoa error 1620.), NSValidationErrorValue=-1}
Yevhen Dubinin
  • 4,657
  • 3
  • 34
  • 57
0

From the Core Data Programming Guide:

The validation constraints are applied by Core Data only during a save operation or upon request (you can invoke the validation methods directly at any time it makes sense for your application flow)

So it is possible to set an attribute of NSManagedObject to a value less than the specified minimum, but it will case an NSError during NSManagedObjectContext save.

As for your exact scenario, it's hard to give you an advice without looking through your data model and code. I would probably recommend you to debug your view controllers transitions and view updates (since you say that some particular transition sequence causes the view to behave incorrectly).

FreeNickname
  • 7,398
  • 2
  • 30
  • 60