我有一个非常奇怪的问题让我很困惑!
我有一个核心数据实体,我刚刚添加了一些新属性:
已删除 - 布尔值 deletedDate - 日期
我有以下代码,按下后会将这两个值设置在核心数据对象上:
- (IBAction)deleteButtonInTable:(id)sender {
//Get the ID of the currently selected item in the table
NSInteger selected = [self.tweetTableView rowForView:sender];
//Create a predicate and fetch the objects from Core Data
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSPredicate *testForTrue = [NSPredicate predicateWithFormat:@"approved == NO"];
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"postDate" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1, nil];
[request setPredicate:testForTrue];
[request setSortDescriptors:sortDescriptors];
[request setEntity:[NSEntityDescription entityForName:@"Tweet" inManagedObjectContext:_managedObjectContext]];
//Setup the Request
[request setEntity:[NSEntityDescription entityForName:@"Tweet" inManagedObjectContext:_managedObjectContext]];
//Assign the predicate to the fetch request
NSError *error = nil;
//Create an array from the returned objects
NSArray *fetchedObjects = [_managedObjectContext executeFetchRequest:request error:&error];
Tweet *selectedTweet = [fetchedObjects objectAtIndex:selected];
if (selectedTweet) {
selectedTweet.deleted = [NSNumber numberWithBool:TRUE];
selectedTweet.deletedDate = [NSDate date];
NSLog(@"%@",selectedTweet);
[self refreshTableView];
if (! self.tweetTableView){
NSLog(@"Tableview doesn't exist!!)");
}
[[self tweetTableView] reloadData];
[[self managedObjectContext] commitEditing];
[self saveAction:nil];
}
if ([self.autoWriteTweets isEqualToString:@"YES"]){
[self writeTweetsToXML];
[self saveAction:nil];
}
}
现在,如果我在 xcode 中观察对象有一些中断,我可以在通过函数时看到对象的属性更改,但是我有一个显示数据源的表,该表被过滤为仅显示具有已删除布尔值的对象设置为true,并且没有任何东西出现在那里。
现在,为了让事情变得更加混乱,我有一个导出对象数组的函数:
-(void)writeTweetsToXML{
//Create new fetch request
NSFetchRequest *request = [[NSFetchRequest alloc] init];
//Set new predicate to only fetch tweets that have been favourited
NSPredicate *filterFavourite = [NSPredicate predicateWithFormat:@"approved == YES"];
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:self.exportSort ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1, nil];
[request setSortDescriptors:sortDescriptors];
//Setup the Request
[request setEntity:[NSEntityDescription entityForName:@"Tweet" inManagedObjectContext:_managedObjectContext]];
[request setResultType:NSDictionaryResultType];
//Assign the predicate to the fetch request
[request setPredicate:filterFavourite];
NSError *error = nil;
//Create an array from the returned objects
NSArray *tweetsToExport = [_managedObjectContext executeFetchRequest:request error:&error];
NSAssert2(tweetsToExport != nil && error == nil, @"Error fetching events: %@\n%@", [error localizedDescription], [error userInfo]);
//NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//NSString *path = [NSString stringWithFormat:@"%@/tweets.xml", documents];
NSString *writeerror;
if(tweetsToExport) {
NSString * exportLocationFull = [[NSString alloc]initWithFormat:@"%@/tweets.xml",self.exportLocation];
BOOL success = [tweetsToExport writeToFile:exportLocationFull atomically:YES];
NSLog(@"Write Status = %d to %@", success, exportLocationFull);
}
else {
NSLog(@"%@",writeerror);
}
}
现在,当我查看导出的文件时,发生了两件奇怪的事情!
首先,我看到的一个对象将其删除值设置为 true,导出值为 0。
其次,deletedDate 属性根本不导出,尽管它位于核心数据模型中。我看不出有任何可能发生这种情况,因为我没有对导出进行特定过滤。
就像某个地方的 getter/setter 坏了,但我检查了类文件,一切都正常并设置为@dynamic。
任何帮助将不胜感激,因为我对到底发生了什么有点迷茫。
人们曾警告过我有关核心数据的怪癖,但这很奇怪!
干杯
加雷斯
注1
顺便说一句,我使用第一部分中完全相同的代码来设置过滤对象的其他属性,这似乎工作正常!