2013-10-04 65 views
0

目前,我有一個表格視圖,它由視頻對象填充,取決於錄製和保存時使用[nsdate date];拍攝的日期。但是,可能在一天內錄製多個視頻。我按照日期對它們進行排序,當有多個視頻具有相同日期時,它顯示該日期記錄的第一個視頻,最近的視頻按照最近的升序排列。我不確定爲什麼發生這種情況,但想要檢查是否有多個錄像帶有相同的日期,並按照標題進行組織。在SQL我可以這樣做:奇怪的預防行爲?

ORDER BY DATE_RECORDED, TITLE ASC

這是我目前在做什麼:

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; 
_managedObjectContext = [appDelegate managedObjectContext]; 

NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"whosVideo == %@", _currentPerson]; 
[request setPredicate:predicate]; 
NSEntityDescription *eval = [NSEntityDescription entityForName:@"Video" inManagedObjectContext:_managedObjectContext]; 
[request setEntity:eval]; 

NSSortDescriptor *sortDescriptor = 
[[NSSortDescriptor alloc] initWithKey:@"date_recorded" 
          ascending:NO 
          selector:@selector(localizedCaseInsensitiveCompare:)]; 
NSArray *sortDescriptors = [[NSArray alloc]initWithObjects:sortDescriptor, nil]; 


[request setSortDescriptors:sortDescriptors]; 

NSError *error = nil; 
NSMutableArray *mutableFetchResults = [[_managedObjectContext executeFetchRequest:request error:&error] mutableCopy]; 
if (mutableFetchResults == nil){ 
    //handle error 
} 

[self setVideoArray:mutableFetchResults]; 
[self.tableView reloadData]; 

我將如何處理添加第二個斷言是否有記錄在多個對象同一日期?

+0

你就不能添加第二個排序描述符和按標題排序? – Ian

回答

1

如果你想比你必須使用NSCompoundPredicate嘗試這樣下面來處理多個斷言: -

NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"whosVideo == %@", _currentPerson]; 

NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"whosVideo == %@", _someOther]; 

    NSArray *subPredicates = [[NSArray alloc] initWithObjects:predicate1, predicate2,nil]; 

NSPredicate *predicate = [NSCompoundPredicate orPredicateWithSubpredicates:subPredicates];