"일"에 정확한 emoAvg를 유지하고 싶습니다.to-many 관련 NSManagedObject 하위 클래스가 변경되면 NSManagedObject 하위 클래스를 업데이트하는 방법은 무엇입니까?
- 갱신에 "감정"이 "날"에 관련 될 때마다 "날"의 emoAvg은 ... 하루에 많은 감정을 가질 수 있습니다 여기에 내가 달성 찾고 있어요거야.
- 갱신에 "감정"이 "날"에서 제거 될 때마다 "날"의 emoAvg
- 업데이트 "날"의 emoAvg 때마다 "감정"변화의 감정 속성입니다.
I 모델 층에있는 모든이 코드를 유지하려면 내가 감정을 만들 때 감정에 날을 첨부 다음 코드를 호출하여 일에 감정을 연결할 수 있었다.
"일"에 "감정"이있는 일대 다 관계에 대한 사용자 지정 접근자는 쓸 수 없었습니다. Xcode 4.6.2에서 멈추었 고 Copy/paste 커스텀 ObjectiveC 접근 자 기능이이 버전에 존재하지 않는 것 같습니다.
이 코드를 작성하는 좋은 방법은 무엇입니까?
- (void)setDate:(NSDate *)date
{
[self willChangeValueForKey:@"date"];
[self setPrimitiveValue:date forKey:@"date"];
[self associateDay];
[self didChangeValueForKey:@"date"];
}
-(void)associateDay
{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:self.date];
//Start of Day
[dateComponents setHour:0];
[dateComponents setMinute:0];
[dateComponents setSecond:0];
NSDate *startOfDay = [calendar dateFromComponents:dateComponents];
//NSLog(@"Start of Day: %@", [dateFormatter stringFromDate:startOfDay]);
//End of Day
[dateComponents setHour:23];
[dateComponents setMinute:59];
[dateComponents setSecond:59];
NSDate *endOfDay = [calendar dateFromComponents:dateComponents];
//NSLog(@"End of Day: %@", [dateFormatter stringFromDate:endOfDay]);
//Find all Day Entities that fall on the same calendar date
NSPredicate *datePredicate = [NSPredicate predicateWithFormat:@"date > %@ AND date < %@", startOfDay, endOfDay];
NSFetchRequest *fetchDays = [[NSFetchRequest alloc]initWithEntityName:@"Day"];
fetchDays.predicate = datePredicate;
NSManagedObjectContext *context = [(id)[UIApplication sharedApplication].delegate managedObjectContext];
NSArray *daysFound = [context executeFetchRequest:fetchDays error:nil];
if ([daysFound count] == 1)
{
Day *dayWithMatchingDate = [daysFound objectAtIndex:0];
self.day = dayWithMatchingDate;
NSLog(@"Found %d matching days",[daysFound count]);
}
if ([daysFound count] > 1)
{
NSLog(@"Found %d matching days...Something needs fixing",[daysFound count]);
}
if ([daysFound count] < 1)
{
NSLog(@"Found %d matching days...Making a new Day and associating it",[daysFound count]);
NSManagedObjectContext *context = [(id)[UIApplication sharedApplication].delegate managedObjectContext];
Day *newDay = [NSEntityDescription insertNewObjectForEntityForName:@"Day" inManagedObjectContext:context];
newDay.date = self.date;
self.day = newDay;
}
}
먼디, 응답 해 주셔서 감사합니다. 질문을하기 전에 내가 링크 한 섹션을 읽었습니다. 당신은 "[[self primitiveEmotions] removeObject : value];" 그 코드의 출처가 확실하지 않습니다. – Aaronium112
모든 관리 객체'attribute'는'setPrimitiveAttribute'와 같은 메소드에 의해 자동으로 지원됩니다. 'removeObject'는 일반적인 NSSet 인스턴스 메소드입니다. – Mundi