在Cocoa中获取更新

在Cocoa中获取更新,cocoa,core-data,fetch,nsfetchrequest,Cocoa,Core Data,Fetch,Nsfetchrequest,这是myAppDelegate.m中的代码: -(IBAction)fetch:(id)sender{ NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"Foo" inManagedObjectContext:[self managedObjectContext]]; [fetchReque

这是my
AppDelegate.m
中的代码:

-(IBAction)fetch:(id)sender{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Foo" inManagedObjectContext:[self managedObjectContext]];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"title == 'some title'"];
[fetchRequest setPredicate:predicate];
NSError *error = nil;
NSArray *fetchedObjects = [[self managedObjectContext] executeFetchRequest:fetchRequest error:&error];
if (fetchedObjects == nil) {NSLog(@"Error: %@", error);}
NSMutableArray *fooArray = [[NSMutableArray alloc]init];
for (Foo *f in fetchedObjects) {
    //here another fastenum for a to-many relationship
    for(Bar *b in f.relationship){
        [fooArray addObject:b.title];
    }
}
每次执行fetch操作时,即使我通过UI更改了app.storedata文件并在finder中检查了更改,在退出应用程序之前,结果始终相同。重新启动后,获取结果是最新的,并与app.storedata文件对齐。fooArray计数总是相同的,不管我是否在entities中添加了一些条目,coredata是否保存了所有内容。 我尝试过[FetchRequestSetIncludesPendingChanges:YES],但它不会影响行为。 如何在应用程序运行时更新获取结果

更新:我已经“解决”了此解决方案的问题:

-(IBACTION)fetch:(id)sender{
    _managedObjectContext = nil;
    _persistenStoreCoordinator = nil;
    //rest of the code...

这是最终解决方案吗?是否有更“正确”的方法来解决此问题?

是否保存上下文?是的,正如我前面提到的,应用程序正在执行正确的保存操作,并且在应用程序运行时更新app.storedata文件。