我有一個很奇怪的問題,但如果我能解釋得很好,我想答案不會那麼困難。 這是我目前的場景:從特定部分刪除核心數據對象
我有一個tableView有六個固定部分。
這些部分填充了核心數據對象。
我正在使用瞬態屬性來決定對象必須顯示在哪個部分。此過程在NSManagedObject子類中完成。瞬態屬性稱爲sectionIdentifier
。
由於使用瞬態屬性來決定每個對象應該出現哪個部分的原因,我決定在每個部分上創建一個特殊的行,意圖在每個部分總是至少有一行。這個選項將減少問題的空白部分,在添加或刪除行的情況下會引發異常,至少在測試應用程序時這是一個問題。
這意味着在第一次啓動時,每個部分都有一行(我將稱它爲特殊行)。然後用戶可以完美地添加/刪除行。
我現在的問題是,如果該部分有新行,我需要刪除此特殊行。同時,如果用戶刪除了該部分中的最後一行,應用程序會自動創建一個新的特殊行,因此該部分永遠不會爲空。
我知道這不是管理它的最好方式,但是由於我的iOS知識,這是我發現在從空白部分添加或刪除行時避免出現異常的最佳方式。
我需要的是當用戶向節中添加了至少一個新行時,從節中刪除特殊行。
該對象具有名爲quitar
的字符串屬性和特殊行:quitar = @"si"
。這樣,所有特殊的行很容易識別。
我使用這個代碼刪除特殊行是否有部分新行:
NSManagedObjectContext *context = self.managedObjectContext;
NSEntityDescription *entity=[NSEntityDescription entityForName:@"ToDoItem" inManagedObjectContext:context];
NSFetchRequest *fetch=[[NSFetchRequest alloc] init];
[fetch setEntity:entity];
NSPredicate *predicate=[NSPredicate predicateWithFormat:@"quitar like 'si'"];
[fetch setPredicate:predicate];
NSError *fetchError;
NSArray *fetchedData=[self.managedObjectContext executeFetchRequest:fetch error:&fetchError];
for (NSManagedObject *item in fetchedData){
[context deleteObject:item];
}
有人告訴我,你不能在NSPredicate
使用臨時屬性,你可以在我的代碼看,所有特殊的行都被刪除,而不僅僅是特定的部分。 我需要你的幫助來找到一種方法來刪除我想要的部分中的特殊行,而不是從所有部分中刪除。
謝謝你,對不起我的英語。
EDITED
請在下面找到我的方式在NSManagedObject
類ToDoItem
分配sectionIdentifier:
-(NSString *)sectionIdentifier{
[self willAccessValueForKey:@"sectionIdentifier"];
NSString *tmp = [self primitiveValueForKey:@"sectionIdentifier"];
[self didAccessValueForKey:@"sectionIdentifier"];
if (!tmp){
NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger comps = (NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit);
NSDate *today = [NSDate date];
NSDate *date = self.todoDueDate;
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setCalendar:calendar];
[components setYear:2065];
[components setMonth:11];
[components setDay:12];
NSDate *dateFuturoSomeday = [calendar dateFromComponents:components];
NSCalendar *calendar1 = [NSCalendar currentCalendar];
NSDateComponents *components1 = [[NSDateComponents alloc] init];
[components1 setCalendar:calendar1];
[components1 setYear:2065];
[components1 setMonth:11];
[components1 setDay:13];
NSDate *dateFuturoCompleted = [calendar1 dateFromComponents:components1];
NSDateComponents *date1Components = [calendar components:comps
fromDate: today];
NSDateComponents *date2Components = [calendar components:comps
fromDate: date];
NSDateComponents *date3Components = [calendar components:comps
fromDate: dateFuturoSomeday];
NSDateComponents *date4Components = [calendar components:comps
fromDate: dateFuturoCompleted];
today = [calendar dateFromComponents:date1Components];
date = [calendar dateFromComponents:date2Components];
dateFuturoSomeday = [calendar dateFromComponents:date3Components];
dateFuturoCompleted = [calendar dateFromComponents:date4Components];
NSInteger daysAfterToday = [calendar components:NSDayCalendarUnit
fromDate:today toDate:date options:0].day;
// NSString *section;
if (daysAfterToday < 0) {
tmp = @"0";
} else if (daysAfterToday == 0) {
tmp = @"1";
} else if (daysAfterToday > 0 && daysAfterToday < 2) {
tmp = @"2";
}
else if ([self.todoDueDate isEqualToDate:dateFuturoSomeday]){
tmp = @"4";
}
else if ([self.todoDueDate isEqualToDate:dateFuturoCompleted]){
tmp = @"5";
}
else {
tmp = @"3";
}
[self setPrimitiveValue:tmp forKey:@"sectionIdentifier"];
}
return tmp;
}
你如何計算你的「sectionIdentifier」,請分享代碼?你可以改變你的「sectionIdentifier」是持久的而不是暫時的嗎? –
我將把「sectionIdentifier」的代碼編輯爲我的問題。我不知道是否有必要將其更改爲持久性,我認爲必須有一個理由來維持它的臨時性... – mvasco
當您使用瞬態屬性作爲「sectionNameKeyPath」時,所有對象在抓取中必須出現故障並且計算他們的部分,以便FRC可以決定他們屬於哪個部分==>如果存在許多對象,則性能很差。 –