I have two project with same code to test the behaviors of uniques attributes in iOS and Mac OS and find they are different.
First, I created a entity named Person
and added a attribute name
to it. Then added this attribute to the constraints
.
Second, I added [_managedObjectContext setMergePolicy:NSMergeByPropertyObjectTrumpMergePolicy];
to - (NSManagedObjectContext *)managedObjectContext
in AppDelegate.m
.
At last, I modify the viewDidLoad
in ViewController.m
as follow:
- (void)viewDidLoad {
[super viewDidLoad];
AppDelegate *sharedDelegate = [[UIApplication sharedApplication] delegate];
// NSApplication for Mac OS
Person *salary = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:sharedDelegate.managedObjectContext];
[salary setName:@"a"];
[sharedDelegate.managedObjectContext save:nil];
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Person"];
NSArray *result = [[sharedDelegate managedObjectContext] executeFetchRequest:request error:nil];
NSLog(@"%lu",(unsigned long)[result count]);
for (Person *item in result) {
NSLog(@"%@",[item name]);
}
}
After I run these two project several times, I found that the uniqueness of attribute is guaranteed in iOS while is incorrect in Mac OS. Was it a existed bug , or I have done the test in wrong way?