当我尝试在视图控制器中加载其属性时,Managedobject变为零。我的操作流程如下。我的所有操作都发生在主线程(thread1)中。
这里我从moc2
创建一个managedobjectManagedObjectClass* someClass = (ManagedObjectClass*)[NSEntityDescription insertNewObjectForEntityForName:@"ManagedObjectClass" inManagedObjectContext:moc2];
someClass.orderID = @"123";
NSError *error = nil;
moc2 save:&error];
//下面是我的NSManagedObjectContextDidSaveNotification处理程序
- (void) mocDidSaveNotification:(NSNotification *)notification
{
NSManagedObjectContext *savedContext = [notification object];
// ignore change notifications for the main MOC
if ([CoreDataController sharedCoreDataController].moc2 == savedContext)
{
return;
}
dispatch_async(dispatch_get_main_queue(), ^{
[[CoreDataController sharedCoreDataController].moc1 mergeChangesFromContextDidSaveNotification:notification];
});
}
//现在使用moc1
获取创建的someClassdispatch_async(dispatch_get_main_queue(),^{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"ManagedObjectClass" inManagedObjectContext:moc1];
[fetchRequest setEntity:entity];
NSPredicate *matchedPredicate = [NSPredicate predicateWithFormat:@"orderID = 123"];
[fetchRequest setPredicate:matchedPredicate];
NSError *error = nil;
NSMutableArray *fetchedObjects = [[[moc1 executeFetchRequest:fetchRequest error:&error]mutableCopy]autorelease];
// Here am able to see the contents of currentOrder
[appcontroller sharedAppController].currentOrder = [fetchedObjects objectAtIndex:0]
});
//现在加载视图控制器
DetailsViewController *aSelectedViewController = [[DetailsViewController alloc]initWithNibName:@"DetailsViewController" bundle:nil];
UINavigationController aNavigationController = [[[UINavigationController alloc] initWithRootViewController:aSelectedViewController]autorelease];
self.applicationWindow.rootViewController = navigationController;
//查看控制器的实现
@implementation DetailsViewController
- (void)viewDidLoad
{
if([appcontroller sharedAppController].currentOrder == nil)
{
NSLog(@"currentOrder became nil");
}
}
@end