나는 모든 메시지는이 사용자가 보내 내 채팅 응용 프로그램에서 다음 개체 (일대 다 관계)자 NSPredicate는 컬렉션의 최신 항목에 대한
@interface ChatEntry : NSManagedObject
@property (nonatomic, retain) NSString * text;
@property (nonatomic, retain) NSDate * timestamp;
@property (nonatomic, retain) ChatContext *context;
@end
@interface ChatContext : NSManagedObject
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * userId;
@property (nonatomic, retain) NSSet *entries;
@end
각 대화 (ChatContext) 그룹이 (고유하게 식별 by userId 필드).
각 대화의 마지막 메시지를 표시하는 대화 목록을 표시하려고합니다 (iMessage와 유사).
나는 다음과 같은 코드를 사용하고 있습니다 : 그 시점에서 나는 (NSFetchedResultsChangeInsert으로) 새로운 메시지를받을 때까지
NSFetchRequest* request=[[NSFetchRequest alloc] initWithEntityName:@"ChatEntry"];
request.predicate=[NSPredicate predicateWithFormat:@"[email protected]"];
request.sortDescriptors=[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"timestamp" ascending:NO]];
m_chat=[[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:m_context sectionNameKeyPath:nil cacheName:@"chat"];
m_chat.delegate=self;
이 잘 작동
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath
{
...
}
을, 내 테이블에 다른 항목을 얻을 전망. 페치 컨트롤러는 이전 항목을 유지합니다 (타임 스탬프가 더 이상 최신 항목이 아닐지라도). 이 문제를 해결하고 컨트롤러가 항상 술어를 평가하도록하는 방법을 모르겠습니다.
또는 ChatContext 인스턴스를 가져 오기 위해 쿼리를 변경하려고했지만 실제로 컨텍스트에서 최신 항목을 읽는 방법을 모르겠습니다.
해결 했습니까? –
아래 내 대답을 확인하십시오. –