我有一个很奇怪的问题,但如果我能解释得很好,我想答案不会那么困难。 这是我目前的场景:从特定部分删除核心数据对象
我有一个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可以决定他们属于哪个部分==>如果存在许多对象,则性能很差。 –