コアデータエンティティがユニークであるかどうかを判断することについて多くの議論があったことは知っていますが、私は人が投げ捨てたさまざまな解決策を理解するのに多くの問題があります。うまくいけば、誰かが私にシンプルなソリューションを提供することができます。コアデータの既存エンティティ
状況:
私は基本的に多対多の関係(イベント - >出席者< - ピープル)であるエンティティを持っています。人々はアドレス帳のエントリへの参照です(私はPeopleエンティティにAddressBookID、firstName、lastNameおよびcreatedOn日付を格納しています)。イベントテーブルには、イベントの詳細があります。出席者テーブルには、イベント(イベント)と人(人)との関係と、ソートのためのcreatedOn日付があります。
問題: 参加者レコードを挿入する前に、ユーザーがイベントに人を既に追加しているかどうかを確認したいと思います。
伝統的なデータベースでは、ユニークキー制約を作成します。重複を挿入しようとすると、エラーが発生し、トラップして移動します(既存のレコードを最新のデータで更新する可能性があります)。
私は次のことを試してみた:任意の助けを事前に
#pragma mark - Selected People Delegate
-(void)returnPeople:(NSMutableArray *)selectedPeople
{
NSError *error = nil;
Attendee *newAttendee;
for (Attendee *selectedPerson in selectedPeople) {
if (self.context == nil) {
NSLog(@"NSManagedObjectContext is nil");
// return nil;
}
newAttendee = [NSEntityDescription insertNewObjectForEntityForName:@"Attendee" inManagedObjectContext:context];
[event addAttendeeObject:newAttendee];
newAttendee.createdOn = [NSDate date];
newAttendee.event = event;
newAttendee.person = selectedPerson;
NSLog(@"Person ID: %@", [selectedPerson.objectID description]);
if ([self uniqueEntityExistsWithEnityName:@"Attendee" UniqueKey:@"person" UniqueValue:[selectedPerson.objectID description] SortAttribute:@"createdOn" ManagedObjectContext:context]) {
NSLog(@"Attendee Exists!");
}
if (![context save:&error]) {
// Handle the error.
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
error = nil;
self.eventAttendee = nil;
[self.attendees insertObject:newAttendee atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
[FlurryAnalytics logEvent:@"Event Attendee Added"];
}
}
#pragma mark - Core Data - Manage Attendees
-(BOOL)uniqueEntityExistsWithEnityName:(NSString*)entityName UniqueKey:(NSString*) uniqueKey UniqueValue:(NSString*)uniqueValue SortAttribute:(NSString*)sortDescriptorAttribute ManagedObjectContext:(NSManagedObjectContext*) context1;
{
BOOL returnValue = NO;
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:entityName];
//what is the correct predates to compare the text an string core data property against a passed in string?
// request.predicate = [NSPredicate predicateWithFormat:@"unique= %@", uniqueValue];
request.predicate = [NSPredicate predicateWithFormat:@"%@=%@", uniqueKey, uniqueValue];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:sortDescriptorAttribute ascending:YES];
request.sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSError *error = nil;
NSArray *matches = [context1 executeFetchRequest:request error:&error];
if (!matches)
{
NSLog(@"Error: no object matches");
}
else if([matches count] > 1) {
NSLog(@"Error: More than one object for unique record");
returnValue = YES;
} else if ([matches count] == 0) {
returnValue = NO;
} else {
returnValue = YES;
}
return returnValue;
}
感謝を!コメントの後
ジェイソン
コードの問題点は何ですか? –
私のコードの問題は、uniqueEntityExistsWithEnityNameメソッドを使うと、人が既に追加されていることが決して見つからないということです。したがって、TRUEを返す必要があるときは、基本的にFALSEを返します。 – JasonBub